Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    using while and loop and variable in Macro

    Scheduled Pinned Locked Moved
    Gcode meta commands
    4
    15
    1.1k
    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.
    • fcwiltundefined
      fcwilt @CrazyCreator
      last edited by

      @crazycreator said in using while and loop and variable in Macro:

      can you explain to me why nothing happens when i start the macro?

      if sensors.analog[0].lastReading >= 40 then the while loop will not run.

      Frederick

      Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

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

        @fcwilt
        The heating is not on ... or there is not even one installed. Only one temperature sensor is connected.

        How can I display, for check, the result of sensors.analog[0].lastReading ???

        Bildschirmfoto 2021-11-20 um 18.06.32.png

        is this the right value? rounding in dwc I see 23°C

        http://www.crazycreatorcube.com

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

          @crazycreator said in using while and loop and variable in Macro:

          @fcwilt
          The heating is not on ... or there is not even one installed. Only one temperature sensor is connected.

          How can I display, for check, the result of sensors.analog[0].lastReading ???

          right before the line containing the while put this:

          echo sensors.analog[0].lastReading
          

          It will display a message showing the value. While the message will go away after a few seconds you can still see the result in the DWC Console.

          Frederick

          Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

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

            @fcwilt said in using while and loop and variable in Macro:

            echo sensors.analog[0].lastReading

            i See the message in dwc but this is the only thing.

            my macro:

            ; R=Red / U=Green / B=Blue / P=Brightness (0-255)
            M150 X1 ; Define RGB NeoPixel (default in RRF 3.2 and later)
            
            var counter = 150
            var down = true
            echo sensors.analog[0].lastReading
            
            
             
            while sensors.analog[0].lastReading < 40
            	if var.down
            		set var.counter = var.counter-1
            		M150 R255 U0 B0 S20 P<var.counter> F0
            		if var.counter == 255
            			set var.down = true
            	else
            		set var.counter = var.counter+1
            		M150 R255 U0 B0 S20 P<var.counter> F0
            		if var.counter == 0
            			set var.down = false 	
            	G4 P100
            

            When I change P<var.counter> to P155 the script is running actually endless 😄
            and always in the same if ....

            I think the variable var.counter will not be changed.

            because if I change var down = true and var.down = false then it changes from the if to the else but it then gets stuck in it.

            http://www.crazycreatorcube.com

            fcwiltundefined GeneRisiundefined 2 Replies Last reply Reply Quote 0
            • fcwiltundefined
              fcwilt @CrazyCreator
              last edited by

              @crazycreator

              If the while loop is not ending it is either because the sensor reading is not changing OR the firmware does not update the sensor reading in a while loop.

              You could put code in daemon.g but that only runs once every 10 seconds.

              Frederick

              Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

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

                @fcwilt
                But that would be strange if the while loop only continues when the value of the sensor has changed.

                the condition says if the value is less than 40 then run.

                http://www.crazycreatorcube.com

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

                  @crazycreator said in using while and loop and variable in Macro:

                  @fcwilt
                  But that would be strange if the while loop only continues when the value of the sensor has changed.

                  the condition says if the value is less than 40 then run.

                  What was the value displayed by the echo statement when you ran the macro?

                  Frederick

                  Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

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

                    @fcwilt 23

                     
                    while sensors.analog[0].lastReading < 40
                    	if var.down
                    		set var.counter = var.counter-1
                    		M150 R255 U0 B0 S20 P<var.counter> F0
                    		echo var.counter
                    		if var.counter == 0
                    			set var.down = false
                    	else
                    		set var.counter = var.counter+1
                    		M150 R255 U0 B0 S20 P<var.counter> F0
                    		**echo var.counter**		
                    		if var.counter == 255
                    			set var.down = true 	
                    	G4 P50
                    

                    if I write an echo IN the loop and display the value of var.counter, then the outputs are perfectly counted. exactly as it should be, but the light does not come on.

                    if instead of P <var.counter> a fixed number is written in, the light goes on ... but then of course it doesn't breathe

                    if P <var.counter> the correct spelling?

                    http://www.crazycreatorcube.com

                    fcwiltundefined 1 Reply Last reply Reply Quote 0
                    • GeneRisiundefined
                      GeneRisi @CrazyCreator
                      last edited by GeneRisi

                      @crazycreator Values passed to a macro are found using "param." parameter letter. So, if the macro was called with P155, you would access the value as param.p. You could do this in the beginning of your macro:

                      If exists(param.p)
                         Set var.value = param.p
                      Else
                         Set var.value = 150
                      

                      Use var.value in the body of your macro.

                      Does that help ?

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

                        @crazycreator

                        Try using { and } instead of < and >.

                        Frederick

                        Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

                        CrazyCreatorundefined 1 Reply Last reply Reply Quote 1
                        • CrazyCreatorundefined
                          CrazyCreator @fcwilt
                          last edited by

                          @fcwilt
                          it lives ... it breathes

                          the {} were the solution

                          many many thanks: D

                          http://www.crazycreatorcube.com

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