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.
    • CrazyCreatorundefined
      CrazyCreator
      last edited by

      I came up with a few lines of code with which I would like to try to let the neopixel stripes connected to the MB6HC "breathe" as long as the printer is idle.

      Unfortunately, my knowledge is currently more or less at NULL ... so I have absolutely no idea whether my lines of code work or whether they are in the correct syntax.

      I would prefer the whole thing as a macro, which starts when the printer is switched on and ends as soon as I turn on the heating and / or a print is running.

      maybe someone can help me with learning and solving.

      thanks

      Declare variable
      var <counter> = <int>
      var <down> = <bool>
      
      Fill variable
      var.counter = 1
      var.down = false
      
      while <heater.0 < 40>
      
      	if <var.counter> == 255
      		<var.down> = true
      	if <var.counter> == 0
      		<var.down> = false 	
      
      	if <var.down> == false
      		M150 …. P<var.counter> F0
      		var.counter = var.counter+1
      	if <var.down> == true
      		M150 …. P<var.counter> F0
      		var.counter = var.counter-1
      

      http://www.crazycreatorcube.com

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

        @crazycreator something like this may work if you put it in daemon.g assuming that sensor 0 is the bed thermistor:

        var counter = 1
        var down = false
         
        while sensors.analog[0].lastReading < 40
        	if var.down
        		set var.counter = var.counter-1
        		M150 …. P<var.counter> F0
        		if var.counter == 255
        			set var.down = true
        	else
        		set var.counter = var.counter+1
        		M150 …. P<var.counter> F0
        		if var.counter == 0
        			set var.down = false 	
        	G4 P100
        

        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

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

          @dc42 Many, many thanks for the feed, I have something to try and test and expand on. thanks.

          Is it imperative that I have to run it in a deamon.g, or could I also start it in a macro ... for testing the script or so.

          So the "breathing" of the led strip in idle should of course always run.
          I only ask if I want to program something similar for something else.

          First question:
          if I want to check var.down for true or false, then

          if var.down
          

          is enough to check the condition?

          /EDIT
          When I create a macro with this content:

          M150 X1 ; Define RGB NeoPixel (default in RRF 3.2 and later)
          
          var counter = 1
          var down = false
           
          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
          

          Then nothing happens when I start it ...

          http://www.crazycreatorcube.com

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

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

            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:

              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 small Utilmaker style, a small CoreXY and a E3D MS/TC setup. Various hotends. 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 small Utilmaker style, a small CoreXY and a E3D MS/TC setup. Various hotends. 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 small Utilmaker style, a small CoreXY and a E3D MS/TC setup. Various hotends. 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 small Utilmaker style, a small CoreXY and a E3D MS/TC setup. Various hotends. 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 small Utilmaker style, a small CoreXY and a E3D MS/TC setup. Various hotends. 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