Multiple "while" conditions
-
I'm trying to wait within a macro until both the bed and hot end are at or above some pre-determined values.
I thought maybe I could use:-
while sensors.analog[1].lastReading < 120 && sensors.analog[0].lastReading < 40 M291 P"Waiting for bed and hot end to pre-heat" R"Pre-Print Macro" G4 S50
but that seems to work more like an "OR" than an "AND" - i,e. the macro continues once the bed has reached 40 regardless of the fact that the hot end might be less than 120.
What is the elegant way to do what I would like to do? I'm guessing I should use use nested "while" loops each with a single boolean? Or do I need to put the multiple booleans inside some sort of braces?
-
I'm using firmware 3.3 and "SD" mode (no SBC).
I've found the logic processing to be confusing.
I had to change the order of tests to get the following to work but I don't know why - yet.
var z_pos = 50 var z_trg = 5 ; this order gives an error - see below if {var.z_pos} < 2 || {var.z_pos} > 20 || {var.z_pos} < {var.z_trg} ; do something here ; this order works if {var.z_pos} < {var.z_trg} || {var.z_pos} < 2 || {var.z_pos} > 20 ; do something here
-
@fcwilt Thanks - but my code doesn't produce any errors. It just doesn't work as I thought it would.
-
@deckingman said in Multiple "while" conditions:
@fcwilt Thanks - but my code doesn't produce any errors. It just doesn't work as I thought it would.
Understood.
I was thinking that the flaw may not be in your approach but in the logic processing of the firmware.
Frederick
-
@deckingman, that should work. Please can you change the M291 command to display the values of the two sensor readings, to confirm what is being read.
-
@dc42 said in Multiple "while" conditions:
@deckingman, that should work. Please can you change the M291 command to display the values of the two sensor readings, to confirm what is being read.
Can you explain why the order of comparisons in my example made a difference?
It may well be an error I made but I cannot see it.
Thanks.
Frederick
-
@fcwilt said in Multiple "while" conditions:
Can you explain why the order of comparisons in my example made a difference?
No, and I am looking into it.
-
@deckingman @fcwilt I have located the bug with the ordering of conditions in the of-statement, and fixed it. I don't think the problem you are having with the while-condition is related, assuming you are not getting any error messages in the console. If it was caused by the same bug then I would expect there to be an error message, as there was for the if-statement.
PS - the fix is included in the 3.4 pre beta2 binaries at https://www.dropbox.com/sh/ja08b7qdzsl8kjc/AAAwUbkN2XJvurq5CuQTgx5Wa?dl=0,
-
@dc42 I've just see the error of my ways!!
What I want to happen is that the hot end must be at or above 120 deg C and the bed must be at or above 40 deg C before the rest of the stuff happens.
This statement that I used ...........
while sensors.analog[1].lastReading < 120 && sensors.analog[0].lastReading < 40
.......... won't work because if the bed reaches 40 but the hot end is at 110, it's still below 120, therefore the criteria has been met and the loop will exit.
So effectively, it's working like an "OR" rather than and "AND" because it will exit the loop as soon as one or other sensor reaches it's threshold as long as the other sensor is below it's threshold.
EDIT. BTW, I checked that the sensor values were being read correctly and they are. So the firmware is fine - it's my stupid application of it which is at fault.
-
@deckingman thanks, I'll mark this thread as solved. It had the useful effect of prompting @fcwilt to report the issue he found.