manhoosbilli1

deboune and motor state machine

Aug 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1.  
  2. // Include the Bounce2 library found here :
  3. // https://github.com/thomasfredericks/Bounce2
  4. #include <Bounce2.h>
  5.  
  6.  
  7. //left switch
  8. #define BUTTON_PIN_1 6
  9.  
  10. //Right switch
  11. #define BUTTON_PIN_2 5
  12.  
  13.  
  14. #define LED_GREEN 12
  15.  
  16.  
  17. #define LED_PIN 13
  18.  
  19. //Time variables
  20. const int blinkDelay = 500;
  21. unsigned long previousBlink = 0;
  22. unsigned long currentMillis = 0;
  23.  
  24. //motor time variables
  25. unsigned long lastReverse =0;
  26. const int turnInterval = 10000;
  27.  
  28. unsigned long lastMotorDelay = 0;
  29. const int motorDelay = 20000;
  30.  
  31. //this is the delay that the motor will be stop for.
  32.  
  33. //Setting up motor pins
  34. const byte in1 = 4;
  35. const byte in2 = 3;
  36. int in1State =0;
  37. int in2State =0;
  38.  
  39. int value1 =0;
  40. int value2 =0;
  41.  
  42. //
  43. int state =0;
  44.  
  45. // Instantiate a Bounce object
  46. Bounce debouncer1 = Bounce();
  47.  
  48. // Instantiate another Bounce object
  49. Bounce debouncer2 = Bounce();
  50.  
  51. void setup() {
  52.  
  53. // Setup the the left switch :
  54. pinMode(BUTTON_PIN_1,INPUT);
  55.  
  56. //Motor variables
  57. pinMode(in1, OUTPUT);
  58. pinMode(in2, OUTPUT);
  59.  
  60. // After setting up the button, setup the Bounce instance :
  61. debouncer1.attach(BUTTON_PIN_1);
  62. debouncer1.interval(5); // interval in ms
  63.  
  64. // Setup the right switch :
  65. pinMode(BUTTON_PIN_2,INPUT);
  66.  
  67. // After setting up the button, setup the Bounce instance :
  68. debouncer2.attach(BUTTON_PIN_2);
  69. debouncer2.interval(5); // interval in ms
  70.  
  71.  
  72. //Setup the LED :
  73. pinMode(LED_PIN,OUTPUT);
  74. pinMode(LED_GREEN, OUTPUT);
  75.  
  76. // Update switch
  77. debouncer1.update();
  78. debouncer2.update();
  79. //Calibrate the motors at the start.
  80. initialCalibration();
  81. delay(5000);//add a delay here so that when it calibrates it stops for one hour and then move to loop
  82.  
  83. }
  84.  
  85. void loop() {
  86. // Update the Bounce instances :
  87. // i need to initialize all the timers here
  88. lastMotorDelay = 0;
  89. lastReverse = 0;
  90. currentMillis = millis();
  91. debouncer1.update();
  92. debouncer2.update();
  93. int value1 = debouncer1.read();
  94. int value2 = debouncer2.read();
  95. switch(state) {
  96.  
  97. case 0: //Motor Stop
  98. updateValue();
  99. if(value1 == HIGH && value2 == LOW) { //value 1 is left so we have to go right
  100. //making sure motor is stopped
  101. updateValue();
  102. stopMotor();
  103. state = 1; //jump to delay process
  104. }
  105. updateValue();
  106. if(value1 == LOW && value2 == HIGH) { //means the tray is to the left.
  107. updateValue();
  108. stopMotor();
  109. state = 1; //jump to delay process
  110. }
  111.  
  112.  
  113. break;
  114.  
  115. case 1:
  116. //This should wait 1 hour before starting motors. so that there is equal interval
  117. //creating a branch which leads to different reverse processes
  118. //intended to create a delay of 1 hour before it tilts the tray again.
  119.  
  120.  
  121. //as soon as the delay finishes it calculates the state to throw them in to
  122. updateValue();
  123. if(value1 == HIGH && value2 == LOW) {
  124. updateValue();
  125. //tray is tilted towards left and has to right
  126. state = 2;
  127. //go to the going right process
  128. }
  129. updateValue();
  130. //tray is tilted towards right and has to go left
  131. if(value1 == LOW && value2 == HIGH) {
  132. updateValue();
  133. state = 3; // go to the going left process
  134. }
  135. }
  136. break;
  137.  
  138. //Reverse process towards right
  139. case 2:
  140. //this simply turns the motors on
  141. // i believe it stops the motor but at a very fast rate and moves on to the next process
  142. //i think i need to introduce another delay before it reverses the motor which whould be equal to lastReverse
  143.  
  144. stopMotor();
  145. goRight();
  146. //Read the buttons again.
  147. updateValue();
  148. //will detect if it touches the right side
  149. if(value1 == LOW && value2 == HIGH)
  150. { state = 0; }
  151. //this creates a loop
  152. break;
  153.  
  154. // going to the left process
  155. case 3:
  156.  
  157. //when this while condition becomes true i.e 10 seconds have passed since the motor is stopped then it will on to next statement
  158.  
  159. // i want to introduce another delay of one hour here
  160. //basically whenever it towards either switches, it should stop and delay it.
  161. stopMotor();
  162. goLeft();
  163. updateValue();
  164. //IF IT TOUCHES RIGHT
  165. if(value1 == HIGH && value2 == LOW) {
  166. state = 0; }
  167. break;
  168. }
  169.  
  170.  
  171. // Get the updated value :
  172.  
  173.  
  174. }
  175. //Function to calibrate the tray as in (till it enough so that it touches one of the switche)
  176. //basically a function to tackle load-shedding. if electricity cuts in between the tilt motion. it should return to initial position
  177.  
  178. void initialCalibration () {
  179.  
  180. int value1 = debouncer1.read();
  181. int value2 = debouncer2.read();
  182.  
  183. if(value1 == LOW && value2 == LOW) {
  184. digitalWrite(in1, HIGH);
  185. digitalWrite(in2, LOW);
  186. } else {
  187. digitalWrite(in1, LOW);
  188. digitalWrite(in2, LOW);
  189. }
  190. //for now it goes through these commands very fast and does not stop.
  191. //i wish to introduce a delay whenever it touches the switch and the switch is high
  192. //as i have stated many times, the purpose of the machine and detailed functions are going to be explained in the email
  193.  
  194. }
  195.  
  196.  
  197. /* void mSt () { //THIS FUNCTION IS REPEATED IN LOOP.
  198. //State machine for buttons and reverse calculation
  199.  
  200. switch(state) {
  201.  
  202. case 0: //Motor Stop
  203. updateValue();
  204. if(value1 == HIGH && value2 == LOW) { //value 1 is left so we have to go right
  205. //making sure motor is stopped
  206. updateValue(); // i don't know if refreshing the button state is necessary
  207. stopMotor();
  208. state = 1; //jump to delay process
  209. }
  210. updateValue();
  211. if(value1 == LOW && value2 == HIGH) { //means the tray is to the left.
  212. updateValue();
  213. stopMotor();
  214. state = 1; //jump to delay process
  215. }
  216.  
  217.  
  218. break;
  219.  
  220. case 1:
  221. //creating a branch which leads to different reverse processes.
  222. //Delay here aswell.
  223.  
  224. //as soon as the delay finishes it calculates the state to throw them in to
  225. updateValue();
  226. if(value1 == HIGH && value2 == LOW) {
  227. updateValue();
  228. //tray is tilted towards left and has to right
  229. state = 2;
  230. //go to the going right process
  231. }
  232. updateValue();
  233. //tray is tilted towards right and has to go left
  234. if(value1 == LOW && value2 == HIGH) {
  235. updateValue();
  236.  
  237. state = 3; // go to the going left process
  238.  
  239. stopMotor(); //Make sure motors are stopped
  240. // a delay here of 1 hour
  241. goRight();
  242. updateValue();
  243. //will detect if it touches the right side
  244. if(value1 == LOW && value2 == HIGH)
  245. { state = 0; } //resets
  246.  
  247. break;
  248.  
  249. //Reverse process towards right
  250. case 2:
  251. //this simply turns the motors on
  252. stopMotor();
  253. while (currentMillis - lastMotorDelay <= motorDelay) {
  254. break;
  255. }
  256. //introduce delay here.
  257. goRight();
  258. //this should be there before the value is judged
  259. updateValue();
  260. //will detect if it touches the right side
  261. if(value1 == LOW && value2 == HIGH)
  262. { state = 0; }
  263. break;
  264.  
  265. // going to the left process
  266. case 3:
  267. goLeft();
  268. updateValue();
  269. //IF TOUCHES RIGHT
  270. if(value1 == HIGH && value2 == LOW) {
  271. state = 0; }
  272. break;
  273. }
  274. }
  275. */
  276.  
  277. void goLeft() { //figure out clockwise and anti clockwise rotation first and plug in the values
  278. digitalWrite(in1, HIGH);
  279. digitalWrite(in2, LOW);
  280. updateValue();
  281.  
  282. }
  283.  
  284. void goRight() { //tentative values. kindly check clw and aclw
  285. digitalWrite(in1, LOW);
  286. digitalWrite(in2, HIGH);
  287. updateValue();
  288.  
  289. }
  290.  
  291. void stopMotor() {
  292. // if (currentMillis - lastDelayed <= lastMotorDelay) { // this will keep it stopped for the delay period. and move on.
  293. digitalWrite(in1, LOW);
  294. digitalWrite(in2, LOW);
  295. updateValue();
  296. }
  297. // updateValue();
  298.  
  299.  
  300.  
  301. void updateValue() {
  302. int value1 = debouncer1.read();
  303. int value2 = debouncer2.read();
  304. }
Add Comment
Please, Sign In to add comment