I am building a project that requires sleep modes and is controlled by capacitive touch sensing pins. My goal is to have no moving parts (ie. pushbuttons, slide switches, etc). I have used the Capacitive Sensor library but I moved to using ADCTouch since it requires no hardware. I can initialize sleep using the sensor pin attached to A0 but the chip immediately tries to turn back on and seems to stall out at this point.
#include <avr/sleep.h>
#include <avr/power.h>
#include <ADCTouch.h>
int LEDPin = 4;
int pwrPin = 2;
int sensorPin0 = A0; //Enter, or sleep
int sensorPin1 = A1; //Down
int sensorPin2 = A2; //Up
int maxModes = 3;
int mode = 3;
int count = 0;
int ref0, ref1, ref2; //reference values to remove offset
void setup()
{
Serial.begin(9600);
Serial.println("Turning on.");
ref0 = ADCTouch.read(sensorPin0, 500); //create reference values to ..
ref1 = ADCTouch.read(sensorPin1, 500);
ref2 = ADCTouch.read(sensorPin2, 500);
pinMode(pwrPin,INPUT);
digitalWrite(pwrPin,HIGH);
pinMode(LEDPin,OUTPUT);
digitalWrite(LEDPin,HIGH);
// pin change interrupt (example for A0)
PCMSK1 |= bit (PCINT8); // want pin A0
PCIFR |= bit (PCIF1); // clear any outstanding interrupts
PCICR |= bit (PCIE1);
}
ISR (PCINT1_vect)
{
wakeUpNow();
}
void sleepNow()
{
sleep_enable();
set_sleep_mode(SLEEP_MODE_IDLE);
Serial.println("Going to sleep");
digitalWrite(LEDPin,LOW);
delay(1000);
attachInterrupt(0, wakeUpNow, LOW); //for debugging, there is a pushbutton that will also wake the chip.
sleep_cpu(); // Go to sleep
sleep_disable(); // Return from sleep
}
void wakeUpNow(){
detachInterrupt(0);
cli;
wakeUpMsg();
}
void wakeUpMsg(){
delay (00);
Serial.println("Waking up.");
Serial.print("Now on mode ");
Serial.println(mode);
sei;
}
void loop(){
int value0 = ADCTouch.read(sensorPin0); //no second parameter
int value1 = ADCTouch.read(sensorPin1);
int value2 = ADCTouch.read(sensorPin2);
value0 -= ref0; //remove offset
value1 -= ref1;
value2 -= ref2;
if (value0 > 60){
digitalWrite(LEDPin,LOW);//Flash to indicate button recognition
delay(100);
digitalWrite(LEDPin,HIGH);
delay(100);
count ++;
}
if ((value1 > 40) && (mode == 0)) { //mode down
mode = maxModes;
Serial.println("Mode Down");
Serial.print("Now on mode ");
Serial.println(mode);
delay (200);
}
else if ((value1 > 40) && (mode > 0)) {
mode --;
Serial.println("Mode Down");
Serial.print("Now on mode ");
Serial.println(mode);
delay (200);
}
if ((value2 > 40) && (mode == maxModes)) { //mode down
mode = 0;
Serial.println("Mode Up");
Serial.print("Now on mode ");
Serial.println(mode);
delay (200);
}
else if ((value2 > 40) && (mode < maxModes)) {
mode ++;
Serial.println("Mode Up");
Serial.print("Now on mode ");
Serial.println(mode);
delay (200);
}
if (count == 10){
Serial.println("Trying to sleep.");
sleepNow();
count = 0;
}
if ((value0 < 60) && (count < 10)){
count = 0;
}
switch (mode) {
case 0:
digitalWrite(LEDPin,LOW);
delay (1000);
digitalWrite(LEDPin,HIGH);
delay (1000);
break;
case 1:
digitalWrite(LEDPin,LOW);
delay (500);
digitalWrite(LEDPin,HIGH);
delay (500);
break;
case 2:
digitalWrite(LEDPin,LOW);
delay (200);
digitalWrite(LEDPin,HIGH);
delay (200);
break;
case 3:
digitalWrite(LEDPin,LOW);
delay (50);
digitalWrite(LEDPin,HIGH);
delay (50);
break;
}
digitalWrite(LEDPin,HIGH);
}
The loop in this code handles several LED flash modes controlled by A1 and A2. I have pin 2 connected to a pushbutton right now for debugging purposes. void setup is where I try to initialize pin A0 as an interrupt, and right below that I have the ISR call on the wake command. I followed Nick Gammon's instructions the best I could but I seem to be either messing up somewhere, or it isn't possible to wake up using a capsense pin (the former I believe).
I am aware of the Atmel touch sensor chip, and I am prepared to resort to that, but if this can be done without using extra harware, I would be a happy camper. Any help would be greatly appreciated.
Thank you,
Atrius129
