Answer the question
In order to leave comments, you need to log in
The problem is in the arduino IDE language. Command Serial.println(); ignores the condition, why?
int a;
intb;
void setup() {
Serial.begin(9600);
}
void loop() {
a++;
if(a==1000){
a=0;
Serial.println(b);// prints to the port regardless of the condition!
b++;
}
}
Answer the question
In order to leave comments, you need to log in
See note.
It is difficult to answer you with such a design and wording of the question.
But in essence. What does ignore mean? What do you see in the port monitor?
Keep in mind that a ++ will reach 1000 very quickly, almost instantly
(homework for you, calculate or write code, check for how long)
Therefore, you may perceive it as ignoring the condition
==== Here is ESP at hand
Condition a = 1000, runs every 200 microseconds!
You forgot to initialize the a variable, so it gets a random value when reset.
int a = 0;
Before turning here, I tried almost all the options when you add b ++ to the condition; so that when a==1000 is executed, it adds one to b each time the condition is met, and output Serial.println(b); condition, everything works. it outputs zeros, after the condition a==1000 is met, it outputs ones, and so on. but just add Serial.println(b) to the condition, that's it, kaput! delay(); doesn't help either.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question