manhoosbilli1

Failed motor code

Sep 23rd, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. //Pins
  2.  
  3. #define sw1 5
  4. #define sw2 7
  5. #define led1 13
  6. #define m1 10
  7. #define m2 11
  8.  
  9. //states
  10.  
  11. boolean sw1Status;
  12. boolean sw2Status;
  13. boolean prevState1;
  14. boolean prevState2;
  15.  
  16. //Flags
  17.  
  18. boolean blinkFlag = 1;
  19. bool sw1Flag;
  20. bool sw2Flag;
  21. bool m1Flag;
  22. bool m2Flag;
  23. bool motorStopFlag;
  24. int state;
  25.  
  26.  
  27. //Timing
  28.  
  29. unsigned long currentMillis;
  30. unsigned long previousMillis;
  31.  
  32.  
  33. void setup()
  34. {
  35. // put your setup code here, to run once:
  36.  
  37. pinMode(sw1, INPUT_PULLUP);
  38. pinMode(sw2, INPUT_PULLUP);
  39. pinMode(m1, OUTPUT);
  40. pinMode(m2, OUTPUT);
  41. pinMode(13, OUTPUT);
  42. Serial.begin(9600);
  43.  
  44. }
  45.  
  46. void loop()
  47. {
  48. int sw1Status = digitalRead(sw1);
  49. int sw2Status = digitalRead(sw2);
  50. if (sw1Status != sw2Status) {
  51. state = 0;
  52. }
  53. currentMillis = millis();
  54. motorLogic();
  55. }
  56.  
  57.  
  58.  
  59. void motorLogic()
  60. {
  61. switch (state)
  62. {
  63. case 0:
  64. //set flags and stop motor
  65. //logic is inverted
  66. if (sw1Status == LOW & sw2Status == HIGH)
  67. {
  68. sw1Flag = 1;
  69. sw2Flag = 0;
  70. m1Flag = 1;
  71. m2Flag = 0; // meaning it is currently pushing down on sw1 so m1 is high.
  72. motorStopFlag = 1;
  73.  
  74.  
  75. }
  76.  
  77. if (sw1Status == HIGH & sw2Status == LOW)
  78. {
  79. sw2Flag = 1;
  80. sw1Flag = 0;
  81. m2Flag = 1;
  82. m1Flag = 0;
  83. motorStopFlag = 1;
  84.  
  85. }
  86. state = 1;
  87. break;
  88.  
  89. case 1:
  90. if ((sw1Flag == 1) & (motorStopFlag == 1) || (sw2Flag == 1) & (motorStopFlag == 1))
  91. {
  92. motorStop();
  93. Serial.println("In case 1 motor stopped");
  94. delay(5000);
  95. Serial.println("Just finished delay");
  96.  
  97.  
  98. if (sw1Status == LOW & sw2Status == HIGH)
  99. {
  100. Serial.println("Chose state 2 from case 1 menu");
  101. state = 2; //if the left is switch is high then state = 2 to rotate it in the opposite direction
  102.  
  103. } else if (sw1Status == HIGH & sw2Status == LOW)
  104. {
  105. state = 3;
  106. Serial.println("Chose state 3 from case 1 menu");
  107. motorStopFlag = 0;
  108. }
  109.  
  110.  
  111.  
  112.  
  113. }
  114. break;
  115.  
  116. case 2:
  117.  
  118. //reverse and go to the opposite one.
  119. motorStopFlag = 0;
  120. Serial.println("Reversing direction in case 2");
  121. reverseDirection(); // it will keep going in the opposite direction
  122. if (sw2Status == LOW) //until the opposite switch is high
  123. {
  124. motorStop();
  125. Serial.print("Reached sw2 from sw1 in case 2 ");
  126. state = 0; //as it touches that. the state is transfered to 0
  127. }
  128. break;
  129.  
  130. case 3:
  131.  
  132. motorStopFlag = 0;
  133. Serial.println("in case 3 reversing direction ");
  134. reverseDirection(); // it will keep going in the opposite direction
  135. if (sw1Status == LOW) //until the opposite switch is high
  136. {
  137. motorStop();
  138. Serial.println("Just reached sw1 from sw2 in case 3");
  139. state = 0; //as it touches that. the state is transfered to 0
  140. }
  141. break;
  142. }
  143. }
  144.  
  145.  
  146. void reverseDirection()
  147. {
  148. digitalWrite(m1, !m1Flag);
  149. digitalWrite(m2, !m2Flag);
  150. Serial.print("Reversing direction");
  151.  
  152. }
  153.  
  154. void motorStop()
  155. {
  156. digitalWrite(m1, HIGH);
  157. digitalWrite(m2, HIGH);
  158. Serial.print("Motor stopped");
  159. }
Add Comment
Please, Sign In to add comment