Created
September 25, 2018 11:36
-
-
Save rlogiacco/b9c4425830c0f34a8a3f490dfa9f8d4c to your computer and use it in GitHub Desktop.
CapTouch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int ref0, ref1; //reference values to remove offset | |
int ADCTouch_read(byte ADCChannel, int samples) { | |
long _value = 0; | |
for(int _counter = 0; _counter < samples; _counter ++) | |
{ | |
pinMode(ADCChannel, INPUT_PULLUP); | |
analogRead(ADCChannel); | |
/* | |
ADMUX |= 0b11111; | |
ADCSRA |= (1<<ADSC); //start conversion | |
while(!(ADCSRA & (1<<ADIF))); //wait for conversion to finish | |
ADCSRA |= (1<<ADIF); //reset the flag | |
*/ | |
pinMode(ADCChannel, INPUT); | |
_value += analogRead(ADCChannel); | |
} | |
return _value / samples; | |
} | |
void setup() { | |
// No pins to setup, pins can still be used regularly, although it will affect readings | |
Serial.begin(9600); | |
ref0 = ADCTouch_read(A0, 500); //create reference values to | |
ref1 = ADCTouch_read(A1, 500); //account for the capacitance of the pad | |
} | |
void loop() { | |
int value0 = ADCTouch_read(A0, 100); //no second parameter | |
int value1 = ADCTouch_read(A1, 100); // --> 100 samples | |
value0 -= ref0; //remove offset | |
value1 -= ref1; | |
Serial.print(value0 > 40); //send (boolean) pressed or not pressed | |
Serial.print("\t"); //use if(value > threshold) to get the state of a button | |
Serial.print(value1 > 40); | |
Serial.print("\t\t"); | |
Serial.print(value0); //send actual reading | |
Serial.print("\t"); | |
Serial.println(value1); | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment