bananenkeyboard/touchcontroller/src/main.cpp

98 lines
2.7 KiB
C++
Raw Normal View History

2020-02-21 00:04:30 +00:00
#include "Arduino.h"
#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)
2022-01-15 12:36:55 +00:00
#define TOUCHTHRESHOLD 1000 //below which value input counts as touched. 0<x<1024
#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
2020-01-22 21:34:30 +00:00
unsigned long last_adcmicros=0;
2022-01-15 12:20:49 +00:00
#define ADCREADINTERVAL 2000 //in microseconds. interval to read all adc values
2020-01-22 21:34:30 +00:00
unsigned long last_send=0;
#define MINIMUMSENDDELAY 10 //in milliseconds. minimum delay between serial sends
void setup() {
2020-02-21 00:04:30 +00:00
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
2022-01-15 12:04:58 +00:00
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
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() {
2020-01-22 21:34:30 +00:00
unsigned 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);
2022-01-15 12:20:49 +00:00
/*
Serial.print(rawIn[0]);
2022-01-15 12:04:58 +00:00
Serial.print(", ");
Serial.print(rawIn[1]);
2022-01-15 12:20:49 +00:00
Serial.println();*/
2022-01-15 12:04:58 +00:00
for (uint8_t i=0;i<INPUTS;i++){ //for all inputs
if (rawIn[i]<=TOUCHTHRESHOLD) { //touch detected (input low at this sample)
countedLow[i]++; //increase counter
if (countedLow[i]>=COUNTEDLOWTHRESHOLD) { //upper limit
countedLow[i]=COUNTEDLOWTHRESHOLD; //upper limit. prevent overflow
}
}else{ //released or not pressed hard enough
countedLow[i]=0; //reset counter
}
}
2020-02-21 00:04:30 +00:00
//Serial.print("A0="); Serial.println(rawIn[0]);
}
2020-01-22 21:34:30 +00:00
// ## 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;
}
}
2020-02-21 00:04:30 +00:00
digitalWrite(LED_BUILTIN, newTouch!=0); //Show touch on led
// ## Send to Raspberry ##
2020-01-22 21:34:30 +00:00
if (loopmillis-last_send>MINIMUMSENDDELAY) //delay between last send long enough
{
if (newTouch!=touchOut) { //touched keys have changed
touchOut=newTouch; //update
last_send=loopmillis;
2020-02-21 00:04:30 +00:00
//Serial.println(touchOut, BIN); //Debug output
2022-01-15 12:20:49 +00:00
Serial.write(touchOut); //Send byte
2020-02-13 18:40:33 +00:00
}
}
}