Merge pull request #12 from ModuloFS/analog_read

Changed the Analog read function to more non blocking
This commit is contained in:
Alexander Richter 2023-10-29 11:53:45 +01:00 committed by GitHub
commit b3a3813d3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -334,6 +334,7 @@ const int debounceDelay = 50;
#endif #endif
#ifdef AINPUTS #ifdef AINPUTS
int oldAinput[AInputs]; int oldAinput[AInputs];
unsigned long sumAinput[AInputs];
#endif #endif
#ifdef LPOTIS #ifdef LPOTIS
int Lpoti[LPotis]; int Lpoti[LPotis];
@ -420,6 +421,7 @@ void setup() {
for(int i= 0; i<AInputs;i++){ for(int i= 0; i<AInputs;i++){
pinMode(AInPinmap[i], INPUT); pinMode(AInPinmap[i], INPUT);
oldAinput[i] = -1; oldAinput[i] = -1;
sumAinput[i] = 0;
} }
#endif #endif
#ifdef OUTPUTS #ifdef OUTPUTS
@ -662,6 +664,7 @@ void reconnect(){
#ifdef AINPUTS #ifdef AINPUTS
for (int x = 0; x < AInputs; x++){ for (int x = 0; x < AInputs; x++){
oldAinput[x] = -1; oldAinput[x] = -1;
sumAinput[x] = 0;
} }
#endif #endif
#ifdef LPOTIS #ifdef LPOTIS
@ -793,7 +796,7 @@ void controlDLED(int Pin, int Stat){
#endif #endif
#ifdef LPOTIS #ifdef LPOTIS
int readLPoti(){ void readLPoti(){
for(int i= 0;i<LPotis; i++){ for(int i= 0;i<LPotis; i++){
int var = analogRead(LPotiPins[i][0])+margin; int var = analogRead(LPotiPins[i][0])+margin;
int pos = 1024/(LPotiPins[i][1]-1); int pos = 1024/(LPotiPins[i][1]-1);
@ -808,20 +811,28 @@ int readLPoti(){
#ifdef AINPUTS #ifdef AINPUTS
int readAInputs(){ void readAInputs(){
static unsigned int samplecount = 0;
for(int i= 0;i<AInputs; i++){ for(int i= 0;i<AInputs; i++){
unsigned long var = 0;
for(int d= 0;d<smooth; d++){// take couple samples to denoise signal if (samplecount < smooth) {
var = var+ analogRead(AInPinmap[i]); sumAinput[i] = sumAinput[i] + analogRead(AInPinmap[i]);
} }
var = var / smooth; else {
if(oldAinput[i]!= var){ sumAinput[i] = sumAinput[i] / smooth;
oldAinput[i] = var; if(oldAinput[i]!= sumAinput[i]){
oldAinput[i] = sumAinput[i];
sendData('A',AInPinmap[i],oldAinput[i]); sendData('A',AInPinmap[i],oldAinput[i]);
} }
sumAinput[i] = 0;
} }
} }
if(samplecount < smooth){ samplecount = samplecount + 1;}
else {samplecount = 0;}
}
#endif #endif
#ifdef INPUTS #ifdef INPUTS
void readInputs(){ void readInputs(){