Advertisement
seston

Solar tracker up and down with relays

Feb 10th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. #include <Arduino.h>
  2. // PIN CONSTANTS
  3. const byte PIN_LED = 13; // Pin 13 has an LED connected on most Arduino boards.
  4. const byte PIN_LDR_UP = A0; // LDR ülesse poole paneeli
  5. const byte PIN_LDR_DOWN = A1; // LDR alla poole paneeli
  6. const byte PIN_LIMIT_UP = A2; // lääne lõpu lüliti.Peab olema high lõpus low. Kui valgus langeb määratud tasemele-LDR_NIGHT siis pöörab itta?
  7. const byte PIN_LIMIT_DOWN = A3; // ida lõpu lüliti.peab olema high.Lõpus low.Et ei hakkaks enam pöörama itta. Nii riist- kui tarkvaraliselt
  8. const byte PIN_MOTOR_DRIVE_UP = 7;// OUT 1 h-bridge
  9. const byte PIN_MOTOR_DRIVE_DOWN = 8;// OUT 2 h-bridge vahetada OUT 1-ga kui pöörab valele poole
  10.  
  11. // LOGIC CONSTANTS
  12. const byte LDR_DEAD_BAND = 30; // erinevus LDR-de vahel, et hakkaks pöörama
  13. const byte LDR_NIGHT = 1; // väärtus, et on pime ja paneel sõidaks itta ?
  14. const int LDR_INTERVAL = 100; // check LDR every x milliseconds
  15. const int MOTOR_STOP = 0; // 0 to motor_control will stop motors
  16. const int MOTOR_UP = 255; // Amplitude = PWM, sign = direction
  17. const int MOTOR_DOWN = -255; // Amplitude = PWM, sign = direction
  18. const int MOTOR_INTERVAL = 500; // update motor controls every x milliseconds
  19. const int PRINT_INTERVAL = 1000; // print status to serial interface every x milliseconds
  20.  
  21. // GLOBALS
  22. unsigned long timeMotor = 0;
  23. unsigned long timeLDR = 0;
  24. unsigned long timePrint = 0;
  25. int ldrDown = 0;
  26. int ldrUp = 0;
  27. int motor_direction = 0;
  28.  
  29. // light dependant resistor measurememt function
  30. // expects a byte containing the pin to read
  31. // read the analog value of the pin several times, filtering the value to be more repeatable
  32. // protection: check the value of the pin is not full scale or zero, indicating a sensor error
  33. // if protection invoked, return an error message
  34. // otherwise return the result
  35. int read_ldr(byte pin)
  36. {
  37. int ldr_average = analogRead(pin);
  38. for (byte i = 0; i < 5; i++)
  39. {
  40. int value = analogRead(pin);
  41. // if (value == 0 || value == 1023)
  42. // return -1;
  43. ldr_average += value; // ldr_average = ldr_average + value;
  44. ldr_average /= 2; // ldr_average = ldr_average / 2;
  45. }
  46. return ldr_average;
  47. }
  48.  
  49. // motor control function
  50. // expects a signed integer to define direction and duty to drive the motor
  51. // if the parameter is out of range or zero then we stop motors
  52. // if the parameter is greater than zero we drive to the west
  53. // if the parameter is less than zer we drive to the east
  54. // protection: we don't drive in the direction of a limit switch, when it is active.
  55. // returns what happened
  56. int motor_control(int dir)
  57. {
  58. // stop condition
  59. digitalWrite(PIN_LED, LOW);
  60. if (dir == 0 || dir > 255 || dir < -255)
  61. {
  62. // stop motors
  63. //digitalWrite(PIN_MOTOR_ENABLE, LOW);
  64. digitalWrite(PIN_MOTOR_DRIVE_DOWN, LOW);
  65. digitalWrite(PIN_MOTOR_DRIVE_UP, LOW);
  66. motor_direction = MOTOR_STOP;
  67. digitalWrite(PIN_LED, HIGH);
  68. return 0;
  69. }
  70.  
  71. // greater than zero -> drive UP
  72. if (dir > 0)
  73. {
  74. // if on Up limit or driving Down (<0), stop
  75. if (digitalRead(PIN_LIMIT_UP) || motor_direction < 0)
  76. {
  77. motor_control(MOTOR_STOP);
  78. return 2;
  79. }
  80. // drive UP
  81.  
  82. digitalWrite(PIN_MOTOR_DRIVE_UP, HIGH);
  83. motor_direction = MOTOR_UP;
  84. return 1;
  85. }
  86.  
  87. // less than zero -> drive Down
  88. if (dir < 0)
  89. {
  90. // if Down limit or currently driving west (>0), stop
  91. if (digitalRead(PIN_LIMIT_DOWN) || motor_direction > 0)
  92. {
  93. motor_control(MOTOR_STOP);
  94. return -2;
  95. }
  96. // drive Down
  97. digitalWrite(PIN_MOTOR_DRIVE_DOWN, HIGH);
  98. //digitalWrite(PIN_MOTOR_DRIVE_UP, HIGH);
  99. motor_direction = MOTOR_DOWN;
  100. return -1;
  101. }
  102. }
  103.  
  104. // the setup routine runs once when you press reset:
  105. void setup()
  106. {
  107. // digital outputs
  108. pinMode(PIN_LED, OUTPUT);
  109.  
  110. // digital pwm outputs
  111. pinMode(PIN_MOTOR_DRIVE_DOWN, OUTPUT);
  112. pinMode(PIN_MOTOR_DRIVE_UP, OUTPUT);
  113.  
  114. // analog inputs
  115. pinMode(PIN_LDR_DOWN, INPUT);
  116. pinMode(PIN_LDR_UP, INPUT);
  117.  
  118. // digital inputs
  119. pinMode(PIN_LIMIT_DOWN, INPUT);
  120. pinMode(PIN_LIMIT_UP, INPUT);
  121. digitalWrite(PIN_LIMIT_DOWN, HIGH); // enable internal pull-up resistor
  122. digitalWrite(PIN_LIMIT_UP, HIGH); // enable internal pull-up resistor
  123.  
  124. Serial.begin(9600);
  125. }
  126.  
  127. // the loop routine runs over and over again forever:
  128. void loop()
  129. {
  130. unsigned long time = millis();
  131. // check LDRs
  132. if (timeLDR <= time)
  133. {
  134. timeLDR = time + LDR_INTERVAL;
  135. ldrDown = read_ldr(PIN_LDR_DOWN);
  136. ldrUp = read_ldr(PIN_LDR_UP);
  137. }
  138.  
  139. // do motor control
  140. if (timeMotor <= time)
  141. {
  142. timeMotor = time + MOTOR_INTERVAL;
  143. if (ldrDown < 0 || ldrUp < 0)
  144. {
  145. // stop everything
  146. // wait 1 minute
  147. // continue
  148. motor_control(MOTOR_STOP);
  149. Serial.print("Motor stop");
  150. timeMotor = time + 6000;
  151. motor_control(MOTOR_DOWN);
  152. Serial.print("Pöörame ALLA");
  153. delay(5000);
  154. }
  155. else
  156. {
  157. // main operation
  158. if (abs(ldrDown - ldrUp) < LDR_DEAD_BAND && ldrDown > LDR_NIGHT && ldrUp > LDR_NIGHT)
  159. motor_control(MOTOR_STOP);
  160. else if (ldrDown < ldrUp) // when west is more illuminated than east
  161. motor_control(MOTOR_UP);
  162. else if (ldrUp < ldrDown)
  163. motor_control(MOTOR_DOWN);
  164. else if (ldrDown < LDR_NIGHT && ldrUp < LDR_NIGHT)
  165. motor_control(MOTOR_DOWN);
  166.  
  167.  
  168. }
  169. }
  170.  
  171. // print to serial interface
  172. if (timePrint <= time)
  173. {
  174. timePrint = time + PRINT_INTERVAL;
  175. byte limitDown = digitalRead(PIN_LIMIT_DOWN);
  176. byte limitUp = digitalRead(PIN_LIMIT_UP);
  177. Serial.print("Lower LDR, ");
  178. Serial.print(ldrDown, DEC);
  179. Serial.print(",Upper LDR, ");
  180. Serial.print(ldrUp, DEC);
  181. Serial.print(",End switch E-W, ");
  182. Serial.print(limitDown);
  183. Serial.print(",");
  184. Serial.print(limitUp);
  185. Serial.print(",Drive, ");
  186. Serial.print(motor_direction, DEC);
  187. // Serial.print(",Panel,");
  188. Serial.println();
  189.  
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement