Build1=Default,B4RDev Group=Default Group Library1=rcore Library2=rrandomaccessfile Library3=rsoftwareserial NumberOfFiles=0 NumberOfLibraries=3 NumberOfModules=0 Version=3 @EndOfDesignText@ #Region Project Attributes #AutoFlushLogs: True #CheckArrayBounds: True #StackBufferSize: 500 #End Region 'This is a piece of software I use to move the tool head of my CNC-Mill controlled by Duet2 Ethernet. An Arduino Nano is connected to the Panel-Due serial interface. 'I added this line to my config.g : M575 P1 B57600 S0 ;PanelDue-Port -> Arduino Nano. Please use level shifter. Arduino is 5V, Duet 3.3V 'Pushbutton for directions, continuous move, homing and a switch to control Jog mode in general are connected to the input pins of Arduino. 'The switch "JogMode Active" activates general operation of pushbutton. A hint "Jogmodus aktive" is repeatedly (abot 9 sec) displayed on the DWC screen. 'Pressing a "direction button" moves the head a step with the configureg distance. Pressing the "Continuous" button simultanuous with a "direction button" moves the tool head continuously 'as long as buttons are down. 'Variable values can be modifyed to match to your machine forn line 85. 'You can translate DWC Display messages from line 94 'If you modify distance and Jogspeed for continuous moves please also regard timer2 time that fires the commands. 'Formula: ContDistanceXYZ = Speed[mm/min] /60 * Timer2[ms] /1000 'Limitation at my machine. Continuous moves slow down a litle bit until enough commands are buffered. Sounds strange. 'This code is transfered to Arduino with "B4R" form Anywhere Software. Download of B4R is free. 'Feel free to modify this small piece of software and enjoy. 'ToDo Display ansteuern 'Pin Ausgang D8 DC Display via Pegelwandler A0 an Display' 'Pin Ausgang D9 RST Display via Pegelwandler Reset 'Pin Ausgang D10 SCLK Display via Pegelwandler CS 'Pin Ausgang D11 SID Display via Pegelwandler SDA 'Pin Ausgang D12 Reserve 'Pin Ausgang D13 SCLK Display via Pegelwandler SCK 'Pinout of Arduino Nano 'Pin Eingang analog A7 nicht benutzen not in use 'Pin Eingang analog A6 nicht benutzen not in use 'Pin Eingang analog A5 Taster Z-Plus UP 'Pin Eingang analog A4 Taster Z-Minus DOWN 'Pin Eingang analog A3 Taster X-Plus 'Pin Eingang analog A2 Taster X-Minus 'Pin Eingang analog A1 Taster Y-Plus 'Pin Eingang analog A0 Taster Y-Minus 'Pin D2 auf RXD -> Duet TXD via Pegelwandler 'Pin D3 auf TXD -> Duet RXD via Pegelwandler 'Pin Eingang D4 Taster/Schalter JogMode aktiv 'Pin Eingang D5 Taster Homing 'Pin Eingang D6 Taster "Fortlaufend" 'Pin Ausgang D7 Reserve not in use Sub Process_Globals Public Serial1 As Serial Private SoftwareSerial1 As SoftwareSerial Private astream As AsyncStreams Private ANFZ() As Byte = Array As Byte(34) Private LF() As Byte = Array As Byte(10) Private BounceTime As ULong Private BounceDelay = 20 As ULong Private pinD4 As Pin 'Jog aktiv Private pinD5 As Pin 'Taster Homing Private pinD6 As Pin 'Taster "Fortlaufend" Continuous Private pinD7 As Pin 'Reserve Private pinA0 As Pin 'Y-Minus Private pinA1 As Pin 'Y-Plus Private pinA2 As Pin 'X-Minus Private pinA3 As Pin 'X-Plus Private pinA4 As Pin 'Z-Minus Private pinA5 As Pin 'Z-Plus Private timer1 As Timer Private timer2 As Timer Private JogBetrieb As Boolean 'Jogmode Private Fortlaufend As Boolean 'Continuous Private LogStatus As Boolean = True Private XMinus As Boolean = False Private XPlus As Boolean = False Private YMinus As Boolean = False Private YPlus As Boolean = False Private ZMinus As Boolean = False Private ZPlus As Boolean = False Private pinLED13 As Pin 'pin for LED 13 on the Arduino ' Add this line to config.g to switch off checksum checking and set baudrate: M575 P1 B57600 S0 ;USB-> Arduino ok 'Change values from here Private Timer1Mills As UInt = 9000 ' Loop time in Milliseconsds to display message. Recomendation > 9000 Private Timer2Mills As UInt = 100 ' Loop time in Milliseconsds to repeate continuous move command Private StepDist As String = ".25" ' Distance of one "Step" in mm the machine moves when a direction button is pressed. Private ContDistXY As String = "1.667" ' Distance in mm the machine moves repeatedly when a "X" or "Y" direction button is pressed while pushing down "continuos"-button. Private ContDistZ As String = "1.667" ' Distance in mm the machine moves repeatedly when a "Z" direction button is pressed while pushing down "continuos"-button. Private Speed As String = "F1000" ' Jog Speed for G1-Command Private StrJogActive As String = "Jogmodus aktiv" 'repeated message in DWC, when Jog mode is on Private StrJogOff As String = "Jogmodus ausgeschaltet" 'message once displayed in DWC when jog mode is switched off Private StrSwitchOn As String = "Zuerst JogModus einschalten" 'Message is displayed when Jogmode is switched off and a Button is pressed ' Private ValueD13 As Boolean End Sub Private Sub AppStart Serial1.Initialize(115200) Log("AppStart") pinLED13.Initialize(13, pinLED13.MODE_OUTPUT) pinD4.Initialize(4, pinD4.MODE_INPUT_PULLUP) 'Jogmode aktiv pinD4.AddListener("pinD4_StateChanged") pinD5.Initialize(5, pinD5.MODE_INPUT_PULLUP) 'Homing pinD5.AddListener("pinD5_StateChanged") pinD6.Initialize(6, pinD6.MODE_INPUT_PULLUP) ' "Fortlaufend" pinD6.AddListener("pinD6_StateChanged") pinD7.Initialize(7, pinD7.MODE_INPUT_PULLUP) 'Reserve pinA0.Initialize(14, pinA0.MODE_INPUT_PULLUP) 'Y-Minus pinA0.AddListener("pinA0_StateChanged") pinA1.Initialize(15, pinA1.MODE_INPUT_PULLUP) 'Y-Plus pinA1.AddListener("pinA1_StateChanged") pinA2.Initialize(16, pinA2.MODE_INPUT_PULLUP) 'X-Minus pinA2.AddListener("pinA2_StateChanged") pinA3.Initialize(17, pinA3.MODE_INPUT_PULLUP) 'X-Plus pinA3.AddListener("pinA3_StateChanged") pinA4.Initialize(18, pinA4.MODE_INPUT_PULLUP) 'Z-Minus pinA4.AddListener("pinA4_StateChanged") pinA5.Initialize(19, pinA5.MODE_INPUT_PULLUP) 'Z-Plus pinA5.AddListener("pinA5_StateChanged") SoftwareSerial1.Initialize(57600,2,3) 'Rx-Pin, TX-Pin 57600 astream.Initialize(SoftwareSerial1.Stream, "AStream_NewData", Null) timer1.Initialize("timer1_Tick", Timer1Mills) timer1.Enabled = True timer2.Initialize("timer2_Tick", Timer2Mills) timer2.Enabled = True End Sub Sub Timer1_Tick 'Write M27 Byteposition im File ' M26 S(Byteposition) If Not(JogBetrieb) And Not(pinD4.DigitalRead) Then 'Falls beim Einschalten des Duet der Schalter für den Jogbetrieb eingeschaltet ist... Log("Jogbetrieb eingeschaltet") JogBetrieb = True astream.Write("M118 S").Write(ANFZ).Write(StrJogActive).Write(ANFZ).Write(LF) astream.Write("G91").Write(LF) 'relative Posionierung 'astream.Write("M555 P6").Write(LF) ' keine Bufferung im "nanoDLP compatibility mode????" astream.Write("G1 ").Write(Speed).Write(LF) timer1.Enabled = True End If If JogBetrieb Then astream.Write("M118 S").Write(ANFZ).Write(StrJogActive).Write(ANFZ).Write(LF) End Sub Sub Timer2_Tick If JogBetrieb And Fortlaufend Then If XPlus Then astream.Write("G1 X").Write(ContDistXY).Write(LF) If XMinus Then astream.Write("G1 X-").Write(ContDistXY).Write(LF) If YPlus Then astream.Write("G1 Y").Write(ContDistXY).Write(LF) If YMinus Then astream.Write("G1 Y-").Write(ContDistXY).Write(LF) If ZPlus Then astream.Write("G1 Z").Write(ContDistZ).Write(LF) If ZMinus Then astream.Write("G1 Z-").Write(ContDistZ).Write(LF) End If End Sub Sub AStream_NewData (Buffer() As Byte) 'Eingangsdaten von Duet Dim msg() As Byte msg = Buffer 'BytesToString(Buffer, 0, Buffer.Length, "UTF8") If LogStatus Then Log("msg from Duet: ", msg) End Sub Private Sub pinD4_StateChanged (State As Boolean) 'JogMode Ein Select State Case True JogBetrieb = False astream.Write("G90").Write(LF) 'relative Posionierung ' astream.Write("M555 P1").Write(LF) ' keine Bufferung im "nanoDLP compatibility mode" timer1.Enabled=False astream.Write("M118 S").Write(ANFZ).Write(StrJogOff).Write(ANFZ).Write(LF) Case False If Millis - BounceTime < BounceDelay Then Return Else BounceTime = Millis JogBetrieb = True astream.Write("M118 S").Write(ANFZ).Write(StrJogActive).Write(ANFZ).Write(LF) astream.Write("G91").Write(LF) 'relative Posionierung ' astream.Write("M555 P6").Write(LF) ' keine Bufferung im "nanoDLP compatibility mode" astream.Write("G1 ").Write(Speed).Write(LF) timer1.Enabled = True End If End Select pinLED13.DigitalWrite(Not(State)) If LogStatus Then Log("StateD4-JogMode: ", State) End Sub Private Sub pinD5_StateChanged (State As Boolean) 'HomingTaster Select State Case True Case False If Millis - BounceTime < BounceDelay Then Return Else BounceTime = Millis If JogBetrieb Then astream.Write("G28").Write(LF) Else astream.Write("M118 S").Write(ANFZ).Write(StrSwitchOn).Write(ANFZ).Write(LF) End If End If End Select pinLED13.DigitalWrite(Not(State)) If LogStatus Then Log("StateD5-Homing: ", State) End Sub Private Sub pinD6_StateChanged (State As Boolean) '"Fortlaufend Select State Case True Fortlaufend = False Case False If Millis - BounceTime < BounceDelay Then Return Else BounceTime = Millis Fortlaufend = True If Not(JogBetrieb) Then astream.Write("M118 S").Write(ANFZ).Write(StrSwitchOn).Write(ANFZ).Write(LF) End If End Select pinLED13.DigitalWrite(Not(State)) If LogStatus Then Log("StateD6-Fortlaufend: ", State) End Sub Private Sub pinA0_StateChanged (State As Boolean) 'Y-Minus Select State Case True YMinus = False Case False If Millis - BounceTime < BounceDelay Then Return Else BounceTime = Millis YMinus = True If JogBetrieb And Not(Fortlaufend) Then astream.Write("G1 Y-").Write(StepDist).Write(LF) If Not(JogBetrieb) Then astream.Write("M118 S").Write(ANFZ).Write(StrSwitchOn).Write(ANFZ).Write(LF) End If End Select pinLED13.DigitalWrite(Not(State)) If LogStatus Then Log("StateA0-Y-Minus: ", State) End Sub Private Sub pinA1_StateChanged (State As Boolean) 'Y-Plus Select State Case True YPlus = False Case False If Millis - BounceTime < BounceDelay Then Return Else BounceTime = Millis YPlus = True If JogBetrieb And Not(Fortlaufend) Then astream.Write("G1 Y").Write(StepDist).Write(LF) If Not(JogBetrieb) Then astream.Write("M118 S").Write(ANFZ).Write(StrSwitchOn).Write(ANFZ).Write(LF) End If End Select pinLED13.DigitalWrite(Not(State)) If LogStatus Then Log("State:A1-Y-Plus ", State) End Sub Private Sub pinA2_StateChanged (State As Boolean) 'X-Minus Select State Case True XMinus = False Case False If Millis - BounceTime < BounceDelay Then Return Else BounceTime = Millis XMinus = True If JogBetrieb And Not(Fortlaufend) Then astream.Write("G1 X-").Write(StepDist).Write(LF) If Not(JogBetrieb) Then astream.Write("M118 S").Write(ANFZ).Write(StrSwitchOn).Write(ANFZ).Write(LF) End If End Select pinLED13.DigitalWrite(Not(State)) If LogStatus Then Log("StateA2-X-Minus: ", State) End Sub Private Sub pinA3_StateChanged (State As Boolean) 'X-Plus Select State Case True XPlus = False Case False If Millis - BounceTime < BounceDelay Then Return Else BounceTime = Millis XPlus = True If JogBetrieb And Not(Fortlaufend) Then astream.Write("G1 X").Write(StepDist).Write(LF) If Not(JogBetrieb) Then astream.Write("M118 S").Write(ANFZ).Write(StrSwitchOn).Write(ANFZ).Write(LF) End If End Select pinLED13.DigitalWrite(Not(State)) If LogStatus Then Log("StateA3-X-Plus: ", State) End Sub Private Sub pinA4_StateChanged (State As Boolean) 'Z-Minus Select State Case True ZMinus = False Case False If Millis - BounceTime < BounceDelay Then Return Else BounceTime = Millis ZMinus = True If JogBetrieb And Not(Fortlaufend) Then astream.Write("G1 Z-").Write(StepDist).Write(LF) If Not(JogBetrieb) Then astream.Write("M118 S").Write(ANFZ).Write(StrSwitchOn).Write(ANFZ).Write(LF) End If End Select pinLED13.DigitalWrite(Not(State)) If LogStatus Then Log("StateA4-Z-Minus: ", State) End Sub Private Sub pinA5_StateChanged (State As Boolean) 'Z-Plus Select State Case True ZPlus = False Case False If Millis - BounceTime < BounceDelay Then Return Else BounceTime = Millis ZPlus = True If JogBetrieb And Not(Fortlaufend) Then astream.Write("G1 Z").Write(StepDist).Write(LF) If Not(JogBetrieb) Then astream.Write("M118 S").Write(ANFZ).Write(StrSwitchOn).Write(ANFZ).Write(LF) End If End Select pinLED13.DigitalWrite(Not(State)) If LogStatus Then Log("StateA5-Z-Plus: ", State) End Sub