Hello everyone. I am trying to create a "for" loop inside an "if" statement which I fail to have executed even when the condition for the if statement is met. I need someone experienced than me to have a look and give me an insight for why this may be happening. This program will work for counting to 30 when a button is pressed and do something particular when that time interval ends. Can you please have a look and help me out? I am fairly sure that the wiring is OK. I'll try to attach the code below.
Thank you for taking your time to read this,
Cheers
const char buttonPin = 11; // D8 on Arduino Nano
bool pressed = false; //Created a boolean for a clear code look
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
bool buttonState = digitalRead(buttonPin);
if (buttonState == pressed){
for (int i = 0; i <= 30; i++){ //Can't enter this loop, even when bool pressed = true
Serial.print("Counting, i=\t");
delay(1000);
}
Serial.print("Time to open the door\n"); // I need this to only execute once for each time the for loop has ended
}else{
Serial.print("No interaction\n"); //This is just a serial print for validation of functioning
delay(1000);
}
}
No sadly the A does not print I also tried that. And yes, at this point I think I had held the button for longer than 2 seconds, shorter than 2 seconds, also probably exactly two seconds. I have been trying for a while now...
I was replacing all the wiring with newer cables, another push button but still no luck. I am certain that this must be a very simple issue that I miss since I lack the experience
const uint8_t buttonPin = 11; // D8 on Arduino Nano
void setup()
{
// put your setup code here, to run once:
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop()
{
// put your main code here, to run repeatedly:
bool buttonState = digitalRead(buttonPin);
if (buttonState == false) // false == zero (0)
{
for (int i = 0; i <= 30; i++)
{ //Can't enter this loop, even when bool pressed = true
Serial.print("Counting ");
Serial.println(i);
delay(1000);
}
Serial.print("Time to open the door\n"); // I need this to only execute once for each time the for loop has ended
}
else
{
Serial.print("No interaction\n"); //This is just a serial print for validation of functioning
delay(1000);
}
}
I think it always returns 1 because of the INPUT_PULLUP. I tried to change the orientation of my 4 legged push button (figured it might be the case) but still it returned 1 even when the button is pressed. One of the switching legs is connected to D8 (11) and the other is connected to the GND.
It will always return 1 (true) while the button is not pressed (because of the pull up) If you press the button for longer than one second, your for loop should activate. One other thing to check with a multimeter is - are you absolutely certain you are connecting to the correct pair of pins on the switch?
No I am not. That's why I try to run each solution offered by using the 2 possible orientations of the switch. One orientation should have worked, shouldn't it?
Use a multimeter to make sure you have the correct pair of pins selected and operate the switch to ensure that it's working? Then we can be sure the switch (and your connections to it) are ok ?
For purposes of troubleshooting, substitute a wire for the switch.
const char buttonPin = 11; // D8 on Arduino Nano
//bool pressed = false; //Created a boolean for a clear code look
const byte statusLED = 13;
void setup()
{
// put your setup code here, to run once:
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(19200);
pinMode(statusLED, OUTPUT);
digitalWrite(statusLED, LOW);
}
void loop()
{
// put your main code here, to run repeatedly:
byte buttonState = digitalRead(buttonPin);
if (!buttonState)
{
digitalWrite(statusLED, HIGH);
for (int idx = 0; idx <= 30; idx++)
{
//Can't enter this loop, even when bool pressed = true
Serial.print("Counting ");
Serial.println(idx);
delay(500);
}
Serial.println("Time to open the door");
digitalWrite(statusLED, LOW);
}
//else
//{
// Serial.print("No interaction\n"); //This is just a serial print for validation of functioning
// delay(500);
//}
}