rework of code
i wasn't happy with my first code, so i began changing it to be more versatile.
This commit is contained in:
parent
958cc0d9cb
commit
88b06443e0
@ -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<Buttons;i++){
|
||||
pinMode(Button[i], INPUT_PULLUP);
|
||||
ButtonState[i] = -1;
|
||||
#ifdef ABSENCODER
|
||||
pinMode(AbsEncPins[0], INPUT_PULLUP);
|
||||
pinMode(AbsEncPins[1], INPUT_PULLUP);
|
||||
pinMode(AbsEncPins[2], INPUT_PULLUP);
|
||||
pinMode(AbsEncPins[3], INPUT_PULLUP);
|
||||
pinMode(AbsEncPins[4], INPUT_PULLUP);
|
||||
#endif
|
||||
|
||||
#ifdef INPUT
|
||||
//setting Inputs with internal Pullup Resistors
|
||||
for(int i= 0; i<InputNo;i++){
|
||||
pinMode(InPins[i], INPUT_PULLUP);
|
||||
oldInState[i] = -1;
|
||||
}
|
||||
|
||||
for(int o= 0; o<Outputs;o++){
|
||||
pinMode(OutPin[o], OUTPUT);
|
||||
OutState[o] = 0;
|
||||
#endif
|
||||
#ifdef OUTPUT
|
||||
for(int o= 0; o<OutputNo;o++){
|
||||
pinMode(OutPins[o], OUTPUT);
|
||||
oldOutState[o] = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef STATUSLED
|
||||
pinMode(StatLedPin, OUTPUT);
|
||||
#endif
|
||||
|
||||
|
||||
//Setup Serial
|
||||
Serial.begin(9600);
|
||||
Serial.begin(115200);
|
||||
while (!Serial){
|
||||
#ifdef STATUSLED
|
||||
StatLedErr();
|
||||
#endif
|
||||
}
|
||||
if (Serial){delay(1000);}
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
while (!Serial){
|
||||
#ifdef STATUSLED
|
||||
StatLedErr();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
listenSerial();
|
||||
readAbsKnob();
|
||||
readPoti(SpSp);
|
||||
|
||||
SpOd[2] = readLPoti(SpOd[0],SpOd[1],SpOd[2]);
|
||||
FdRes[2] = readLPoti(FdRes[0],FdRes[1],FdRes[2]);
|
||||
writeOutputs();
|
||||
readInputs();
|
||||
delay(10);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int StatLedErr(){
|
||||
unsigned long newMillis = millis();
|
||||
|
||||
if (newMillis - oldmillis >= StatLedErrDel[0]){
|
||||
|
||||
digitalWrite(StatLedPin, HIGH);
|
||||
}
|
||||
if (newMillis - oldmillis >= StatLedErrDel[0]+StatLedErrDel[1]){{
|
||||
digitalWrite(StatLedPin, LOW);
|
||||
oldmillis = newMillis;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
int writeOutputs(){
|
||||
for(int x = 0; x<OutputNo;x++){
|
||||
digitalWrite(OutPins[x], OutState[x]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int readLPoti(int Pin,int Pos, int Stat){
|
||||
int var = analogRead(Pin)+20; //giving it some margin so Numbers dont jitter
|
||||
Pos = 1024/Pos;
|
||||
var = var/Pos;
|
||||
if(var != Stat){
|
||||
Stat = var;
|
||||
Serial.print("LP");
|
||||
Serial.print(Pin);
|
||||
Serial.print(":");
|
||||
Serial.println(Stat);
|
||||
|
||||
}
|
||||
return (Stat);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
int listenSerial(){
|
||||
long rec=0;
|
||||
if(Serial.available()){
|
||||
@ -100,27 +185,7 @@ int listenSerial(){
|
||||
}
|
||||
}
|
||||
|
||||
int writeOutputs(){
|
||||
for(int x = 0; x<Outputs;x++){
|
||||
digitalWrite(OutPin[x], OutState[x]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int readLPoti(int Pin,int Pos,int Stat){
|
||||
int var = analogRead(Pin)+20; //giving it some margin so Numbers dont jitter
|
||||
Pos = 1024/Pos;
|
||||
var = var/Pos;
|
||||
if(var != Stat){
|
||||
Stat = var;
|
||||
Serial.print("LP");
|
||||
Serial.print(Pin);
|
||||
Serial.print(":");
|
||||
Serial.println(Stat);
|
||||
|
||||
}
|
||||
return (Stat);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -188,4 +253,5 @@ int readAbsKnob(){
|
||||
oldvar = var;
|
||||
|
||||
return (var);
|
||||
}
|
||||
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user