Hello! I am trying to add a second limit switch (limitPinB) that stops a motor when it is pressed. I thought that this would be best accomplished with a while loop, but when I added the second while loop for limitPinB, I started getting this error message on digitalWrite(ledPin, LOW); delay(125); line. Prior to inserting this second while loop, the code went through without an error (with only the limitPinA while loop), I am not sure why adding the while loop for limitPinB would cause this error or what I should do to correct it. Any advice would be greatly appreciated.
The error message and the afflicted line is more expressly noted in the attached image.
const int buttonPin = 3; // the number of the pushbutton pin is 3
const int capmotor = 2; // the number of the cap motor is 2S
const int pourmotor = 1; // the number to turn the pour motor on and off is 1
const int ledPin = 5; // the number of the watchdog LED is 5
const int motordir = 6; //the pin to change the pourmotor direction is 6
const int limitPinA = 4; //the pin sensing the cup limitswitch value is 7
const int limitPinB = 7; //the pin sensing the bottle limit switchvalue is 7
int buttonState = 0; // variable for reading the pushbutton starts at zero status
int limitState = 0; //variable for reading the limit switch starts at zero status
int burrito = 125;
void setup() {
pinMode(pourmotor,OUTPUT); //intialize the pourmotor as an output
pinMode(capmotor, OUTPUT); //intalize the capmotor as an output
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
pinMode(ledPin,OUTPUT); //intalizes the LED pin as an output
pinMode(limitPinA, INPUT); //intilizes the cup limit switch as an input
pinMode(motordir, OUTPUT); //intilizes the motor direction pin as output
pinMode(limitPinB, INPUT); //intilizes the
}
void loop() {
digitalWrite(ledPin, HIGH); //blink LED at 4HZ
delay(burrito);
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
if (buttonState == LOW) // if button is pressed
{
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(capmotor,HIGH); //start the cap motor at max torque for 2 seconds
delay(200);
digitalWrite(capmotor, LOW); //turn off cap motor
delay (2000); //wait 2 seconds with cap motor off
while(digitalRead(limitPinB) == LOW){ //while the bottle limit switch is unpressed
digitalWrite(pourmotor, HIGH); //turn on the pour motor
}
digitalWrite(pourmotor, LOW); //once the bottle limit switch is pressed, stop the pourmotor
delay(150);
}
while(digitalRead(limitPinA) == LOW){ //while the limit switch is unpressed
digitalWrite(motordir, HIGH); //change the direction of the motor
digitalWrite(pourmotor, HIGH); //run the motor in the opposite direction
}
digitalWrite(pourmotor,LOW); //when the limit switch is pressed, turn off the motor and wait 3 seconds before returning to the loop
delay(3000);
digitalWrite(motordir,LOW); //turn the direction of the motor back to the starting direction
}
[b]
digitalWrite(ledPin, LOW);
delay(125);[/b]
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
if (buttonState == LOW) // if button is pressed
{
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(capmotor,HIGH); //start the cap motor at max torque for 2 seconds
delay(500);
digitalWrite(capmotor, LOW); //turn off cap motor
delay (2000); //wait 2 seconds with cap motor off
while(digitalRead(limitPinB) == LOW){ //while the bottle limit switch is unpressed
digitalWrite(pourmotor, HIGH); //turn on the pour motor
}
digitalWrite(pourmotor, LOW); //once the bottle limit switch is pressed, stop the pourmotor
delay(150);
}
while(digitalRead(limitPinA) == LOW){
digitalWrite(motordir, HIGH); //change the direction of the motor
digitalWrite(pourmotor, HIGH); //run the motor in the opposite direction
}
digitalWrite(pourmotor,LOW);
delay(3000);
digitalWrite(motordir,LOW); //turn the direction of the motor back to the starting direction
}
digitalWrite(ledPin, LOW);
delay(burrito);
}