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);
}
}
}
4 posts - 2 participants