76 lines
2.0 KiB
C++
Executable File
76 lines
2.0 KiB
C++
Executable File
#include <avr/sleep.h>
|
|
const byte LED = 13;
|
|
int sensorPin = A0; // select the input pin for the potentiometer
|
|
int sensorValue = 0; // variable to store the value coming from the sensor
|
|
// keep the state of register ADCSRA
|
|
byte keep_ADCSRA;
|
|
void wake ()
|
|
{
|
|
// cancel sleep as a precaution
|
|
sleep_disable();
|
|
// must do this as the pin will probably stay low for a while
|
|
detachInterrupt (0);
|
|
ADCSRA = keep_ADCSRA;
|
|
} // end of wake
|
|
|
|
void setup ()
|
|
{
|
|
//Serial.begin(9600);
|
|
|
|
digitalWrite (2, HIGH); // enable pull-up
|
|
} // end of setup
|
|
|
|
void loop ()
|
|
{
|
|
|
|
pinMode (LED, OUTPUT);
|
|
// three second LED to confirm start of process:
|
|
digitalWrite (LED, HIGH);
|
|
delay (3000);
|
|
digitalWrite (LED, LOW);
|
|
delay (50);
|
|
// read the value from the sensor:
|
|
sensorValue = analogRead(sensorPin);
|
|
// turn the ledPin on
|
|
digitalWrite(LED, HIGH);
|
|
// stop the program for <sensorValue> milliseconds:
|
|
delay(sensorValue);
|
|
// turn the ledPin off:
|
|
digitalWrite(LED, LOW);
|
|
// stop the program for for <sensorValue> milliseconds:
|
|
delay(sensorValue);
|
|
|
|
pinMode (LED, INPUT);
|
|
keep_ADCSRA = ADCSRA;
|
|
// disable ADC
|
|
ADCSRA = 0;
|
|
//Serial.print("Disable "); Serial.println(analogRead(A0));
|
|
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
|
|
sleep_enable();
|
|
|
|
// Do not interrupt before we go to sleep, or the
|
|
// ISR will detach interrupts and we won't wake.
|
|
noInterrupts ();
|
|
|
|
// will be called when pin D2 goes low
|
|
attachInterrupt (0, wake, LOW);
|
|
|
|
// turn off brown-out enable in software
|
|
// BODS must be set to one and BODSE must be set to zero within four clock cycles
|
|
MCUCR = bit (BODS) | bit (BODSE);
|
|
// The BODS bit is automatically cleared after three clock cycles
|
|
MCUCR = bit (BODS);
|
|
|
|
// We are guaranteed that the sleep_cpu call will be done
|
|
// as the processor executes the next instruction after
|
|
// interrupts are turned on.
|
|
interrupts (); // one cycle
|
|
sleep_cpu (); // one cycle
|
|
|
|
//Serial.print("Enable "); Serial.println(analogRead(A0));
|
|
|
|
} // end of loop
|
|
|
|
|
|
|