I
I
Ivan Nikiforov2021-03-31 20:37:10
Arduino
Ivan Nikiforov, 2021-03-31 20:37:10

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

4 answer(s)
V
Vladimir, 2021-03-31
@Looka

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!

V
vanyamba-electronics, 2021-03-31
@vanyamba-electronics

You forgot to initialize the a variable, so it gets a random value when reset.
int a = 0;

F
Fenrir89, 2021-04-01
@Fenrir89

If you need to wait a certain time, it is better to use delay(ms)

I
Ivan Nikiforov, 2021-04-01
@1Recviem1

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 question

Ask a Question

731 491 924 answers to any question