Monitoring du Phytotron
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rdtemp.ino 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Phytotron
  3. * lecture des capteurs de temperature LM35
  4. */
  5. /* -------------------------------------------------- */
  6. #define NBVAL 4
  7. #define DELAI 1000
  8. #define RELAIS_FRIGO 42
  9. #define RELAIS_VENTILO 40
  10. #define prt(a) Serial.print(a)
  11. #define prtln(a) Serial.println(a)
  12. typedef struct {
  13. unsigned short magic;
  14. char tag[4];
  15. short delai;
  16. short temp_maxi;
  17. short temp_mini;
  18. short control;
  19. } Global;
  20. Global parametres;
  21. /* -------------------------------------------------- */
  22. void setup() {
  23. Serial.begin(9600);
  24. pinMode(LED_BUILTIN, OUTPUT);
  25. pinMode(RELAIS_FRIGO, OUTPUT);
  26. pinMode(RELAIS_VENTILO, OUTPUT);
  27. Serial.print("\n");
  28. /* XXX */
  29. /* changing the voltage reference of the ADC
  30. * greatly increase the prcision on the limited
  31. * range of our temperatures.
  32. */
  33. analogReference(INTERNAL1V1); // Pour Arduino Mega2560
  34. Serial.print("\n");
  35. read_config(0, &parametres);
  36. // display_config(&parametres);
  37. Serial.print("M running now\n");
  38. }
  39. /* ================================================== */
  40. /*
  41. * the light captor is on A4 pin
  42. */
  43. void readlight(void)
  44. {
  45. short value;
  46. delay(100);
  47. value = analogRead(A4);
  48. Serial.print("L ");
  49. Serial.print(value);
  50. Serial.print('\n');
  51. }
  52. /* ================================================== */
  53. short adc_pins[] = { A0, A1, A2, A3 };
  54. #define NB_PASSE 4
  55. /* -------------------------------------------------- */
  56. void updatevalues(short *ptr)
  57. {
  58. short foo, pass;
  59. for (foo=0; foo<NBVAL; foo++) { ptr[foo] = 0; }
  60. digitalWrite(LED_BUILTIN, HIGH);
  61. for (pass=0; pass<NB_PASSE; pass++) {
  62. for (foo=0; foo<NBVAL; foo++) {
  63. ptr[foo] += analogRead(adc_pins[foo]);
  64. delay(50);
  65. }
  66. }
  67. for (foo=0; foo<NBVAL; foo++) { ptr[foo] /= NB_PASSE; }
  68. digitalWrite(LED_BUILTIN, LOW);
  69. }
  70. /* -------------------------------------------------- */
  71. void sendvalues(short *ptr)
  72. {
  73. int foo;
  74. Serial.print("T");
  75. for (foo=0; foo<NBVAL; foo++) {
  76. Serial.print(" ");
  77. Serial.print(ptr[foo]);
  78. }
  79. Serial.print('\n');
  80. }
  81. /* -------------------------------------------------- */
  82. void update_and_send(void)
  83. {
  84. short values[NBVAL];
  85. updatevalues(values);
  86. sendvalues(values);
  87. }
  88. /* ================================================== */
  89. void loop() {
  90. static int foo = 0;
  91. int key;
  92. update_and_send();
  93. if (foo++ > 5) {
  94. readlight(); foo = 0;
  95. }
  96. /* check for external request */
  97. if (Serial.available()) {
  98. key = Serial.read();
  99. switch (key) {
  100. case 'U':
  101. Serial.println("M cli request");
  102. phytocli();
  103. break;
  104. case '+': /* allume le frigo */
  105. controle_frigo(1);
  106. break;
  107. case '-': /* eteint le frigo */
  108. controle_frigo(0);
  109. break;
  110. case '1': /* allume le ventilo */
  111. controle_ventilo(1);
  112. break;
  113. case '0': /* eteint le ventilo */
  114. controle_ventilo(0);
  115. break;
  116. default:
  117. prtln("M bad control code");
  118. break;
  119. }
  120. }
  121. delay(DELAI);
  122. /* ETERNAL LOOP END HERE */
  123. }
  124. /* -------------------------------------------------- */