manhoosbilli1

Cleaner/Non working code for incubator motion

Aug 23rd, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <Bounce2.h>
  2. //left switch
  3. #define BUTTON_PIN_1 6
  4. //right switch
  5. #define BUTTON_PIN_2 5
  6. #define LED_GREEN 12
  7. #define LED_PIN 13
  8. unsigned long currentMillis = 0;
  9. unsigned long lastReverse =0;
  10. const int turnInterval = 10000;
  11. const byte in1 = 4;
  12. const byte in2 = 3;
  13. int in1State =0;
  14. int in2State =0;
  15. int value1 =0;
  16. int value2 =0;
  17. int state =0;
  18.  
  19. // Instantiate a Bounce object
  20. Bounce debouncer1 = Bounce();
  21.  
  22. // Instantiate another Bounce object
  23. Bounce debouncer2 = Bounce();
  24.  
  25. void setup() {
  26. pinMode(BUTTON_PIN_1,INPUT);
  27. pinMode(in1, OUTPUT);
  28. pinMode(in2, OUTPUT);
  29. debouncer1.attach(BUTTON_PIN_1);
  30. debouncer1.interval(5); // interval in ms
  31. pinMode(BUTTON_PIN_2,INPUT);
  32. debouncer2.attach(BUTTON_PIN_2);
  33. debouncer2.interval(5); // interval in ms
  34. pinMode(LED_PIN,OUTPUT);
  35. pinMode(LED_GREEN, OUTPUT);
  36. debouncer1.update();
  37. debouncer2.update();
  38. //initialCalibration();
  39.  
  40. }
  41.  
  42. void loop() {
  43. // put your main code here, to run repeatedly:
  44. //add machine state for motor
  45. //initial state should be low and motor should be triggered in an if statement
  46. currentMillis = millis();
  47. debouncer1.update();
  48. debouncer2.update();
  49. updateValue();
  50. switch (state) {
  51. updateValue();
  52. case 0: //Motor stop and delay
  53. stopMotor();
  54. updateValue();
  55. if(value1 == HIGH && value2 == LOW) { //motor is towards left
  56. stopMotor();
  57. if(currentMillis - lastReverse >= turnInterval) { //wait for the delay to expire and send to processes
  58. state = 1;
  59. lastReverse = currentMillis; }
  60. else {
  61. state = 0; }
  62. }
  63. updateValue();
  64. if(value1 == LOW && value2 == HIGH) { //motor is towards right
  65. stopMotor();
  66. if(currentMillis - lastReverse >= turnInterval ) {
  67. //when the delay is expired, branch off
  68. state = 2;
  69. lastReverse = currentMillis;
  70. } else {
  71. state = 0; }
  72. }
  73. else {
  74. state = 0;
  75. }
  76. break;
  77.  
  78. case 1:
  79. //go right
  80. updateValue();
  81. if(value2 == LOW) {
  82. //keep going right until the right switch is pressed and jump back to delay state
  83. goRight(); }
  84. else {
  85. state = 0; } //go back to delay state
  86. break;
  87.  
  88. case 2:
  89. //go left
  90. updateValue();
  91. if(value1 == LOW) {
  92. goLeft(); }
  93. else {
  94. state = 0; }
  95. break;
  96. }
  97. }
  98.  
  99. //functions //
  100.  
  101. void stopMotor() {
  102. digitalWrite(in1, LOW);
  103. digitalWrite(in2, LOW);
  104.  
  105. }
  106.  
  107. void goLeft() {
  108. digitalWrite(in1, HIGH);
  109. digitalWrite(in2, LOW);
  110. }
  111.  
  112. void goRight() {
  113. digitalWrite(in1, LOW);
  114. digitalWrite(in2, HIGH);
  115. }
  116.  
  117. void updateValue () {
  118. int value1 = debouncer1.read();
  119. int value2 = debouncer2.read();
  120. }
Add Comment
Please, Sign In to add comment