#include 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 () { 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 milliseconds: delay(sensorValue); // turn the ledPin off: digitalWrite(LED, LOW); // stop the program for for milliseconds: delay(sensorValue); pinMode (LED, INPUT); keep_ADCSRA = ADCSRA; // disable ADC ADCSRA = 0; 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 } // end of loop