Advertisement
Clarvel

Arduino setup and initialization code

Nov 10th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.19 KB | None | 0 0
  1. /*=================================*\
  2. |Blinking LED,spinning motor project|
  3. |        By Matthew Russell         |
  4. \*=================================*/
  5. int i = 0;                                             // startinterger i
  6. int IR1 = 0;                                           // right sensor
  7. int IR2 = 0;                                           // front sensor
  8. int IR3 = 0;                                           // left sensor
  9. int button = LOW;                                      // define interger "button"
  10. int ledState = LOW;                                    // ledState used to set the LED current
  11. long pMillis = 0;                                      // will store last time LED was updated
  12. long blinkinterval = 250;                              // watchdog interval on
  13. long interval = 1000;                                  // watchdog interval total
  14. long mill0 = 0;                                        // time spent in idling loop
  15. long mill1 = 0;                                        // time spent in driving loop
  16. unsigned long cMillis = 0;                             // define current time
  17.  
  18. void setup(){                                          //       ::SETUP::
  19.   pinMode(2,OUTPUT);                                   // motor 1 output :right:
  20.   pinMode(3,OUTPUT);                                   // motor 2 output :left:
  21.   pinMode(4,OUTPUT);                                   // LED output
  22.   pinMode(5,INPUT);                                    // Switch input
  23.   pinMode(6,INPUT);                                    // IR 1 input :right:
  24.   pinMode(7,INPUT);                                    // IR 2 input :front:
  25.   pinMode(8,INPUT);                                    // IR 3 input :left:
  26. }
  27. void loop(){                                           //       ::LOOP::
  28.   button = digitalRead(5);                             // read pin5, write to interger "button"
  29.   if(button == LOW){                                   //    Before button push, do:
  30.     cMillis = millis();                                // read time (in millis) since startup (unsigned ensures positive int)
  31.     if(cMillis - pMillis < blinkinterval){             // if change in time is less than defined blinkinterval, do:
  32.       digitalWrite(4,HIGH);                            // turn on LED
  33.     }
  34.     else if(cMillis - pMillis < interval){             // if blinkinterval < change in time < interval, do:
  35.       digitalWrite(4,LOW);                             // turn off LED
  36.     }
  37.     else{                                              // if change in time > interval, reset, and save time:
  38.       pMillis = cMillis;                               // save the last LED current change
  39.     }
  40.   }  
  41.   else{                                                //    Otherwise, do:
  42.     while(button == HIGH){                             // wait for button release
  43.       digitalWrite(4,LOW);                             // keep LED off while button pressed
  44.     }
  45.     i = 0;                                             // reset interger i
  46.     mill0 = millis();                                  // record time spent idled
  47.     mill1 = millis() - mill0;                          // time spent in driving loop
  48.     delay(1000);                                       // pause to let person get hand out of way
  49.     while(mill1 < 60000){                              //   While: time on < 1 minute
  50.       IR1 = digitalRead(6);                            // read all 3 IR sensors
  51.       IR2 = digitalRead(7);                            // "
  52.       IR3 = digitalRead(8);                            // "
  53.       while(i = 0){                                    // * initialization code: run straight until wall is detected
  54.         if(IR2 = 1){                                   // if: wall in front
  55.           digitalWrite(2,LOW);                         // turn on both motors
  56.           digitalWrite(3,LOW);                         // "
  57.           delay(500);                                  // wait 1/2 second for motors to stop
  58.           while(IR1 = 0){                              // while: wall is not detected by right IR, do:
  59.             digitalWrite(2,HIGH);                      // Turn Left
  60.           }
  61.           i++;                                         // end initialization
  62.         }
  63.         else if(IR1 = 1){                              // if: wall detected to side:
  64.           i++;                                         // end initialization
  65.         }
  66.         else if(IR3 = 1){                              // if: wall detected to side:
  67.           i++;                                         // end initialization
  68.         }
  69.         else{                                          // if: no wall
  70.           digitalWrite(2,HIGH);                        // turn on both motors
  71.           digitalWrite(3,HIGH);                        // "
  72.           delay(10);                                   // wait 1/100 second
  73.         }
  74.       }                                                // * Initialization code: end
  75.        
  76.       //robot avoidance code here//
  77.      
  78.       delay(100);                                      // let motors run for 1/10 second
  79.     mill1 = millis() - mill0;                          // time spent in driving loop
  80.     }
  81.   }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement