Hello, I am trying to change the variable State
to reset to 0 once it reaches a value of 20 or more.
int State;
void setup ()
{
Serial.begin(57600);
pinMode(4,INPUT);
}
void loop()
{
int ButtonPress = digitalRead(4);
if (ButtonPress==0)
{
State=State+1;
Serial.println(State);
delay(250);
}
{
if (State==20);
State=0;
}
}
With this code, the button readout just says "1", instead of counting. If I get rid of
{
if (State==20);
State=0;
}
Then it counts up, but again, I want it to reset once it reaches 20 (or more).
hatcollector:
if (State==20);
Oops.
Hint: why does this one look so different to your other "if"?
hatcollector:
if (State==20);
With a semicolon ( ; ) at the end of the if statement, the line after the if statement will execute unconditionally. Lose the semicolon and enclose the code for the if statement in curly brackets, even if it is just one line.
if (State >= 20)
{
State=0;
}
Welcome to the forum
if (State==20);
State=0;
}
Which of these statements will be executed depending on the value of State and which will always be executed no matter what the value of State is ?
What is the purpose of the semicolon ?
system
Closed
December 27, 2023, 6:00pm
5
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.