Putting it in reverse

Hi, so I have an application that requires: Motor runs for one revolution then pauses for 2 seconds then runs again on loop. UNTIL a button is pressed. If the button is pressed, the motor reverses…and ideally runs continuously at a slower rate until the button is released. Can be that the button latches…actually that would be a good thing, as the motor will feed something that could be best guided with both hands. This is the code I have currently…but it’s not reversing:

const int buttonPin = 8;  //This is the buttons read pin
const int dirPin = 2; // Direction
const int stepPin = 3; // Step
const int MS1Pin = 4; //Halfstep
const int ledPin = 9;
const int STEPS_PER_REV = 250;

int direction = HIGH;      //Variable for current LED state
int buttonState;          //Variable for current button reading
int lastButtonState = LOW;//Variable for last know button reading

unsigned long lastDebounceTime = 0; //last time the pin was toggled, used to keep track of time
unsigned long debounceDelay = 50;   //the debounce time which user sets prior to run

void setup() {

  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  pinMode(MS1Pin,OUTPUT);
  pinMode(ledPin,OUTPUT);
  pinMode(buttonPin,INPUT_PULLUP);
}

void loop() {

  //read the button pin, if pressed will be high, if not pressed will be low
  int reading = digitalRead(buttonPin);

  //in the case that the reading is not equal to the last state set the last debounce time to current millis time

  if (reading != lastButtonState)  {
    lastDebounceTime = millis();
  }

  //check the difference between current time and last registered button press time, if it's greater than user defined delay then change the LED state as it's not a bounce
  if ((millis()-lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
    if (buttonState == HIGH) {
      direction = !direction;
      }
    }
  }


  // Set motor direction clockwise

  if (direction) digitalWrite(dirPin,HIGH);
  else digitalWrite(dirPin, LOW);


  // Spin motor an amount related to step per rev
  for(int x = 0; x < STEPS_PER_REV; x++) {
  digitalWrite(stepPin,HIGH);
  digitalWrite(ledPin,HIGH);
  delayMicroseconds(1000);
  digitalWrite(stepPin,LOW);
  digitalWrite(ledPin,LOW);
  delayMicroseconds(1000);
  }

  delay(2000);

  lastButtonState = reading;
}
3 Likes

At the end of the debounce logic you have a stable reading from the button - HIGH or LOW. At that point you reverse direction if it’s pressed. That doesn’t make sense to me. It seems from your description that when you get a stable button reading you need to check whether or not that state is different than the prior stable button state.

So as soon as you have a stable button reading you would check what the current direction is, and if it doesn’t match what the stable button indicates it ought to be, change the direction and execute the appropriate code for that new direction.

4 Likes

what i currently have without the switch related code…is a motor that runs in one direction and then the other. I just need to work out what the right code for the switch is to make the reverse contingent on the switch being pressed. Not sure what you mean by current direction or stable button. Way past my level I’m afraid!

3 Likes

The direction variable tracks the current rotation of the motor - if it is HIGH then the motor is rotating clockwise, and if it is LOW then the motor is running counter-clockwise. The direction is controlled by the button. Your code includes a routine to ensure that the button press is debounced, so that it provides a stable HIGH or LOW and does not rapidly fluctuate as it opens or closes. The state of the button is recorded in reading. If, however, that value is stable for at least debounceDelay milliseconds then the state of the button is stored in buttonState. That’s the stable button value used to make the decision about what to do with the motor.

So, instead of simply reversing the motor when buttonState indicates that the button is pressed, which is what you now have, you need to test the value of buttonState. If it is LOW (ie, not pressed) then check what the direction is. If the direction is clockwise, then nothing needs to be done. If it is counter-clockwise, then you need to execute the code that moves the motor clockwise. On the other hand, if the button state is HIGH (ie, pressed) then you also need to check the direction, and if it is counter-clockwise then nothing needs to be done, but if it is clockwise then you need to execute the code that puts it into counter-clockwise rotation.

Note that this description is based on the comments in the code - things such as HIGH ad LOW depend on how you have wired the circuit.

4 Likes

OK thanks so much.
I must say that I feel as though you’re talking a foreign language at this point…
But I’ll sit there later and go through it and see if I can make sense of it.
Trouble is I’ve cobbled this up from a couple of places rather than built it…and I have so little time to make it work that I’ve not had time to play.
Thanks for your time again
Chris

3 Likes