Howdy, I am fairly new to programming with Arduino, but I am undertaking the project of operating a relay when an external short is detected. I decided to use the SLEEP_MODE_PWR_DOWN functionality in the avr/sleep.h library to save on battery life, essentially waking up the Arduino and activating the relay whenever the interrupt pin detects a signal from the short. However, after implementing the library my code has stopped working consistently. The interrupt pin (3) will activate for a seemingly random number of shorts and then proceed to stop activating when shorted all of a sudden. The digital pin output received when the system is working (short is detected properly) is 0, but the system will return a 1 value on occasion, although this mistake will not always cause the system to stop working. Another clue may be that the interrupt message I programmed to activate whenever the interrupt pin fires can pop up twice, even when the digital pin signal is not displayed. Any help decoding this error would be greatly appreciated.
#include <avr/sleep.h>
int relay = 2; // Relay Control Pin
int floaty = 3; // Sensor/Interrupt pin
int comp; // Comparison Value
void setup()
{
Serial.begin(9600); // Serial for Debuging
pinMode(relay, OUTPUT);
pinMode(floaty, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}
void wakeUp()
{
Serial.println("Interrupt Activated");
sleep_disable();
detachInterrupt(digitalPinToInterrupt(floaty));
}
void Going_To_Sleep()
{
sleep_enable();
attachInterrupt(digitalPinToInterrupt(floaty), wakeUp, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
sleep_cpu();
digitalWrite(LED_BUILTIN, HIGH);
}
void loop()
{
Going_To_Sleep();
comp = digitalRead(floaty);
Serial.print("Reading =");
Serial.println(comp);
delay(500);
if (comp == 1)
{
digitalWrite(relay, HIGH);
delay(5000);
digitalWrite(relay, LOW);
}
else if (comp == 0)
{
delay(500);
}
}