fix updateflag alway on

This commit is contained in:
interfisch 2020-01-22 22:34:30 +01:00
parent 026937393b
commit dfa89a9785
1 changed files with 17 additions and 22 deletions

View File

@ -8,11 +8,10 @@ uint16_t countedLow[INPUTS]; //count the times input was below threshold (touche
#define COUNTEDLOWTHRESHOLD 10 //how many times input has to be sampled as low in a row to count as real touch. #define COUNTEDLOWTHRESHOLD 10 //how many times input has to be sampled as low in a row to count as real touch.
// Inputdelay is given by: COUNTEDLOWTHRESHOLD*ADCREADINTERVAL // Inputdelay is given by: COUNTEDLOWTHRESHOLD*ADCREADINTERVAL
long last_adcmicros=0; unsigned long last_adcmicros=0;
#define ADCREADINTERVAL 2000 //in microseconds. interval to read all adc values #define ADCREADINTERVAL 2000 //in microseconds. interval to read all adc values
boolean inputchangeFlag; //flag to update keypress unsigned long last_send=0;
long last_send=0;
#define MINIMUMSENDDELAY 10 //in milliseconds. minimum delay between serial sends #define MINIMUMSENDDELAY 10 //in milliseconds. minimum delay between serial sends
void setup() { void setup() {
@ -28,7 +27,7 @@ void setup() {
} }
void loop() { void loop() {
long loopmillis=millis(); unsigned long loopmillis=millis();
// ## Sampling and Touch Filtering ## // ## Sampling and Touch Filtering ##
if (micros()-last_adcmicros>=ADCREADINTERVAL) if (micros()-last_adcmicros>=ADCREADINTERVAL)
@ -52,34 +51,30 @@ void loop() {
countedLow[i]++; //increase counter countedLow[i]++; //increase counter
if (countedLow[i]>=COUNTEDLOWTHRESHOLD) { //upper limit if (countedLow[i]>=COUNTEDLOWTHRESHOLD) { //upper limit
countedLow[i]=COUNTEDLOWTHRESHOLD; //upper limit. prevent overflow countedLow[i]=COUNTEDLOWTHRESHOLD; //upper limit. prevent overflow
inputchangeFlag=true; //flag to update keypress
} }
}else{ //released or not pressed hard enough }else{ //released or not pressed hard enough
countedLow[i]=0; //reset counter countedLow[i]=0; //reset counter
inputchangeFlag=true; //flag to update keypress
} }
} }
} }
// ## Calculate Byte ##
byte newTouch=0;
for (uint8_t i=0;i<INPUTS;i++){ //for all keys
if (countedLow[i]>=COUNTEDLOWTHRESHOLD) { //key pressed/touched
newTouch^=1<<i;
}
}
// ## Send to Raspberry ## // ## Send to Raspberry ##
if (inputchangeFlag) { //something changed if (loopmillis-last_send>MINIMUMSENDDELAY) //delay between last send long enough
if (loopmillis-last_send>MINIMUMSENDDELAY) //delay between last send long enough {
{ if (newTouch!=touchOut) { //touched keys have changed
touchOut = 0; //reset bits touchOut=newTouch; //update
for (uint8_t i=0;i<INPUTS;i++){ //for all keys
if (countedLow[i]>=COUNTEDLOWTHRESHOLD) { //key pressed/touched
touchOut^=1<<i;
//Serial.print("1");
}//else{
//Serial.print("0");
//}
}
//Serial.println(touchOut, BIN);
//Serial.println();
Serial.write(touchOut);
last_send=loopmillis; last_send=loopmillis;
inputchangeFlag=false; //clear Flag //Serial.println(touchOut, BIN);
Serial.write(touchOut); //Send byte
} }
} }