Hi Guys.
My project is behaving fairly odd and for the life of me, I cant figure out why.
First off, My Code:
int siren = 12;
int beamLed = 11;
int beam = 8; // this is a no pushbutton during the POC phase.
static unsigned long lastBeamTime = 0;
boolean alarmIsActive = false;
int sirenDuration;
void setup()
{
pinMode (siren, OUTPUT);
pinMode (beamLed, OUTPUT);
pinMode (beam, INPUT);
digitalWrite (siren, LOW);
digitalWrite (beamLed, LOW);
digitalWrite (beam, HIGH);
}
void loop()
{
if (alarmIsActive && (millis() - sirenDuration) > 5000)
{
sirenDuration = millis();
alarmIsActive = false;
digitalWrite (siren, LOW);
}
else if (digitalRead(beamLed) == HIGH && digitalRead(beam) == LOW && alarmIsActive && (millis() - sirenDuration) > 5000)
{
alarmIsActive = false;
digitalWrite (siren, LOW);
}
if (digitalRead(beam) == LOW && (millis() - lastBeamTime) > 100)
{
lastBeamTime = millis();
alarmIsActive = true;
sirenDuration = millis();
digitalWrite (siren, HIGH);
digitalWrite (beamLed, HIGH);
}
}
Although this is doing exactly as expected, my problem is that when it hits about the 4th cycle, the pin marked "siren", no longer responds.
Initially I thought that it could be due to debouncing, so I attemted to cater for it.
After that, I though that it could be that the leds were initially run without resistors, but that also didb't solve my issue, Now I simply don't know.
I know my code is horrible, but I'm still learning and I will sanitize it as my understanding is improved, for now, I simply want to know why the system eventually ignores the "siren" OUTPUT even though the input for it is being triggered.
Please let me know if you need any more information required that might assist in the solution for this.
PS. I get the same result on both hardware as well as simulator.
Your help and positive criticism is greatly appreciated.