If statement not working with external button

Hi Group, I am having trouble executing my code in my if statement for when my button is activated (buttonPin == LOW). I am reading the button is pushed in my serial port and I can see when I press the button the value changes to 0 or LOW but my if statement will not execute for some reason. Please help.

#define buttonPin 9
#define stepPin 3
#define dirPin 4
#define LimitPin 8
#define LedStatusPin LED_BUILTIN

const int PulsesPerRevolution = 3200;
const int feedTimer = 10000;
byte feedCount = 1;
unsigned long currentMillis;
unsigned long timerMillis;

void setup() {
  
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(LimitPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(LedStatusPin, OUTPUT);
 
  delay(2000); //delay after first plugged in to give operator a chance to acknowledge led light flash to confirm boot up
  Serial.begin(9600);
 
  //show user board has booted up by blinking LED twice
  for(int a = 0; a < 2; a++) {
      digitalWrite(LedStatusPin, HIGH);
      delay(1000);
      digitalWrite(LedStatusPin, LOW);
      delay(1000);
  }
}
 
void loop() {
currentMillis = millis();
if ((currentMillis-timerMillis >= feedTimer) && (feedCount <= 3)){
    for (int i = 0; i < 3*PulsesPerRevolution; i++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(50);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(50); 
    }
    timerMillis = currentMillis;
    feedCount ++; 
}

Serial.println(digitalRead(buttonPin)); //diagnostic only in my code so I can see what the status is of buttonPin

//this is the start of auto return to home if button is pressed
if (buttonPin==LOW){
 Serial.println("button pushed to spin ccw");
    //spin CCW until limit switch is activated
    while(digitalRead(LimitPin)==LOW){
        digitalWrite(dirPin, HIGH);
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(1000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(1000); 

    }
    Serial.println("limit switch active");
    //Change direction of Motor to CW
    
    //Spin CW until limit switch is deactivated
    while(digitalRead(LimitPin)==HIGH){
        digitalWrite(dirPin, LOW);
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(2000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(2000);
    }
     Serial.println("limit switch not active");

    //Go to home position after limit switch is released
    for(int x=0; x < PulsesPerRevolution; x++) {
        digitalWrite(dirPin, LOW);
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(1000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(1000);  
    }
} 
}

If your button has activated, this change the state of buttonPin, but not the buttonPin value.
The state of buttonPin you can check by

if(digitalRead(buttonPin)==LOW){

not
if (buttonPin == LOW)

Do you see the difference?

yes I do see the difference and that was my mistake on the code, thanks for the feedback. However now when I update if statement to the actual reading of the pin, it is reading high but starting my if statement if that makes sense. my serial port is staying value is HIGH but then my if statement starts waiting for my limit switch to get pressed.

#define buttonPin 9
#define stepPin 3
#define dirPin 4
#define LimitPin 8
#define LedStatusPin LED_BUILTIN

const int PulsesPerRevolution = 3200;
const int feedTimer = 10000;
byte feedCount = 1;
unsigned long currentMillis;
unsigned long timerMillis;

void setup() {
  
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(LimitPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(LedStatusPin, OUTPUT);
 
  delay(2000); //delay after first plugged in to give operator a chance to acknowledge led light flash to confirm boot up
  Serial.begin(9600);
 
  //show user board has booted up by blinking LED twice
  for(int a = 0; a < 2; a++) {
      digitalWrite(LedStatusPin, HIGH);
      delay(1000);
      digitalWrite(LedStatusPin, LOW);
      delay(1000);
  }
}
 
void loop() {
currentMillis = millis();
if ((currentMillis-timerMillis >= feedTimer) && (feedCount <= 3)){
    for (int i = 0; i < 3*PulsesPerRevolution; i++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(50);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(50); 
    }
    timerMillis = currentMillis;
    feedCount ++; 
}

Serial.println(digitalRead(buttonPin)); //diagnostic only in my code so I can see what the status is of buttonPin

//this is the start of auto return to home if button is pressed
if (digitalRead(buttonPin==LOW)){
 Serial.println("button pushed to spin ccw");
    //spin CCW until limit switch is activated
    while(digitalRead(LimitPin)==LOW){
        digitalWrite(dirPin, HIGH);
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(1000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(1000); 

    }
    Serial.println("limit switch active");
    //Change direction of Motor to CW
    
    //Spin CW until limit switch is deactivated
    while(digitalRead(LimitPin)==HIGH){
        digitalWrite(dirPin, LOW);
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(2000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(2000);
    }
     Serial.println("limit switch not active");

    //Go to home position after limit switch is released
    for(int x=0; x < PulsesPerRevolution; x++) {
        digitalWrite(dirPin, LOW);
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(1000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(1000);  
    }
} 
}

It almost appears if I have a floating value however that shouldn't be the case with my pinMode for buttonPin. I have ground on one side of my button and the other side is attached to pin 9 on the Uno. I also verified this with my multimeter, have 5 volts on pin 9 when button is not pressed and goes down to about zero when I push the button.

Nevermind, I had a parenthesis in the wrong spot on my code. It works perfectly fine now. Thanks again for the help!

For those wanting to see what my final code was for reference see below.

#define buttonPin 9
#define stepPin 3
#define dirPin 4
#define LimitPin 8
#define LedStatusPin LED_BUILTIN

const int PulsesPerRevolution = 3200;
const int feedTimer = 10000;
byte feedCount = 1;
unsigned long currentMillis;
unsigned long timerMillis;

void setup() {
  
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(LimitPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(LedStatusPin, OUTPUT);
 
  delay(2000); //delay after first plugged in to give operator a chance to acknowledge led light flash to confirm boot up
  Serial.begin(9600);
 
  //show user board has booted up by blinking LED twice
  for(int a = 0; a < 2; a++) {
      digitalWrite(LedStatusPin, HIGH);
      delay(1000);
      digitalWrite(LedStatusPin, LOW);
      delay(1000);
  }
}
 
void loop() {
currentMillis = millis();
if ((currentMillis-timerMillis >= feedTimer) && (feedCount <= 3)){
    for (int i = 0; i < 3*PulsesPerRevolution; i++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(50);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(50); 
    }
    timerMillis = currentMillis;
    feedCount ++; 
}

Serial.println(digitalRead(buttonPin)); //diagnostic only in my code so I can see what the status is of buttonPin

//this is the start of auto return to home if button is pressed
if (digitalRead(buttonPin)==LOW){
 Serial.println("button pushed to spin ccw");
    //spin CCW until limit switch is activated
    while(digitalRead(LimitPin)==LOW){
        digitalWrite(dirPin, HIGH);
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(1000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(1000); 

    }
    Serial.println("limit switch active");
    //Change direction of Motor to CW
    
    //Spin CW until limit switch is deactivated
    while(digitalRead(LimitPin)==HIGH){
        digitalWrite(dirPin, LOW);
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(2000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(2000);
    }
     Serial.println("limit switch not active");

    //Go to home position after limit switch is released
    for(int x=0; x < PulsesPerRevolution; x++) {
        digitalWrite(dirPin, LOW);
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(1000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(1000);  
    }
} 
}

I’ll lift out 2 lines that may explain what your processor is doing:

The compiler will replace buttonPin with 9 and LOW is translated to 0, so the comparison will never be equal.

You still use your if condition incorrectly:

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