Button action release while still holding it pressed

Hi I have a project in which I have a toggle button that reverses the stepper motor direction but it only changes the direction when released! I would like the opposite to change direction on the press of the button not the release!

here is my code:_

const int stepPin = 3;
 int dirPin = 4; 
 int dirButton = 2;
int state = HIGH;
int reading;
int previous = LOW;
int stepDelay=500;

#define bt_S A2 // Stop Button
#define enPin   10 //10Pin of Arduino--Enabled of stepper motor driver  
#define bt_Sp A1 // ms1
#define ms1Pin   11 //10Pin of Arduino—ms1 high or low select 

int Mode=1, flag=0;

long time = 0;
long debounce = 500;

int customDelay,customDelayMapped; // Defines variables
 
void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
 pinMode(dirButton, INPUT_PULLUP);

pinMode(bt_S, INPUT_PULLUP); // declare bt_S as input
pinMode(enPin,   OUTPUT); // declare as output for Enabled of stepper motor driver 
pinMode(bt_Sp, INPUT_PULLUP); // declare bt_S as input
pinMode(ms1Pin,   OUTPUT); // declare as output for Enabled of stepper motor driver 

  digitalWrite(dirPin,HIGH); //change the rotation direction HIGH for clockwise and LOW for anticlockwise
}
void loop() {
if(digitalRead (bt_Sp) == 0){ //For ms1
if(flag==0){flag=1;
 if(Mode>1)Mode=1; 
      else{Mode=!Mode; 
      if(Mode==0)digitalWrite(ms1Pin, HIGH);
            else digitalWrite(ms1Pin, LOW);
      }
delay(200);
 }
}else{flag=0;} 


   reading = digitalRead(dirButton);
if(digitalRead (bt_S) == 0){ //For Stop
if(flag==0){flag=1;
 if(Mode>1)Mode=1; 
      else{Mode=!Mode; 
      if(Mode==0)digitalWrite(enPin, HIGH);
            else digitalWrite(enPin, LOW);
      }
delay(200);
 }
}else{flag=0;}

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }



  digitalWrite(dirPin, state);

  previous = reading;

digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
  
  customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
  // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
  int customDelay = analogRead(A0); // Reads the potentiometer
  int newCustom = map(customDelay, 0, 1023, 100,15000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
  return newCustom;  
}

Thanks

You didn't include a schematic showing how your button was wired up, so this is just a guess. When you press your button, does it complete a circuit to ground? If so, your input is LOW when the button is pressed, not HIGH.

Try changing

if (reading == HIGH && previous == LOW && millis() - time > debounce) {

to

if (reading == LOW && previous == HIGH && millis() - time > debounce) {

You'll probably also want to change the initial setting of previous to HIGH.

But like I said, since you didn't include a schematic, this is all just a guess.

Thank you so much that worked perfect! is there a way I could do the same for the stop button as well!

Just invert the logic level there as well, I imagine.

Ive tried swopping the 0's and 1's and the LOW AND HIGH but without success not sure what I need to do with the FLAGS etc here is the code section :-1:

  reading = digitalRead(dirButton);
if(digitalRead (bt_S) == 0){ //For Stop
if(flag==0){flag=1;
 if(Mode>1)Mode=1; 
      else{Mode=!Mode; 
      if(Mode==0)digitalWrite(enPin, HIGH);
            else digitalWrite(enPin, LOW);

Hi @sophiekatie123

you should use ctrl-T to autoformat your code.

you should make it a habit to always post your complete actual sketch in a new posting.

In 80% of all cases the hwole sketch is needed to anaylse a problem
So just post the complete sketch each time (=100% of all cases)

with posting code-snippets you load additonal work on the shoulder of the users beacuse they have to do guessing what would make the comlete code

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.