From 026937393bb45084a0fa33a643d3411ff5c1be63 Mon Sep 17 00:00:00 2001 From: Fisch Date: Wed, 22 Jan 2020 21:43:53 +0100 Subject: [PATCH] add touchcontroller code for arduino pro mini --- touchcontroller/touchcontroller.ino | 87 +++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 touchcontroller/touchcontroller.ino diff --git a/touchcontroller/touchcontroller.ino b/touchcontroller/touchcontroller.ino new file mode 100644 index 0000000..0874cd3 --- /dev/null +++ b/touchcontroller/touchcontroller.ino @@ -0,0 +1,87 @@ + +#define INPUTS 8 +byte touchOut=0; //binary encoded sensors +uint16_t rawIn[INPUTS]; + +uint16_t countedLow[INPUTS]; //count the times input was below threshold (touched) +#define TOUCHTHRESHOLD 300 //below which value input counts as touched +#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; +#define ADCREADINTERVAL 2000 //in microseconds. interval to read all adc values + +boolean inputchangeFlag; //flag to update keypress +long last_send=0; +#define MINIMUMSENDDELAY 10 //in milliseconds. minimum delay between serial sends + +void setup() { + pinMode(A0, INPUT_PULLUP); + pinMode(A1, INPUT); + pinMode(A2, INPUT_PULLUP); + pinMode(A3, INPUT_PULLUP); + pinMode(A4, INPUT_PULLUP); + pinMode(A5, INPUT_PULLUP); + pinMode(A6, INPUT_PULLUP); + pinMode(A7, INPUT_PULLUP); + Serial.begin(115200); +} + +void loop() { + long loopmillis=millis(); + + // ## Sampling and Touch Filtering ## + if (micros()-last_adcmicros>=ADCREADINTERVAL) + { + last_adcmicros=micros(); + //Sample all analog Inputs + rawIn[0]=analogRead(A0); + rawIn[1]=analogRead(A1); + rawIn[2]=analogRead(A2); + rawIn[3]=analogRead(A3); + rawIn[4]=analogRead(A4); + rawIn[5]=analogRead(A5); + rawIn[6]=analogRead(A6); + rawIn[7]=analogRead(A7); + //Serial.print(analogRead(A1)); + //Serial.print(": "); + //Serial.println(micros()-mic); + + for (uint8_t i=0;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 + } + } + + } + + // ## 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=COUNTEDLOWTHRESHOLD) { //key pressed/touched + touchOut^=1<