manhoosbilli1

Tilt motion using switch cases

Aug 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. this code asssume that i would have already calibrated the motor to a tilt position meaning one button is going to be high while the other is going to be LOW which i can. no problem
  2. this code also assumes that both the switches are debounced
  3.  
  4.  
  5.  
  6.  
  7. ---------------leftbuttonstatemachine-------------------
  8.  
  9. switch (leftButtonState) { //when one leftsw goes high
  10. case 0: // stop/reset state
  11. readSw(); //this will contain debounce mechanism stop the values of motor and
  12. stopMotor(); //will read the sw state stop the motor and jump to next delay
  13. case = 1;
  14. break;
  15. }
  16.  
  17. case 1: //Delay/timer state
  18. //readSw(); // this will read switch and only start the function if LeFt switch is HIGH. i dont know it //would need to read it. infact i think this function is not necessary since we already confirmed that the left switch is already high
  19. //get the same
  20. if(currentMillis - lastReversed >= 1000*60*60){ //check if delay expires which is 1 hour
  21. case = 2; //once the delay expires move on to the next case
  22. }
  23. break;
  24.  
  25. case 2; //reverseDirection{
  26. if(rightButtonState == LOW) { //this will execute the following statements in the rightbutton state is LOW as soon as it becomes high
  27. //it should stop
  28. digitalWrite(in1, HIGH);
  29. digitalWrite(in1, LOW); //how do i tell the difference between the first HIGH when the motor started
  30. //and the next high when the motor is to be stoped. this is assuming i connect both the switches to one pin
  31. //ok lets just split it for now and see what happens
  32. }
  33. else
  34. stopMotor(); //once the other switch is HIGH it will stop the motor and return to case 0 where it will read
  35. case = 0; //i would like the switch case to stop here. even if it jumps back to case 0 it should not activate as the button is
  36. //not high anymore
  37.  
  38.  
  39.  
  40.  
  41. ------------------------RIGHTBUTTON machine state--------------------------
  42.  
  43. //this state will contain its own rightbutton debounce state
  44. switch (rightButtonState) { //when one leftsw goes high
  45. case 0: // stop/reset state
  46. //readSw(); //this will contain if statement and proceed only if right button is pressed and
  47. stopMotor(); //also contain debounce mechanism
  48. case = 1;
  49. break;
  50.  
  51.  
  52. case 1: //Delay/timer state
  53. //readSw(); // this will read switch and only start the function if the delay has
  54. if(currentMillis - lastReversed >= 1000*60*60){ //check if delay expires
  55. case = 2; //if the time is not yet equal to interval the if will fale and case will not be jumped
  56. //when the interval expires case will jumped to delay
  57. }
  58. }
  59. break;
  60.  
  61. case 2; //reverseDirection{
  62. if(leftButtonState == LOW) { //will keep the motor running as long at the other switch is low as soon as it becomes high the function
  63. //is going to stop
  64. digitalWrite(in1, LOW); // will wait for the other button to go high once it goes high motor stops
  65. digitalWrite(in1, HIGH); //how do i tell the difference between the first HIGH when the motor started
  66. } //and the next high when the motor is to be stoped. this is assuming i connect both the switches to one pin
  67. //ok lets just split it for now and see what happens
  68. else {
  69. stopMotor(); //once the other switch is HIGH it will stop the motor and return to case 0 where it will read
  70. case = 0;
  71. //that the left button is not pressed and i will make another switch case once the right one is on.
  72. }
Add Comment
Please, Sign In to add comment