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.
// 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
boolean inputchangeFlag; //flag to update keypress
long last_send=0;
unsigned long last_send=0;
#define MINIMUMSENDDELAY 10 //in milliseconds. minimum delay between serial sends
void setup() {
@ -28,7 +27,7 @@ void setup() {
}
void loop() {
long loopmillis=millis();
unsigned long loopmillis=millis();
// ## Sampling and Touch Filtering ##
if (micros()-last_adcmicros>=ADCREADINTERVAL)
@ -52,34 +51,30 @@ void loop() {
countedLow[i]++; //increase counter
if (countedLow[i]>=COUNTEDLOWTHRESHOLD) { //upper limit
countedLow[i]=COUNTEDLOWTHRESHOLD; //upper limit. prevent overflow
inputchangeFlag=true; //flag to update keypress
}
}else{ //released or not pressed hard enough
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 ##
if (inputchangeFlag) { //something changed
if (loopmillis-last_send>MINIMUMSENDDELAY) //delay between last send long enough
{
touchOut = 0; //reset bits
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);
if (loopmillis-last_send>MINIMUMSENDDELAY) //delay between last send long enough
{
if (newTouch!=touchOut) { //touched keys have changed
touchOut=newTouch; //update
last_send=loopmillis;
inputchangeFlag=false; //clear Flag
//Serial.println(touchOut, BIN);
Serial.write(touchOut); //Send byte
}
}