# Mystery Latching, Speed and Direction Control with Pulldown Switching

Well a mystery to me. This is a realistically simple exercise intended to drive a step motor at a constant speed except when one of two momentary switches are pressed.

All works as expected, except that frequently when the direction is desired to be changed by selecting the alternate switch, the motor speeds as desired, but drives in the direction last used rather than the desired direction.

``````#include <Stepper.h>
const byte STEPS = 48;
const byte button1 = 3; //west correction
const byte button2 = 4;  //east correction
boolean buttonState = 0;
boolean button1State = 1;
boolean button2State = 1;
Stepper myStepper(STEPS, 10, 11, 12, 13);

void setup() {
pinMode(button1,INPUT_PULLUP);
pinMode(button2,INPUT_PULLUP);
myStepper.setSpeed(50);
Serial.begin(9600);
}

void Forward() { //constant speed
myStepper.setSpeed(50);
myStepper.step(1);
}

void Reverse() {  //east 8X - 15 arcsec/second
myStepper.setSpeed(400);
myStepper.step(-1);
}
void ForwardF() { //west 8X + 15 arcsec/second
myStepper.setSpeed(400);
myStepper.step(1);
}

void loop() {
buttonState = 0;

if (button1State == 0 && buttonState == 0) {

/*
buttonState should always return 0: button1State or button2State should only return 0 when closed (pressed)
/*

ForwardF();
}

else if
(button2State == 0 && buttonState == 0) {
Reverse();
}
else{
Forward();
}
}
``````

This is the circuit. It consists of a ProMini (Atmega168, 5V 16MHz), L298P driver and 48 step - step motor wired for bipolar operation.

OP’s image…

Urf! Even worse than the typical F**ing image. You know that website has a “schematic” page? Much more readable.

If you have followed that layout exactly then it is likely that the switches are wired incorrectly. They are very tricky those square switches. Just wire across the diagonal and don’t try to go “through” a switch like your diagram shows.

Thanks for all your help. Didn't know you were an art critic and no I don't think a schematic will be clear to everyone. As to the switches you are incorrect. Perhaps you'd like to the review the datasheet before displaying your ignorance in the future.

buttonState should always return 0:

So you could replace:

``````if (button1State == 0 && buttonState == 0)
``````

With:

``````if (button1State == 0 && 0 == 0)
``````

That would save you typing 11 characters.

Yes I am an art critic and that is a colourful piece of crap.

Daisy-chaining ground like that will always cause problems, no matter what the datasheet says. Particularly with those square switches that aren't immediately obvious which way around they are.

outsider:
So you could replace:

``````if (button1State == 0 && buttonState == 0)
``````

With:

``````if (button1State == 0 && 0 == 0)
``````

That would save you typing 11 characters.

My first post with a question and I get abuse. I didn't realize hazing was part of the entry process. OK, your point is that an empty state is well, empty and the conditional could just be:

``````if (button1State == 0)
``````

But only harm is, as you say cruelty to some characters and memory.

MorganS:
Yes I am an art critic and that is a colourful piece of crap.

Daisy-chaining ground like that will always cause problems, no matter what the datasheet says. Particularly with those square switches that aren't immediately obvious which way around they are.

If you think suggesting code cleanup and simplification and is abuse, how can we possibly help?