Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*=================================*\
- |Blinking LED,spinning motor project|
- | By Matthew Russell |
- \*=================================*/
- int i = 0; // startinterger i
- int IR1 = 0; // right sensor
- int IR2 = 0; // front sensor
- int IR3 = 0; // left sensor
- int button = LOW; // define interger "button"
- int ledState = LOW; // ledState used to set the LED current
- long pMillis = 0; // will store last time LED was updated
- long blinkinterval = 250; // watchdog interval on
- long interval = 1000; // watchdog interval total
- long mill0 = 0; // time spent in idling loop
- long mill1 = 0; // time spent in driving loop
- unsigned long cMillis = 0; // define current time
- void setup(){ // ::SETUP::
- pinMode(2,OUTPUT); // motor 1 output :right:
- pinMode(3,OUTPUT); // motor 2 output :left:
- pinMode(4,OUTPUT); // LED output
- pinMode(5,INPUT); // Switch input
- pinMode(6,INPUT); // IR 1 input :right:
- pinMode(7,INPUT); // IR 2 input :front:
- pinMode(8,INPUT); // IR 3 input :left:
- }
- void loop(){ // ::LOOP::
- button = digitalRead(5); // read pin5, write to interger "button"
- if(button == LOW){ // Before button push, do:
- cMillis = millis(); // read time (in millis) since startup (unsigned ensures positive int)
- if(cMillis - pMillis < blinkinterval){ // if change in time is less than defined blinkinterval, do:
- digitalWrite(4,HIGH); // turn on LED
- }
- else if(cMillis - pMillis < interval){ // if blinkinterval < change in time < interval, do:
- digitalWrite(4,LOW); // turn off LED
- }
- else{ // if change in time > interval, reset, and save time:
- pMillis = cMillis; // save the last LED current change
- }
- }
- else{ // Otherwise, do:
- while(button == HIGH){ // wait for button release
- digitalWrite(4,LOW); // keep LED off while button pressed
- }
- i = 0; // reset interger i
- mill0 = millis(); // record time spent idled
- mill1 = millis() - mill0; // time spent in driving loop
- delay(1000); // pause to let person get hand out of way
- while(mill1 < 60000){ // While: time on < 1 minute
- IR1 = digitalRead(6); // read all 3 IR sensors
- IR2 = digitalRead(7); // "
- IR3 = digitalRead(8); // "
- while(i = 0){ // * initialization code: run straight until wall is detected
- if(IR2 = 1){ // if: wall in front
- digitalWrite(2,LOW); // turn on both motors
- digitalWrite(3,LOW); // "
- delay(500); // wait 1/2 second for motors to stop
- while(IR1 = 0){ // while: wall is not detected by right IR, do:
- digitalWrite(2,HIGH); // Turn Left
- }
- i++; // end initialization
- }
- else if(IR1 = 1){ // if: wall detected to side:
- i++; // end initialization
- }
- else if(IR3 = 1){ // if: wall detected to side:
- i++; // end initialization
- }
- else{ // if: no wall
- digitalWrite(2,HIGH); // turn on both motors
- digitalWrite(3,HIGH); // "
- delay(10); // wait 1/100 second
- }
- } // * Initialization code: end
- //robot avoidance code here//
- delay(100); // let motors run for 1/10 second
- mill1 = millis() - mill0; // time spent in driving loop
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement