The problem is solved
I simply need to uncomment the attachInterrupt line in setup(). The interrupt can wake up teensy.
Also, documentation on sleep modes can be found on page 51 of http://www.pjrc.com/teensy/at90usb1286.pdf
Dear Paul and friends,
I am trying to run Teensy on battery and I hope to make the battery last as long as possible.
My plan is to have Teensy wake up on an external interrupt signal. I can reliably detect the external interrupt signal (please see more info at the end of the post), but I don't know how to write the code to properly put Teensy into and out of sleep mode.
More specifically, I don't know how to set up Teensy with the interrupt port I wish to wake it up with.
Below is the code I got from http://www.pjrc.com/teensy/low_power.html . Maybe I'm just missing a line? I have also put some questions inline.
As you can see, I am a little clueless about how sleep mode works. I would love to be pointed to an external reference where I can learn more about it.
Thanks for your help!
Huan
I am using Teensy++ 2.0. I have tested the external interrupt with the following code:
The external interrupt is set up with a pull up resistor, and I am detecting the falling signal. INPUT in the diagram is connected to INT2 (D2) on Teensy++ 2.0 .
I simply need to uncomment the attachInterrupt line in setup(). The interrupt can wake up teensy.
Also, documentation on sleep modes can be found on page 51 of http://www.pjrc.com/teensy/at90usb1286.pdf
Dear Paul and friends,
I am trying to run Teensy on battery and I hope to make the battery last as long as possible.
My plan is to have Teensy wake up on an external interrupt signal. I can reliably detect the external interrupt signal (please see more info at the end of the post), but I don't know how to write the code to properly put Teensy into and out of sleep mode.
More specifically, I don't know how to set up Teensy with the interrupt port I wish to wake it up with.
Below is the code I got from http://www.pjrc.com/teensy/low_power.html . Maybe I'm just missing a line? I have also put some questions inline.
As you can see, I am a little clueless about how sleep mode works. I would love to be pointed to an external reference where I can learn more about it.
Thanks for your help!
Huan
Code:
const int ledPin = 6;
const int wakeupPin = 2;
volatile int ledState = LOW;
volatile int ledVal;
void wakeUp()
{
Serial.println("in wakeUp()");
ledState = HIGH;
Serial.println(ledState);
Serial.println("Turn on");
digitalWrite(ledPin, HIGH);
delay(5000);
Serial.println("Turn off");
digitalWrite(ledPin, LOW);
}
void setup()
{
Serial.begin(38400);
pinMode(ledPin, OUTPUT);
pinMode(wakeupPin, INPUT);
//attachInterrupt(wakeupPin, wakeUp, FALLING); //LOW,RISING, FALLING,CHANGE
ledVal=0;
digitalWrite(ledPin, ledVal);
Serial.println("done init");
}
#include <avr/sleep.h>
void powerdown() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
Serial.println("bye world!");
delay(1000);
Serial.end(); // shut off USB
ADCSRA = 0; // shut off ADC
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // QUESTION: what about PWR_SAVE mode?
noInterrupts(); // QUESTION: are we disabling the interrupts so that the powerdown procedure can finish?
sleep_enable();
interrupts(); // QUESTION: how do I specify which port? can I specify multiple ports?
sleep_cpu(); // QUESTION: does teensy go to sleep here?
sleep_disable(); // QUESTION: why do we clear the sleep bit here?
// QUESTION: will the code below ever get executed?
Serial.begin(38400);
delay(1000);
Serial.println("hello world!");
delay(1000);
digitalWrite(ledPin, HIGH);
delay(1000);
}
void loop()
{
powerdown();
}
I am using Teensy++ 2.0. I have tested the external interrupt with the following code:
Code:
const int ledPin = 6;
const int wakeupPin = 2;
volatile int ledState = LOW;
void wakeUp()
{
Serial.println("in wakeUp()");
Serial.println("Turn on");
digitalWrite(ledPin, HIGH);
delay(5000);
Serial.println("Turn off");
digitalWrite(ledPin, LOW);
}
volatile int ledVal;
void setup()
{
Serial.begin(38400);
pinMode(ledPin, OUTPUT);
pinMode(wakeupPin, INPUT);
attachInterrupt(wakeupPin, wakeUp, FALLING); //LOW,RISING, FALLING,CHANGE
ledVal=0;
digitalWrite(ledPin, ledVal);
Serial.println("done init");
}
The external interrupt is set up with a pull up resistor, and I am detecting the falling signal. INPUT in the diagram is connected to INT2 (D2) on Teensy++ 2.0 .
Last edited: