/ 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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//program substitutes LEDs for Motorstates for debugging purposes //Constants for up and down buttons: const int button1Pin = 2; const int button2Pin = 3; const int button3Pin = 4; const int button4Pin = 5; //Constants for mechnical stop switches: const int button5Pin = 6; const int button6Pin = 7; const int button7Pin = 8; const int button8Pin = 9; //Constants for LEDs const int led1Pin = 10; const int led2Pin = 11; const int led3Pin = 12; const int led4Pin = 13; //Variables for tracking button and motor states: int button1State = 0; int lastButton1State = 0; int button2State = 0; int lastButton2State = 0; int button3State = 0; int lastButton3State = 0; int button4State = 0; int lastButton4State = 0; int mech5Switch = 0; int mech6Switch = 0; int mech7Switch = 0; int mech8Switch = 0; int stepper1State = 0; int stepper2State = 0; void setup() { //intialize the button pins as inputs pinMode(button1Pin, INPUT); pinMode(button2Pin, INPUT); pinMode(button3Pin, INPUT); pinMode(button4Pin, INPUT); //intialize mechanical stop switches as inputs pinMode(button5Pin, INPUT); pinMode(button6Pin, INPUT); pinMode(button7Pin, INPUT); pinMode(button8Pin, INPUT); //intialize LEDs to indicate stepper state pinMode (led1Pin, OUTPUT); pinMode (led2Pin, OUTPUT); pinMode (led3Pin, OUTPUT); pinMode (led4Pin, OUTPUT); } void loop() { button1State = digitalRead (button1Pin); button2State = digitalRead (button2Pin); button3State = digitalRead (button3Pin); button4State = digitalRead (button4Pin); mech5Switch = digitalRead (button5pin); mech6Switch = digitalRead (button5pin); mech7Switch = digitalRead (button5pin); mech8Switch = digitalRead (button5pin); //check up & down switches to see if they are being pressed and set motor states accordingly: if (button1State != lastButton1State) { if (button1State == HIGH && stepper1State == 1 ) { stepper1State = 0; } else if (button1State == high) { stepper1State = 1; } } if (button2State != lastButton2State) { if (button2State == HIGH && stepper1State == 2 ) { stepper1State = 0; } else if (button2State == high) { stepper1State = 2; } } if (button3State != lastButton3State) { if (button3State == HIGH && stepper2State == 1 ) { stepper2State = 0; } else if (button3State == high) { stepper2State = 1; } } if (button4State != lastButton4State) { if (button4State == HIGH && stepper2State != 2 ) { stepper2State = 0; } else if (button4State == high) { stepper2State = 2; } } //check mechanical switches to make sure we aren't running off the track: if (mech5Switch == HIGH && stepper1State == 1) { stepper1State = 0; } if (mech6Switch == HIGH && stepper1State == 2) { stepper1State = 0; } if (mech7Switch == HIGH && stepper2State == 1) { stepper2State = 0; } if (mech8Switch == HIGH && stepper2State == 2) { stepper2State = 0; } //act on motor states, this will later be replaced with code to run the stepper motors: if (stepper1State == 1) { digitalWrite(led1Pin, HIGH) } else { digitalWrite(led1Pin, LOW) } if (stepper1State == 2) { digitalWrite(led2Pin, HIGH) } else { digitalWrite(led2Pin, LOW) } if (stepper2State == 1) { digitalWrite(led3Pin, HIGH) } else { digitalWrite(led3Pin, LOW) } if (stepper2State == 2) { digitalWrite(led4Pin, HIGH) } else { digitalWrite(led4Pin, LOW) } //set last button state lastButton1State = button1State lastButton2State = button2State lastButton3State = button3State lastButton4State = button4State }