Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

increase ADCtouch sensivity with 10 instead of 8 bit resolution #133

Merged
merged 1 commit into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion avr/libraries/ADCTouch/examples/Touch_read/Touch_read.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
|***********************************************************************/

#include <ADCTouch.h>
#include <EEPROM.h>

// Sample each touch pin 32 times
const uint16_t ADCTouch::samples = 32;
Expand All @@ -39,11 +40,14 @@ int16_t ref_A3;

// Touch threshold for turning on or off the LEDs
// Lower is more sensitive
const uint8_t threshold = 20;
const uint8_t threshold = 50;


void setup()
{
uint8_t cal = EEPROM.read(0);
if (cal < 0x80)
OSCCAL = cal;
Serial.begin();

// Reference values to account for the capacitance of the touch pad
Expand All @@ -57,6 +61,16 @@ void loop()
int16_t val_A2 = Touch.read(A2);
int16_t val_A3 = Touch.read(A3);

// Drift compensation
if(val_A2 > ref_A2)
ref_A2++;
else
ref_A2 = (val_A2 + ref_A2)/2;
if(val_A3 > ref_A3)
ref_A3++;
else
ref_A3 = (val_A3 + ref_A3)/2;

// Remove offset
val_A2 -= ref_A2;
val_A3 -= ref_A3;
Expand All @@ -65,11 +79,15 @@ void loop()
Serial.print(val_A2 > threshold); // Send (boolean) pressed or not pressed
Serial.print(F(", A2 touch value: "));
Serial.print(val_A2);
Serial.print(F(", A2 ref value: "));
Serial.print(ref_A2);

Serial.print(F("\tA3 touch bool: "));
Serial.print(val_A3 > threshold); // Send (boolean) pressed or not pressed
Serial.print(F(", A3 touch value: "));
Serial.print(val_A3);
Serial.print(F(", A3 ref value: "));
Serial.print(ref_A3);
Serial.print('\n');
delay(100);
}
4 changes: 2 additions & 2 deletions avr/libraries/ADCTouch/src/ADCTouch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int16_t ADCTouch::read(const analog_pin_t adc_channel)
uint8_t digitalPin = pgm_read_byte(analog_pin_to_digital_pin + adc_channel);
do
{
ADMUX = (1 << ADLAR) | adc_channel; // Select ADC channel
ADMUX = adc_channel; // Select ADC channel
DDRB |= (1 << digitalPin); // Discharge touchpad

ADCSRA |= (1 << ADEN); // Enable ADC & discharge S/H cap
Expand All @@ -42,7 +42,7 @@ int16_t ADCTouch::read(const analog_pin_t adc_channel)

ADCSRA |= (1 << ADSC); // Start ADC conversion
while (bit_is_set(ADCSRA, ADSC)); // Wait for conversion to complete
level += ADCH;
level += ADCW;
ADCSRA = 0; // ADC off
}
while(--samples_left);
Expand Down