Hey guys im having an issue with my code. starting off my uno enters power down sleep after the short delay. If i press the downbutton (pin2) uno wakes up and relay 3/4 come on like normal but relay 1 only stays on about 1 second even tho the delay says 5. uno goes back to sleep like i want then i hit the upbutton(pin3) and relay 2 turns on with 5 second delay. uno goes back to sleep but how do i keep the loop going, my interrupt buttons will not wake the uno from this point on, I need to reload the sketch to reset. Thanks in advance for the help.
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/io.h>
// constants won't change.
const int downbutton = 2; // downbutton to pin 2
const int upbutton = 3; // upbutton to pin 3
const int relay1 = 7; // relay1 to pin
const int relay2 = 6; // relay2 to pin
const int relay3 = 5; // relay3 to pin
const int relay4 = 4; // relay4 to pin
// variables will change:
int buttonState2 = 0; // variable for reading the pushbutton status
int buttonState3 = 0;
void setup(){
DDRD &= B00000011; // set Arduino pins 2 to 7 as inputs, leaves 0 & 1 (RX & TX) as is
DDRB = B00000000; // set pins 8 to 13 as inputs
PORTD |= B11111100; // enable pullups on pins 2 to 7
PORTB |= B11111111; // enable pullups on pins 8 to 13
pinMode(13,OUTPUT); // set pin 13 as an output so we can use LED to monitor
digitalWrite(13,HIGH); // turn pin 13 LED on
pinMode(upbutton, INPUT); // sets the downbutton pin as input
pinMode(downbutton, INPUT); // sets the downbutton pin as input
pinMode(relay1, OUTPUT); // sets the relay pin as output
pinMode(relay2, OUTPUT); // sets the relay pin as output
pinMode(relay3, OUTPUT); // sets the relay pin as output
pinMode(relay4, OUTPUT); // sets the relay pin as output
digitalWrite(relay1, LOW); // Prevents relays from starting up engaged
digitalWrite(relay2, LOW); // Prevents relays from starting up engaged
digitalWrite(relay3, LOW); // Prevents relays from starting up engaged
digitalWrite(relay4, LOW); // Prevents relays from starting up engaged
}
void loop(){
// Stay awake for 1 second, then sleep.
// LED turns off when sleeping, then back on upon wake.
delay(5000);
sleepNow();
}
//
void sleepNow(void)
{
// Set pin 2/3 as interrupt and attach handler:
attachInterrupt(0, pinInterrupt, HIGH);
attachInterrupt(1, pinInterrupt, HIGH);
delay(500);
//
// Choose our preferred sleep mode:
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
//
// Set sleep enable (SE) bit:
sleep_enable();
//
// Put the device to sleep:
digitalWrite(13,LOW); // turn LED off to indicate sleep
sleep_mode();
//
// Upon waking up, sketch continues from this point.
sleep_disable();
digitalWrite(13,HIGH); // turn LED on to indicate awake
}
//
void pinInterrupt(void)
{
detachInterrupt(0);
detachInterrupt(1);
buttonState3 = digitalRead(upbutton); // read the state of the upbutton value
buttonState2 = digitalRead(downbutton); // read the state of the downbutton value
// check whether a pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState2 == HIGH) {
// turn relay 1 on:
digitalWrite(relay3, HIGH); digitalWrite(relay4, HIGH); digitalWrite(relay1, HIGH);
// wait for 5 seconds
delay(5000);
}
if (buttonState2 == LOW || buttonState3 == LOW) {
// relay 1,2 off
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
}
if (buttonState3 == HIGH) {
// turn relay 2 on:
digitalWrite(relay3, LOW); digitalWrite(relay4, LOW); digitalWrite(relay2, HIGH);
// wait for 5 seconds
delay(5000);
}
}