Good day, Ms. and Mr.,
I am using an Arduino R3 board with ATMEGA 328P microcontroller.
I am new to this forum and I would like to seek help. I have used a code that uses 2 push buttons. 1 button is for sleep mode, and the other is for wake-up. I wish to know how to connect the wake-up sequence to the main code so that the machine would start again.
int topleft;
int topright;
int downleft;
int downright;
int waittime = 1;
#include "Arduino.h"
#include <avr/sleep.h>
#define sleepPin 8 // When low, makes 328P go to sleep
#define wakePin 3 // when low, makes 328P wake up, must be an interrupt pin (2 or 3 on ATMEGA328P)
void setup() {
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
TCCR1A = 0;
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
TCCR1B = 0;
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
ICR1 = 40000;
OCR1A = 4000;
OCR1B = 4750;
Serial.begin(9600);
// Keep pins high until we ground them
pinMode(sleepPin, INPUT_PULLUP);
pinMode(wakePin, INPUT_PULLUP);
Serial.println("Setup completed.");
}
void loop() {
topleft = analogRead(A0);
topright = analogRead(A1);
downleft = analogRead(A2);
downright = analogRead(A3);
if (topleft > topright) {
OCR1A = OCR1A + 1;
delay(waittime);
}
if (downleft > downright) {
OCR1A = OCR1A + 1;
delay(waittime);
}
if (topleft < topright) {
OCR1A = OCR1A - 1;
delay(waittime);
}
if (downleft < downright) {
OCR1A = OCR1A - 1;
delay(waittime);
}
if (OCR1A > 4000) {
OCR1A = 4000;
}
if (OCR1A < 2000) {
OCR1A = 2000;
}
if (topleft > downleft) {
OCR1B = OCR1B - 1;
delay(waittime);
}
if (topright > downright) {
OCR1B = OCR1B - 1;
delay(waittime);
}
if (topleft < downleft) {
OCR1B = OCR1B + 1;
delay(waittime);
}
if (topright < downright) {
OCR1B = OCR1B + 1;
delay(waittime);
}
if (OCR1B > 4200) {
OCR1B = 4200;
}
if (OCR1B < 3000) {
OCR1B = 3000;
}
// Is the "go to sleep" pin now LOW?
if (digitalRead(sleepPin) == LOW) {
// Disable the ADC (Analog to digital converter, pins A0 [14] to A5 [19])
static byte prevADCSRA = ADCSRA;
ADCSRA = 0;
*/
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
MCUCR = bit (BODS) | bit (BODSE);
// The BODS bit is automatically cleared after three clock cycles so we better get on with it
MCUCR = bit (BODS);
noInterrupts();
attachInterrupt(digitalPinToInterrupt(wakePin), sleepISR, LOW);
// Send a message just to show we are about to sleep
Serial.println("Good night!");
Serial.flush();
// Allow interrupts now
interrupts();
// And enter sleep mode as set above
sleep_cpu();
// --------------------------------------------------------
// µController is now asleep until woken up by an interrupt
// --------------------------------------------------------
// Wakes up at this point when wakePin is brought LOW - interrupt routine is run first
Serial.println("I'm awake!");
// Re-enable ADC if it was previously running
ADCSRA = prevADCSRA;
}
}
// When wakePin is brought LOW this interrupt is triggered FIRST (even in PWR_DOWN sleep)
void sleepISR() {
// Prevent sleep mode, so we don't enter it again, except deliberately, by code
sleep_disable();
// Detach the interrupt that brought us out of sleep
detachInterrupt(digitalPinToInterrupt(wakePin));
}