Skip to content

Commit

Permalink
Merge pull request #133 from kai-morich/master
Browse files Browse the repository at this point in the history
Increase ADCtouch sensivity fro 8 to 10 bits
  • Loading branch information
MCUdude committed Jan 19, 2022
2 parents d00d18b + afa8a27 commit f1b7e29
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
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

0 comments on commit f1b7e29

Please sign in to comment.