Duet3D Logo

    Duet3D

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Documentation
    • Order

    Solved Multiple "while" conditions

    Gcode meta commands
    3
    10
    89
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • deckingman
      deckingman last edited by

      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?

      Ian
      https://somei3deas.wordpress.com/
      https://www.youtube.com/@deckingman

      fcwilt dc42 2 Replies Last reply Reply Quote 0
      • fcwilt
        fcwilt @deckingman last edited by

        @deckingman

        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
        
        

        Logic Error.png

        Printers: A FT-5 with the 713 upgrade bits. A custom MarkForged style. A small Utilmaker style and a CoreXY from kits. Various hotends. Using Duets (2 and 3) running 3.4.1

        deckingman 1 Reply Last reply Reply Quote 0
        • deckingman
          deckingman @fcwilt last edited by

          @fcwilt Thanks - but my code doesn't produce any errors. It just doesn't work as I thought it would.

          Ian
          https://somei3deas.wordpress.com/
          https://www.youtube.com/@deckingman

          fcwilt 1 Reply Last reply Reply Quote 0
          • fcwilt
            fcwilt @deckingman last edited by

            @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

            Printers: A FT-5 with the 713 upgrade bits. A custom MarkForged style. A small Utilmaker style and a CoreXY from kits. Various hotends. Using Duets (2 and 3) running 3.4.1

            1 Reply Last reply Reply Quote 0
            • dc42
              dc42 administrators @deckingman last edited by

              @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.

              Duet WiFi hardware designer and firmware engineer
              Please do not ask me for Duet support via PM or email, use the forum
              http://www.escher3d.com, https://miscsolutions.wordpress.com

              fcwilt 1 Reply Last reply Reply Quote 0
              • fcwilt
                fcwilt @dc42 last edited by

                @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

                Printers: A FT-5 with the 713 upgrade bits. A custom MarkForged style. A small Utilmaker style and a CoreXY from kits. Various hotends. Using Duets (2 and 3) running 3.4.1

                dc42 2 Replies Last reply Reply Quote 0
                • dc42
                  dc42 administrators @fcwilt last edited by

                  @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.

                  Duet WiFi hardware designer and firmware engineer
                  Please do not ask me for Duet support via PM or email, use the forum
                  http://www.escher3d.com, https://miscsolutions.wordpress.com

                  1 Reply Last reply Reply Quote 1
                  • dc42
                    dc42 administrators @fcwilt last edited by dc42

                    @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,

                    Duet WiFi hardware designer and firmware engineer
                    Please do not ask me for Duet support via PM or email, use the forum
                    http://www.escher3d.com, https://miscsolutions.wordpress.com

                    deckingman 1 Reply Last reply Reply Quote 1
                    • deckingman
                      deckingman @dc42 last edited by deckingman

                      @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.

                      Ian
                      https://somei3deas.wordpress.com/
                      https://www.youtube.com/@deckingman

                      dc42 1 Reply Last reply Reply Quote 2
                      • dc42
                        dc42 administrators @deckingman last edited by

                        @deckingman thanks, I'll mark this thread as solved. It had the useful effect of prompting @fcwilt to report the issue he found.

                        Duet WiFi hardware designer and firmware engineer
                        Please do not ask me for Duet support via PM or email, use the forum
                        http://www.escher3d.com, https://miscsolutions.wordpress.com

                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post
                        Unless otherwise noted, all forum content is licensed under CC-BY-SA