Way can I not get this if statement to work?
if (dustreading > maxsize);{
maxsize = dustreading;
}
If dustreading go's up and down why does maxsize track it and not show max reading? >:(
Way can I not get this if statement to work?
if (dustreading > maxsize);{
maxsize = dustreading;
}
If dustreading go's up and down why does maxsize track it and not show max reading? >:(
Check the Arduino if statement reference, which I found by Googling 'Arduino if'
Does the code you quoted;
if (dustreading > maxsize);{
maxsize = dustreading;
}
match the format in the reference ?
Rich555:
Way can I not get this if statement to work?if (dustreading > maxsize);{
maxsize = dustreading;
}If dustreading go's up and down why does maxsize track it and not show max reading? >:(
Notice what you did here:
[b]if (dustreading > maxsize) [color=red];[/color] { <-- note the red semicolon!
maxsize = dustreading;
}[/b]
That statement, compiled executes this:
[b](1) if (dustreading > maxsize); // that's it... just an IF with nothing to do about it because the semicolon ends the IF line
(2) { maxsize = dustreading; } // this executes always because it's NOT under the control of the "IF" statement above.[/b]
Conclusion: Remove the semicolon highlighted in red.
if (dustreading > maxsize)
maxsize = dustreading;
this doesn't work either.
Rich555:
if (dustreading > maxsize)
maxsize = dustreading;this doesn't work either.
Yes it does. If your sketch is not doing what you expect after fixing this, we will need to see the whole thing, because you have an error in some other part of your sketch.
Rich555:
if (dustreading > maxsize)
maxsize = dustreading;this doesn't work either.
Yeah, that should work. I use single statement if()'s all the time.
... If dustreading go's up and down why does maxsize track it and not show max reading?
Cause you told it to. That's the only way computers do anything. But you think the problem is in this one line, so you didn't show where the actual problem is.
You have discovered the empty statement. Every ';' in C/C++ ends a statement, which
if it has nothing in it is still a statement (with no effect of course).
Thus these all do exactly the same thing:
if (whatever) ;
if (whatever)
{}
if (whatever)
{;}
if (whatever)
{;;;;{};;{;;;;};;}
Thus these all do exactly the same thing:
The last one looks like you are swearing at us. 8)
this is a small program that shows the problem.
void setup() {
Serial.begin(19200);
}
void loop() {
int dustreading;
int dustreading1;
int maxsize;
dustreading = analogRead(A0); // get new reading
dustreading1 = map(dustreading,0, 1023, 0, 34);
if (dustreading1 > maxsize)
maxsize = dustreading1;
Serial.print(dustreading1);
Serial.print(" ");
Serial.println(maxsize);
delay(2000);
}
If dustreading go's up and down why does maxsize track it and not show max reading?
Because you reset the value to random values each time through loop()
void loop()
{
int dustreading;
int dustreading1;
int maxsize;
etc
yes, this works. thank you!
Rich555:
this works
What works ?