Posted By

TheSplicer on 06/17/12


Tagged

arduino


Versions (?)

Arduino Debugger Code for Scripting Proper Button Behavior for Driving Stepper Motors


 / Published in: C++
 

This is debugger code used for scripting proper button behavior for driving stepper motors. In place code driving the stepper motors code has been put in place switching LED's on and off for debugging purposes. The buttons switch a stepperState variable which determines the direction the motor turns. Pressing a button once will cause the motor to turn in that direction, with a second press of the button stopping it. Code is also in place to stop the motor if a mechanical stopper switch is pressed.

  1. //program substitutes LEDs for Motorstates for debugging purposes
  2.  
  3. //Constants for up and down buttons:
  4. const int button1Pin = 2;
  5. const int button2Pin = 3;
  6. const int button3Pin = 4;
  7. const int button4Pin = 5;
  8.  
  9. //Constants for mechnical stop switches:
  10. const int button5Pin = 6;
  11. const int button6Pin = 7;
  12. const int button7Pin = 8;
  13. const int button8Pin = 9;
  14.  
  15. //Constants for LEDs
  16. const int led1Pin = 10;
  17. const int led2Pin = 11;
  18. const int led3Pin = 12;
  19. const int led4Pin = 13;
  20.  
  21. //Variables for tracking button and motor states:
  22. int button1State = 0;
  23. int lastButton1State = 0;
  24. int button2State = 0;
  25. int lastButton2State = 0;
  26. int button3State = 0;
  27. int lastButton3State = 0;
  28. int button4State = 0;
  29. int lastButton4State = 0;
  30. int mech5Switch = 0;
  31. int mech6Switch = 0;
  32. int mech7Switch = 0;
  33. int mech8Switch = 0;
  34.  
  35.  
  36. int stepper1State = 0;
  37. int stepper2State = 0;
  38.  
  39. void setup()
  40. {
  41. //intialize the button pins as inputs
  42. pinMode(button1Pin, INPUT);
  43. pinMode(button2Pin, INPUT);
  44. pinMode(button3Pin, INPUT);
  45. pinMode(button4Pin, INPUT);
  46.  
  47. //intialize mechanical stop switches as inputs
  48. pinMode(button5Pin, INPUT);
  49. pinMode(button6Pin, INPUT);
  50. pinMode(button7Pin, INPUT);
  51. pinMode(button8Pin, INPUT);
  52.  
  53. //intialize LEDs to indicate stepper state
  54. pinMode (led1Pin, OUTPUT);
  55. pinMode (led2Pin, OUTPUT);
  56. pinMode (led3Pin, OUTPUT);
  57. pinMode (led4Pin, OUTPUT);
  58.  
  59. }
  60.  
  61. void loop() {
  62. button1State = digitalRead (button1Pin);
  63. button2State = digitalRead (button2Pin);
  64. button3State = digitalRead (button3Pin);
  65. button4State = digitalRead (button4Pin);
  66. mech5Switch = digitalRead (button5pin);
  67. mech6Switch = digitalRead (button5pin);
  68. mech7Switch = digitalRead (button5pin);
  69. mech8Switch = digitalRead (button5pin);
  70.  
  71. //check up & down switches to see if they are being pressed and set motor states accordingly:
  72. if (button1State != lastButton1State) {
  73. if (button1State == HIGH && stepper1State == 1 ) { stepper1State = 0; }
  74. else if (button1State == high) { stepper1State = 1; } }
  75.  
  76. if (button2State != lastButton2State) {
  77. if (button2State == HIGH && stepper1State == 2 ) { stepper1State = 0; }
  78. else if (button2State == high) { stepper1State = 2; } }
  79.  
  80. if (button3State != lastButton3State) {
  81. if (button3State == HIGH && stepper2State == 1 ) { stepper2State = 0; }
  82. else if (button3State == high) { stepper2State = 1; } }
  83.  
  84. if (button4State != lastButton4State) {
  85. if (button4State == HIGH && stepper2State != 2 ) { stepper2State = 0; }
  86. else if (button4State == high) { stepper2State = 2; } }
  87.  
  88. //check mechanical switches to make sure we aren't running off the track:
  89. if (mech5Switch == HIGH && stepper1State == 1) { stepper1State = 0; }
  90. if (mech6Switch == HIGH && stepper1State == 2) { stepper1State = 0; }
  91. if (mech7Switch == HIGH && stepper2State == 1) { stepper2State = 0; }
  92. if (mech8Switch == HIGH && stepper2State == 2) { stepper2State = 0; }
  93.  
  94. //act on motor states, this will later be replaced with code to run the stepper motors:
  95. if (stepper1State == 1) { digitalWrite(led1Pin, HIGH) }
  96. else { digitalWrite(led1Pin, LOW) }
  97. if (stepper1State == 2) { digitalWrite(led2Pin, HIGH) }
  98. else { digitalWrite(led2Pin, LOW) }
  99. if (stepper2State == 1) { digitalWrite(led3Pin, HIGH) }
  100. else { digitalWrite(led3Pin, LOW) }
  101. if (stepper2State == 2) { digitalWrite(led4Pin, HIGH) }
  102. else { digitalWrite(led4Pin, LOW) }
  103.  
  104. //set last button state
  105. lastButton1State = button1State
  106. lastButton2State = button2State
  107. lastButton3State = button3State
  108. lastButton4State = button4State
  109. }

Report this snippet  

You need to login to post a comment.