Guest User

Untitled

a guest
Jun 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. /*
  2. * Braitenberg Vehicle
  3. *
  4. * A device with two light sensors, each controlling one of two motors.
  5. * When the left sensor sees more light, it drives the right motor faster
  6. * so the vehicle turns to the left.
  7. * When the right sensor sees more light, it drives the left motor faster
  8. * so the vehicle turns to the right.
  9. *
  10. * Two DC motors are controlled by a single H-bridge,
  11. * with the motor control inputs of the H-bridge
  12. * connected to analog output (PWM) pins
  13. * and the analog inputs for speed scaled to a range of 0-255.
  14. *
  15. * The brighter the light, the faster the device moves toward the light.
  16. *
  17. *
  18. * Created by Corinna Sherman
  19. * September 30, 2010
  20. */
  21.  
  22. int LEFT = 0; // index for lightSensorPins array corresponding to left sensor
  23. int RIGHT = 1; // index for lightSensorPins array corresponding to right sensor
  24.  
  25. int CURRENT = 0;// index for lightSensorValues array corresponding to a given sensor's current value
  26. int MIN = 1; // index for lightSensorValues array corresponding to a given sensor's minimum value
  27. int MAX = 2; // index for lightSensorValues array corresponding to a given sensor's maximum value
  28.  
  29. int enableSwitchPin = 8; // enable switch input pin to turn robot ON/OFF
  30.  
  31. int lightSensorPins[] = {0, 1}; // analog input pins for photocells controlling left and right motors
  32.  
  33. // The outer array in the multidimensional array lightSensorValues
  34. // holds sensor values for {leftSensorValues, rightSensorValues};
  35. // Each inner array element is {currentSensorValue, minimumSensorValue, maximumSensorValue}
  36. // for a given sensor.
  37. int lightSensorValues[2][3] = {
  38. { 0,0,0 },
  39. { 0,0,0 }
  40. };
  41.  
  42. int motorLSpeed = 0; // variable to hold the left motor's speed as a PWM in a range of 0-255
  43. int motorRSpeed = 0; // variable to hold the right motor's speed as a PWM in a range of 0-255
  44.  
  45. int enablePinL = 9; // H-bridge enable pin 1 (1,2EN) to enable left motor
  46. int enablePinR = 10; // H-bridge enable pin 2 (3,4EN) to enable right motor
  47.  
  48. int motorLPin1 = 3; // left motor logic pin 1 on H-bridge (1A) with PWM
  49. int motorLPin2 = 5; // left motor logic pin 2 on H-bridge (2A) with PWM
  50. int motorRPin1 = 6; // right motor logic pin 1 on H-bridge (4A) with PWM
  51. int motorRPin2 = 11; // right motor logic pin 2 on H-bridge (3A) with PWM
  52.  
  53. void setup() {
  54.  
  55. // Initialize serial communication.
  56. Serial.begin(9600);
  57.  
  58. // Set enable switch pin as input pin.
  59. pinMode(enableSwitchPin, INPUT);
  60.  
  61. // Set other pins as output pins.
  62. pinMode(enablePinL, OUTPUT);
  63. pinMode(enablePinR, OUTPUT);
  64. pinMode(motorLPin1, OUTPUT);
  65. pinMode(motorLPin2, OUTPUT);
  66. pinMode(motorRPin1, OUTPUT);
  67. pinMode(motorRPin2, OUTPUT);
  68.  
  69. // Disable motor by default until the speed is calculated in the loop function.
  70. disable();
  71. }
  72.  
  73. void loop() {
  74.  
  75. // Get the current, minimum, and maximum light levels
  76. // in the environment from the light sensors.
  77. calibrate();
  78.  
  79. // Calculate the device's speed for analog output (pulse width modulation ranging from 0 to 255)
  80. // using the current light level seen by the light sensor
  81. // as analog input (ranging from min light sensed to max light sensed).
  82. setMotorSpeed();
  83.  
  84. // Go forward by default.
  85. goForward();
  86.  
  87. // If the switch is on, the motor turns in one direction.
  88. if (digitalRead(enableSwitchPin) == HIGH) {
  89. // Set enablePins to HIGH to turn on both motors.
  90. //Serial.println("go");
  91. enable();
  92. }
  93. else {
  94. //Serial.println("stop");
  95. disable();
  96. }
  97.  
  98. }
  99.  
  100. // Calibrate the light sensors on the device.
  101. void calibrate() {
  102. calibrateLightSensor(LEFT);
  103. calibrateLightSensor(RIGHT);
  104. }
  105.  
  106. // Calibrate the light sensor with the given pin index into the lightSensorPins array.
  107. void calibrateLightSensor(int pinIndex) {
  108.  
  109. // Read current value from the light sensor.
  110. lightSensorValues[pinIndex][CURRENT] = analogRead(lightSensorPins[pinIndex]);
  111.  
  112. // For light sensor calibration:
  113. // Set the maximum light sensor value if the current reading exceeds the previous maximum.
  114. // Else, set the minimum light sensor value if the current reading is less than the previous minimum.
  115. if (lightSensorValues[pinIndex][CURRENT] > lightSensorValues[pinIndex][MAX]) {
  116. lightSensorValues[pinIndex][MAX] = lightSensorValues[pinIndex][CURRENT];
  117. }
  118. else if (lightSensorValues[pinIndex][CURRENT] < lightSensorValues[pinIndex][MIN]) {
  119. lightSensorValues[pinIndex][MIN] = lightSensorValues[pinIndex][CURRENT];
  120. }
  121.  
  122. }
  123.  
  124. void enable() {
  125. // Set enable pins to HIGH to turn on both motors.
  126. digitalWrite(enablePinL, HIGH);
  127. digitalWrite(enablePinR, HIGH);
  128. }
  129.  
  130. void disable() {
  131. // Set enable pins to LOW to turn off both motors.
  132. digitalWrite(enablePinL, LOW);
  133. digitalWrite(enablePinR, LOW);
  134. }
  135.  
  136. void goForward() {
  137. // Set left motor's direction
  138. analogWrite(motorLPin1, 0); // Set H-bridge 1A pin to low
  139. analogWrite(motorLPin2, motorLSpeed); // Set H-bridge 2A pin to a speed based on light level
  140.  
  141. // Set right motor's direction
  142. analogWrite(motorRPin1, 0); // Set H-bridge 4A pin to low
  143. analogWrite(motorRPin2, motorRSpeed); // Set H-bridge 3A pin to a speed based on light level
  144. }
  145.  
  146. void goBackward() {
  147. // Set left motor's direction
  148. analogWrite(motorLPin1, motorLSpeed); // Set H-bridge 1A pin to a speed based on light level
  149. analogWrite(motorLPin2, 0); // Set H-bridge 2A pin to low
  150.  
  151. // Set right motor's direction
  152. analogWrite(motorRPin1, motorRSpeed); // Set H-bridge 4A pin to a speed based on light level
  153. analogWrite(motorRPin2, 0); // Set H-bridge 3A pin to low
  154. }
  155.  
  156. // Set the speed variables for both left and right motors
  157. // based on their corresponding light sensor readings
  158. void setMotorSpeed() {
  159.  
  160. // Read current value from each light sensor.
  161. lightSensorValues[LEFT][CURRENT] = analogRead(lightSensorPins[LEFT]);
  162. lightSensorValues[RIGHT][CURRENT] = analogRead(lightSensorPins[RIGHT]);
  163.  
  164. // Calculate the motor speed for analog output (pulse width modulation ranging from 0 to 255)
  165. // using the current light level as seen by the light sensor
  166. // as analog input (ranging from min light sensed to max light sensed).
  167. motorLSpeed = constrain(map(lightSensorValues[LEFT][CURRENT], lightSensorValues[LEFT][MIN], lightSensorValues[LEFT][MAX], 0, 255), 0, 255);
  168. motorRSpeed = constrain(map(lightSensorValues[RIGHT][CURRENT], lightSensorValues[RIGHT][MIN], lightSensorValues[RIGHT][MAX], 0, 255), 0, 255);
  169.  
  170. //Serial.print("left sensor value = ");
  171. //Serial.print(lightSensorValues[LEFT][CURRENT]);
  172. //Serial.print(", right sensor value = ");
  173. //Serial.println(lightSensorValues[RIGHT][CURRENT]);
  174. //Serial.print("left motor speed = ");
  175. //Serial.print(motorLSpeed);
  176. //Serial.print(", right motor speed = ");
  177. //Serial.println(motorRSpeed);
  178. }
Add Comment
Please, Sign In to add comment