From 88b06443e08367515ea988517f982dd581f8d778 Mon Sep 17 00:00:00 2001 From: Alexander Richter Date: Mon, 21 Nov 2022 12:56:51 +0100 Subject: [PATCH] rework of code i wasn't happy with my first code, so i began changing it to be more versatile. --- LinuxCNC_ArduinoConnector.ino | 210 ++++++++++++++++++++++------------ 1 file changed, 138 insertions(+), 72 deletions(-) diff --git a/LinuxCNC_ArduinoConnector.ino b/LinuxCNC_ArduinoConnector.ino index d41d2df..46a50cb 100644 --- a/LinuxCNC_ArduinoConnector.ino +++ b/LinuxCNC_ArduinoConnector.ino @@ -1,84 +1,169 @@ - - //###IO's### +#define POTI +#ifdef POTI + const int PotiNo = 1; + int PotiPins[] = {A3}; //Knob Potentiometer for SpindleSpeed in manual mode +#endif -//###Analog In### -int SpOd[] = {A1,8,-1}; //Knob Spindle Overdrive on A1, has 4 Pos, and initialised with 0 -int FdRes[] = {A2,3,-1};//Knob Feed Resolution on A2, has 9 Pos, and initialised with 0 -const int SpSp = A3;//Knob Potentiometer for SpindleSpeed in manual mode +#define LPOTI +#ifdef LPOTI + const int LPotiNo = 2; + int LPotiPins[LPotiNo][2] = { + {A1,8}, //Latching Knob Spindle Overdrive on A1, has 9 Positions + {A2,3} //Latching Knob Feed Resolution on A2, has 4 Positions + }; +#endif -//###Digital In### -//Absolute encoder Knob -const int DI0 = 27;//1 -const int DI1 = 28;//2 -const int DI2 = 31;//4 -const int DI3 = 29;//8 -const int DI4 = 30;//16 - -//Buttons -const int Buttons = 17; //Number of Buttons (Inputs) -int Button[] = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48}; - -//Digital Out### -const int Outputs = 22; //Number of Outputs (i use them for LEDs in Buttons) -int OutPin[] = {13,12,11,10,9,8,7,6,5,4,3,2,22,23,24,25,26}; +#define ABSENCODER +#ifdef ABSENCODER + int AbsEncPins[] = {27,28,31,29,30}; //1,2,4,8,16 +#endif +#define INPUTS +#ifdef INPUTS + const int InputNo = 16; //number of inputs using internal Pullup resistor. (short to ground to trigger) + int InPins[] = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48}; +#endif +#define OUTPUTS +#ifdef OUTPUTS + const int OutputNo = 22; //number of outputs + int OutPins[] = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48}; +#endif + +#define STATUSLED +#ifdef STATUSLED + const int StatLedPin = 13; //Pin for Status LED + const int StatLedErrDel[] = {1000,10}; //Blink Timing for Status LED Error (no connection) + +#endif + //Variables for Saving States -int oldvar = -1; -int SpSpvar = -1; -int ButtonState[Buttons]; -int OutState[Outputs]; -int FdOdSt = -1; -int SpOdSt = -1; -int SpSpSt = -1; +#ifdef POTI + int oldPoti[PotiNo]; +#endif +#ifdef LPOTI + int oldLpoti[LPotiNo]; +#endif +#ifdef ABSENCODER + int oldAbsEncState; +#endif +#ifdef INPUTS + int InState[InputNo]; + int oldInState[InputNo]; +#endif +#ifdef OUTPUTS + int OutState[OutputNo]; + int oldOutState[OutputNo]; +#endif + +int FirstSend = 0; +unsigned long oldmillis = 0; + + void setup() { - pinMode(DI0, INPUT_PULLUP); - pinMode(DI1, INPUT_PULLUP); - pinMode(DI2, INPUT_PULLUP); - pinMode(DI3, INPUT_PULLUP); - pinMode(DI4, INPUT_PULLUP); -//setting Buttons as Inputs with internal Pullup Resistors - for(int i= 0; i= StatLedErrDel[0]){ + + digitalWrite(StatLedPin, HIGH); + } + if (newMillis - oldmillis >= StatLedErrDel[0]+StatLedErrDel[1]){{ + digitalWrite(StatLedPin, LOW); + oldmillis = newMillis; + } + } + +} + + +int writeOutputs(){ + for(int x = 0; x