document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * MINI PROJECT
  3.  * - by Iris Howley -
  4.  *
  5.  * Runs two motors connected through an h-bridge.
  6.  * Motors start \'off\', but when the lights go on, then the
  7.  * motors go \'on\'. When the lights turn off, then the motors turn
  8.  * \'off.\'
  9.  **/
  10.  
  11. int hbridgeL1 = 6;    //declares the first pin on the left side of the hbridge for the motor
  12. int hbridgeL2 = 7;    //declares the second pin on the left side of the hbridge for the motor
  13. int motorLpmw = 9;     // this is the pmw that will set how much power the motor is getting (speed)
  14.  
  15. int hbridgeR1 = 4;
  16. int hbridgeR2 = 3;
  17. int motorRpmw = 5;
  18.  
  19. int LEFT_POWER = 255;
  20. int RIGHT_POWER = 255
  21. ;
  22.  
  23. int lightSensorPin = A0;
  24. int lowLightValue = 1000; // stores the current lowest light sense value
  25. int highLightValue = -1; // stores the highest light sense value
  26.  
  27. int RECORDED_LOW_LIGHT = 58;  // the recorded lowest light sense value
  28. int RECORDED_HIGH_LIGHT = 746; // the highest recorded light sense value
  29.  
  30. int status = 0; // default, motors = off
  31. int currentLight = 50; // the amount of light we\'re current experiencing
  32. int lightThreshold = 50; // the min amount light has to change for us to consider the lights changing
  33.  
  34. int movingForward = -1; // everytime the lights turn on/off, we\'re going to switch out direction
  35. // there may be better ways to handle this.
  36.  
  37. unsigned long lastMillisec = 0; // To keep track of time, we need a start time. 0.
  38. int directionChangeCycle = 4000; // Every 3 seconds, we want the direction of the spools to change
  39.  
  40. void setup() {
  41.   Serial.begin(9600);
  42.  
  43.   // Declaring as outputs
  44.   pinMode(hbridgeL1, OUTPUT);          
  45.   pinMode(hbridgeL2, OUTPUT);          
  46.   //pinMode(motorLpmw, OUTPUT);    
  47.  
  48.   pinMode(hbridgeR1, OUTPUT);          
  49.   pinMode(hbridgeR2, OUTPUT);          
  50.   //pinMode(motorRpmw, OUTPUT);    
  51. }
  52.  
  53. void loop() {
  54.   run();
  55.  
  56.   //rightMotorOn();
  57.   //leftMotorOn();
  58.  
  59.   //
  60.   calibrate();
  61.   //testReverse(hbridgeR1, hbridgeR2, motorRpmw);
  62.   //moveForward(hbridgeL1, hbridgeL2, motorLpmw);
  63. }
  64.  
  65. /**
  66.  * Runs the left and right motors. Motors start \'off\', but when the lights turn on
  67.  * the motors turn on. Lights turn off, motors turn off.
  68.  **/
  69. void run() {
  70.   int lightSensorValue = analogRead(lightSensorPin);
  71.   Serial.print(currentLight); // debug printing
  72.   Serial.print(" -> "); // debug printing
  73.  
  74.   Serial.println(lightSensorValue); // debug printing
  75.  
  76.   if (status && ((currentLight-lightSensorValue) > lightThreshold))  { // we\'re on, and the lights turned off
  77.     status = 0; // change the \'status\' of the power, so we know whether to stay on/off
  78.     Serial.println("\\tTURN OFF");
  79.   }
  80.   else if (!status && ((currentLight-lightSensorValue) < -lightThreshold)) { // we\'re off and the lights turned on
  81.     status = 1;
  82.     Serial.println("\\tTURN ON");
  83.   }
  84.   currentLight = lightSensorValue;
  85.  
  86.   if (status) { // continue on
  87.     if (cycleCheck(&lastMillisec, directionChangeCycle)) {
  88.     movingForward = movingForward*(-1); // switch direction!
  89.     }
  90.     if (movingForward > 0) { // if we\'re moving forward, then move forward!
  91.       rightMotorForward();
  92.       leftMotorBackward();
  93.     }
  94.     else { // otherwise, move backward.
  95.       rightMotorBackward();
  96.       leftMotorForward();    
  97.     }
  98.     //TODO: put delays in here!!!
  99.   }
  100.   else { // stay stopped
  101.     analogWrite(motorLpmw, 0); // turn off left motor
  102.     digitalWrite(hbridgeL1, LOW);
  103.     digitalWrite(hbridgeL2, LOW); // I\'m messing something up. Quick fix.
  104.     analogWrite(motorRpmw, 0); // turn off right motor  
  105.   }
  106.  
  107.   // Fix the light value if it\'s not the recorded low/high
  108.   /**
  109.    * if (lightSensorValue < RECORDED_LOW_LIGHT) {
  110.    * lightSensorValue = RECORDED_LOW_LIGHT;
  111.    * }
  112.    * if (lightSensorValue > RECORDED_HIGH_LIGHT) {
  113.    * lightSensorValue = RECORDED_HIGH_LIGHT;
  114.    * }
  115.    **/
  116. }
  117.  
  118. /**
  119.  * Returns true if enough time has gone by (as defined by \'cycle\').
  120.  **/
  121. boolean cycleCheck(unsigned long *lastMillis, unsigned int cycle) {
  122.   unsigned long currentMillis = millis();
  123.   if(currentMillis - *lastMillis >= cycle)
  124.   {
  125.     *lastMillis = currentMillis;
  126.     return true;
  127.   }
  128.   else
  129.     return false;
  130. }
  131.  
  132. /**
  133.  * Move the left motor forward.
  134.  **/
  135. void leftMotorForward() {
  136.   // Keeps the left motor on
  137.   analogWrite(motorLpmw, LEFT_POWER);
  138.   digitalWrite(hbridgeL1, LOW);    //turns the motors on    
  139.   digitalWrite(hbridgeL2, HIGH);
  140. }
  141.  
  142. /**
  143.  * Move the rightft motor forward.
  144.  **/
  145. void rightMotorForward() {
  146.   // Keeps the right motor on
  147.   analogWrite(motorRpmw, RIGHT_POWER);
  148.   digitalWrite(hbridgeR1, LOW);    //turns the motors on    
  149.   digitalWrite(hbridgeR2, HIGH);
  150. }
  151.  
  152. /**
  153.  * Move the left motor "backward".
  154.  **/
  155. void leftMotorBackward() {
  156.   // Keeps the left motor on
  157.   analogWrite(motorLpmw, LEFT_POWER);
  158.   digitalWrite(hbridgeL1, HIGH);    //turns the motors on    
  159.   digitalWrite(hbridgeL2, LOW);
  160. }
  161.  
  162. /**
  163.  * Move the rightft motor "backward".
  164.  **/
  165. void rightMotorBackward() {
  166.   // Keeps the right motor on
  167.   analogWrite(motorRpmw, RIGHT_POWER);
  168.   digitalWrite(hbridgeR1, HIGH);    //turns the motors on    
  169.   digitalWrite(hbridgeR2, LOW);
  170. }
  171.  
  172. /**
  173.  * Senses light through analog input, then outputs
  174.  * current light sensor value, as well as the current
  175.  * high and low sensor values. Use to calibrate the high/low
  176.  * values of the photo sensor.
  177.  **/
  178. void calibrate() {
  179.   int lightSensorValue = analogRead(lightSensorPin);
  180.  
  181.   if (lightSensorValue > highLightValue) {
  182.     highLightValue = lightSensorValue;
  183.   }
  184.   else if (lightSensorValue < lowLightValue) {
  185.     lowLightValue = lightSensorValue;
  186.   }
  187.  
  188.   // Print values
  189.   Serial.print(lowLightValue);
  190.   Serial.print("\\t<\\tcur:");
  191.   Serial.print(lightSensorValue);  
  192.   Serial.print("\\t<\\t");
  193.   Serial.println(highLightValue);  
  194. }
  195.  
  196. /**
  197.  * Tests if the hbridge/motor circuit works.
  198.  * Spins a single motor in one direction for 1000ms, and then in the other direction.
  199.  * @param pin1 the first digital pin coming from the h-bridge
  200.  * @param pin2 the second digital pin coming from the h-bridge
  201.  * @param pmwpin the pwm pin determining how much power to give the motor (0-255)
  202.  **/
  203. void testReverse (int pin1, int pin2, int pmwpin) {
  204.   analogWrite(pmwpin, RIGHT_POWER);      // this is the analog speed value for the arduino (0-255)
  205.   digitalWrite(pin1, HIGH);             //turns the motors on - forwards
  206.   digitalWrite(pin2, LOW);  
  207.   delay(1000);                          // wait    
  208.   digitalWrite(pin1, LOW);              //makes motor go backwards
  209.   digitalWrite(pin2, HIGH);            
  210.   delay(1000);                          // wait
  211.  
  212.  
  213. }
  214.  
  215. /**
  216.  * Tests if the hbridge/motor circuit works.
  217.  * Spins a single motor in one direction.
  218.  * @param pin1 the first digital pin coming from the h-bridge
  219.  * @param pin2 the second digital pin coming from the h-bridge
  220.  * @param pmwpin the pwm pin determining how much power to give the motor (0-255)
  221.  **/
  222. void moveForward (int pin1, int pin2, int pmwpin) {
  223.   digitalWrite(pmwpin, RIGHT_POWER);      // this is the analog speed value for the arduino (0-255)
  224.   digitalWrite(pin1, LOW);             //turns the motors on - forwards
  225.   digitalWrite(pin2, HIGH);  
  226. }
');