manhoosbilli1

motor reverse code working

Sep 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unsigned long currentMillis;
  2. unsigned long previousMillis;
  3. unsigned long previous2Millis;
  4. const long onInterval = 10000;
  5. const long offInterval = 20000;
  6. const byte in1 = 10;
  7. const byte in2 = 11;
  8. const byte sw1 = 7;  //this is left switch
  9. const byte sw2 = 5;  //this is right switch
  10. boolean sw1Status;
  11. boolean sw2Status;
  12. int stateMachine;
  13.  
  14. void setup() {
  15.   // put your setup code here, to run once:
  16.   pinMode(in1, OUTPUT);
  17.   pinMode(in2, OUTPUT);
  18.   pinMode(sw1, INPUT_PULLUP);
  19.   pinMode(sw2, INPUT_PULLUP);
  20.   Serial.begin(9600);
  21.  
  22.  
  23. }
  24.  
  25. void loop() {
  26.  
  27.   currentMillis = millis();
  28.   sw1Status = digitalRead(sw1);
  29.   sw2Status = digitalRead(sw2);
  30.   switch (stateMachine) {
  31.     case 0:
  32.     digitalWrite(in1, LOW);
  33.     digitalWrite(in2, LOW);
  34.     if(sw1Status == HIGH && sw2Status == LOW)
  35.     {
  36.       Serial.println("Just chose case 2 from case 0");
  37.       delay(5000);              //waiting before going to the next command
  38.       stateMachine = 2;
  39.     } else if (sw1Status == LOW && sw2Status == HIGH) {  
  40.       delay(5000);
  41.       stateMachine = 1;  
  42.     }
  43.     break;
  44.  
  45.     case 1:
  46.     if(sw2Status == HIGH) {  
  47.         digitalWrite(in1, HIGH);
  48.         digitalWrite(in2, LOW);
  49.     } else if (sw2Status == LOW) {  
  50.       stateMachine = 0;  // go to reset
  51.     }
  52.     break;
  53.  
  54.     case 2:  
  55.     if(sw1Status == HIGH) {
  56.       digitalWrite(in1, LOW);  
  57.       digitalWrite(in2, HIGH);
  58.     } else if (sw1Status == LOW) {    
  59.       stateMachine = 0;
  60.     }
  61.     break;
  62.  
  63.    
  64.   }
  65.   }
Add Comment
Please, Sign In to add comment