Advertisement
seston

Träkker 08.02.2017-2

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