Thanks for the response.
What I'm trying to do is use a single pin to wake up the device from deep sleep and then use the same pin for general input once the device is awake and runs the code. This works with Arduino (avr chips) via attachInterrupt and detachInterrupt. Take a look at the sample below taken from a site and modified slightly.
Within the main loop, a buttonPin is assigned to pin 2 that triggers a led change before the device goes into deep sleep. Within the function instructing the device to sleep(Going_To_Sleep()), the same Pin (2) is then attached as an interrupt before the device sleeps. The same pin (2) wakes the device from sleep and then the pin (2) is immediately detached as an interrupt before running the main loop. That same pin (2) is then used as buttonPin1 within the loop function before the cycle repeats.
So once the device initially starts, it will put itself to sleep. The current draw decreases appropriately. Once I press the button, the device wakes up, prints to serial that the button is pressed and ledPin1 is then high for 1 second. The device then goes back to sleep and the process repeats.
Does the deep sleep library in CircuitPython have a similar detach function? Or is there some other way to achieve the same?
Code: Select all
/**
* Modified from: http://www.thearduinomakerman.info/blog/2018/1/24/guide-to-arduino-sleep-mode
*/
#include <avr/sleep.h>
byte interruptPin1 = 2; //Pin used to wake up the Arduino
const int ledPin1 = 8;
void setup() {
Serial.begin(115200);//Start Serial Comunication
pinMode(LED_BUILTIN,OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(interruptPin1,INPUT_PULLUP); //Probably not necessary...Set pin d2 to input
}
void loop() {
int buttonPin1 = 2;
pinMode(buttonPin1, INPUT_PULLUP);
Serial.println("Pin 2 changed");
int buttonState1 = 0;
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == LOW)
{
// turn LED on:
Serial.println("Button pressed!");
digitalWrite(ledPin1, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
}
else {
// turn LED off:
digitalWrite(ledPin1, LOW);
Serial.println("Button not pressed");
}
Serial.println("Going to sleep in 2...");
led_flash();
Going_To_Sleep();
}
void Going_To_Sleep()
{
sleep_enable();
attachInterrupt(0, wakeUp, LOW); //interrupt to pin d2 (Int.0=pin2 on an uno)
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Setting the sleep mode to full sleep
sleep_cpu(); //activating sleep mode
Serial.println("just woke up!"); //next line of code executed after the interrupt
digitalWrite(LED_BUILTIN, HIGH);//turning LED on
}
void wakeUp(){
Serial.println("Interrrupt Fired");//Print message to serial monitor
sleep_disable();//Disable sleep mode
detachInterrupt(0); //Removes the interrupt (Int.0=pin2 on an uno)
}
void led_flash()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
}