I am following a YouTube project to control a model train turntable. The following code uses a SPDT switch and I want to use a pushbutton 'toggle' switch.
No matter what I try, I end up breaking out of the loop, and cannot get back to execute the program further.
Any suggestions would be welcome.
[code]
//Turntable stepper (ex Dutch) with toggle pushbutton to rotate
//define stepper pins
const int stepperPin1 = A1;
const int stepperPin2 = A2;
const int stepperPin3 = A3;
const int stepperPin4 = A4;
//define pushbutton and led pins
const int moveButPin = 9; //when pushed (LOW), (0), (ON), stepper turns. Pull up resistor inverts logic
const int redLedPin = 5; //led is HIGH, (ON), (1) when stepper is running
//define pushbutton states
int moveButState = HIGH;
int moveButStateNew;
int moveButStateOld = LOW;
int delayTime = 50;
unsigned long timeSinceLastToggle = 0;
unsigned long debounce = 20UL;
//define stepper constants
const float stepsPerMotorRevolution = 32; //for the 28BYJ stepper this is 32
const float gearboxReduction = 64; //the 28BYJ is fitted with a 64:1 reduction gearbox
const float stepsPerStepperRevolution = stepsPerMotorRevolution * gearboxReduction; //Stepper and gearbox specific, = 2048 for 28BYJ-48
//define stepper variable
int stepperRpm = 8; // max speed of stepper is 12 rpm, else pulses get lost
/if stepper turns at x Rpm, then time for one revolution is 60/x seconds, or 60 000 000/x micros (us)
and if each revolution is y stepsPerStepperRevolution, then each step takes 60 000 000/x/y us/
unsigned long timeOfEachStep;
unsigned long timeOfLastStep;
byte stepnr;
void doOneStep() {
digitalWrite(redLedPin, HIGH);
stepnr = (stepnr + 1) % 8; // counts and remembers in which of the 8 phases the stepper is
switch (stepnr) {
case 0:
digitalWrite(stepperPin1, HIGH);
digitalWrite(stepperPin2, LOW);
digitalWrite(stepperPin3, LOW);
digitalWrite(stepperPin4, LOW);
break;
case 1:
digitalWrite(stepperPin1, HIGH);
digitalWrite(stepperPin2, HIGH);
digitalWrite(stepperPin3, LOW);
digitalWrite(stepperPin4, LOW);
break;
case 2:
digitalWrite(stepperPin1, LOW);
digitalWrite(stepperPin2, HIGH);
digitalWrite(stepperPin3, LOW);
digitalWrite(stepperPin4, LOW);
break;
case 3:
digitalWrite(stepperPin1, LOW);
digitalWrite(stepperPin2, HIGH);
digitalWrite(stepperPin3, HIGH);
digitalWrite(stepperPin4, LOW);
break;
case 4:
digitalWrite(stepperPin1, LOW);
digitalWrite(stepperPin2, LOW);
digitalWrite(stepperPin3, HIGH);
digitalWrite(stepperPin4, LOW);
break;
case 5:
digitalWrite(stepperPin1, LOW);
digitalWrite(stepperPin2, LOW);
digitalWrite(stepperPin3, HIGH);
digitalWrite(stepperPin4, HIGH);
break;
case 6:
digitalWrite(stepperPin1, LOW);
digitalWrite(stepperPin2, LOW);
digitalWrite(stepperPin3, LOW);
digitalWrite(stepperPin4, HIGH);
break;
case 7:
digitalWrite(stepperPin1, HIGH);
digitalWrite(stepperPin2, LOW);
digitalWrite(stepperPin3, LOW);
digitalWrite(stepperPin4, HIGH);
break;
}
}
void stepperIdle() {
digitalWrite(stepperPin1, LOW);
digitalWrite(stepperPin2, LOW);
digitalWrite(stepperPin3, LOW);
digitalWrite(stepperPin4, LOW);
digitalWrite(redLedPin, LOW);
}
void setup() {
Serial.begin(9600);
pinMode(stepperPin1, OUTPUT);
pinMode(stepperPin2, OUTPUT);
pinMode(stepperPin3, OUTPUT);
pinMode(stepperPin4, OUTPUT);
pinMode(moveButPin, INPUT_PULLUP);
pinMode(redLedPin, OUTPUT);
digitalWrite(redLedPin, LOW);
timeOfEachStep = (60000000UL / stepperRpm) / stepsPerStepperRevolution; //approx 3000us
}
void loop() {
while (moveButState == LOW) {
if ((micros() - timeOfLastStep) > timeOfEachStep) { //micros is the number of milliseconds since the program started
timeOfLastStep = micros();
doOneStep();
}
}
stepperIdle();
}