manhoosbilli1

Working motor reversal code with a unsteady delay

Aug 27th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  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 = 3;
  7. const byte in2 = 4;
  8. const byte sw1 = 6; //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);
  19. pinMode(sw2, INPUT);
  20.  
  21.  
  22. }
  23.  
  24. void loop() {
  25. // put your main code here, to run repeatedly:
  26. currentMillis = millis();
  27. sw1Status = digitalRead(sw1);
  28. sw2Status = digitalRead(sw2);
  29.  
  30. switch (stateMachine) {
  31. case 0: //motor stop/ reset
  32. digitalWrite(in1, LOW);
  33. digitalWrite(in2, LOW);
  34. if(sw1Status == HIGH && sw2Status == LOW) //tray is tilted to the left
  35. {
  36. stateMachine = 1;
  37. } else if (sw1Status == LOW && sw2Status == HIGH) { //try is tilted to the right
  38. stateMachine = 2;
  39. }
  40. break; //erase the break see what happens
  41.  
  42. case 1: //go right
  43. if(sw2Status == LOW) { //go right until the right switch is high
  44. if(currentMillis - previousMillis >= onInterval) {
  45. digitalWrite(in1, HIGH);
  46. digitalWrite(in2, LOW);
  47. previousMillis = currentMillis;
  48. } else if (sw2Status == HIGH ) {
  49. stateMachine = 0; // go to reset
  50. }
  51. } else if (sw2Status == HIGH) {
  52. stateMachine = 0; // go to reset
  53. }
  54. break;
  55.  
  56. case 2: //go left
  57. if(sw1Status == LOW) { //go left until it touches left switch
  58. if(currentMillis - previousMillis >= onInterval) {
  59. digitalWrite(in1, LOW); //go right mechanism
  60. digitalWrite(in2, HIGH);
  61. previousMillis = currentMillis;
  62. } else if (sw1Status == HIGH) { //if it touches left switch
  63. stateMachine = 0; //go to reset
  64. }
  65. } else if (sw1Status == HIGH) {
  66. stateMachine = 0;
  67. }
  68. break;
  69.  
  70.  
  71. }
  72. }
Add Comment
Please, Sign In to add comment