Hi all,
A few years back I wrote a basic program for an Arduino Uno which reads analog inputs and starts two different events based on these inputs. Besides delays, switches and indicator lights that's all there is to it. It worked for a little while, but there has always been periods where it wouldn't start any of the two events.
Due to the fact it's my first code and I haven't touched it since, i'm having difficulty fault finding. If anyone can take a look over it and tell me why void Ab() and void aB() won't run that would be great. I've established that it isn't a faulty relay through checking if it was being activated on the serial monitor.
I'm an electrician by trade and the program is for a tool I developed for use at work. For anyone familiar with a voltstick or non-contact voltage tester - it's basically two voltsticks attached to a box with a siren. The box is set up attached to cables in a roofspace, and when one of the voltsticks detects power loss (breaker switched off at the switchboard) , an event will start which will send a different tone to the siren via a relay depending on which stick has lost power. They also work individually. Here's a video showing the basic functions.
Some more info..
The two black buttons don't interact with the arduino, they're connected directly to the siren I ripped from a bike horn.
The red button is a reset button for the 10minute delay.
Code Below
// ONE MAN BAND
// dual voltstick siren notification system
int switchA = 13; // the first switch
int switchB = 12; // the second switch
int relay = 11; // the relay controlling siren
int ledA = 10; // the first LED
int ledB = 9; // the second LED
int resetButton = 8; // reset siren button
int valueA; // value of switch A
int valueB; // value of switch B
unsigned long changeTime; //time since reset button pressed
unsigned long delayStart_Ab_Init = 0; // the time the delay started before Ab is triggered
bool delayRunning_Ab_Init = false; // true if still waiting for Ab trigger delay to finish
unsigned long delayStart_Ab = 0; // the time the delay started Ab
bool delayRunning_Ab = false; // true if still waiting for Ab delay to finish
unsigned long delayStart_aB_Init = 0; // the time the delay started before aB is triggered
bool delayRunning_aB_Init = false; // true if still waiting for aB trigger delay to finish
unsigned long delayStart_aB = 0; // the time the delay started aB
bool delayRunning_aB = false; // true if still waiting for aB delay to finish
void setup(){
Serial.begin(9600); // initialize serial communication with computer:
pinMode (switchA,INPUT_PULLUP); // setting switch 1 as input
pinMode (switchB,INPUT_PULLUP); // setting switch 2 as input
pinMode (relay,OUTPUT); // setting relay controlling siren as output
pinMode (ledA,OUTPUT); // setting led A as output
pinMode (ledB,OUTPUT); // setting led B as output
pinMode (resetButton,INPUT); // setting reset button as input
digitalWrite(relay,LOW); // set relay low
digitalWrite(ledA,LOW); // set led A low
digitalWrite(ledB,LOW); // set led B low
}
void loop(){
// establishing analog readings
(void)analogRead(A0);
int grounded1 = analogRead(A0);
(void)analogRead(A1);
int stickA = analogRead(A1);
(void)analogRead(A2);
int grounded2 = analogRead(A2);
(void)analogRead(A3);
int stickB = analogRead(A3);
(void)analogRead(A4);
int grounded3 = analogRead(A4);
(void)analogRead(A5);
int grounded4 = analogRead(A5);
int relayValue = digitalRead(1);
// reset siren button
int state = digitalRead(resetButton);
if(state == HIGH && (millis() - changeTime)> 1000){
delayRunning_Ab = false; // stop delay
delayRunning_aB = false;
}
// led triggering
if (stickA>800) {digitalWrite(ledA, HIGH);}
else {digitalWrite(ledA,LOW);}
if (stickB>800) {digitalWrite(ledB, HIGH);}
else {digitalWrite(ledB,LOW);}
// reset all event delays after 10 minutes
if (delayRunning_Ab && ((millis() - delayStart_Ab) >= 600000)) {
delayRunning_Ab = false; // // prevent this code being run more then once Ab
}
if (delayRunning_aB && ((millis() - delayStart_aB) >= 600000)) {
delayRunning_aB = false; // // prevent this code being run more then once aB
}
// event triggering
// if A is OFF and b is on, start 1 second timer to allow for verification, then start Ab event (RED)
if (delayRunning_Ab_Init && ((millis() - delayStart_Ab_Init) >= 1000)) {
Ab();
}
valueA = digitalRead(switchA);
if ((delayRunning_Ab == false) && (delayRunning_Ab_Init == false) && (stickA<800) && (valueA == 1)) {
delayStart_Ab_Init = millis(); // start timer for function delay Ab initialisation
delayRunning_Ab_Init = true; // start Ab initialisation delay
}
// if a is on and B is OFF, start 1 second timer to allow for verification, then start aB event (BLUE)
if (delayRunning_aB_Init && ((millis() - delayStart_aB_Init) >= 1000)) {
aB();
}
valueB = digitalRead(switchB);
if ((delayRunning_aB == false) && (delayRunning_aB_Init == false) && (stickB<800) && (valueB == 1)) {
delayStart_aB_Init = millis(); // start timer for function delay aB initialisation
delayRunning_aB_Init = true; // start aB initialisation delay
}
// printing to serial monitor
Serial.print(" a ");
Serial.print(stickA);
Serial.print(" b ");
Serial.println(stickB);
Serial.println(relayValue);
delay(50);
}
void Ab() {
delayRunning_Ab_Init = false;
digitalWrite(relay,HIGH);
delay(2000);
digitalWrite(relay,LOW);
delay(1000);
digitalWrite(relay,HIGH);
delay(500);
digitalWrite(relay,LOW);
delay(500);
digitalWrite(relay,HIGH);
delay(500);
digitalWrite(relay,LOW);
delayStart_Ab = millis(); // start timer for function delay Ab
delayRunning_Ab = true; // start delay
changeTime = millis();
return;
}
void aB() {
delayRunning_aB_Init = false;
digitalWrite(relay,HIGH);
delay(2000);
digitalWrite(relay,LOW);
delay(1000);
digitalWrite(relay,HIGH);
delay(500);
digitalWrite(relay,LOW);
delay(500);
digitalWrite(relay,HIGH);
delay(500);
digitalWrite(relay,LOW);
delay(500);
digitalWrite(relay,HIGH);
delay(500);
digitalWrite(relay,LOW);
delayStart_aB = millis(); // start timer for function delay aB
delayRunning_aB = true; // start delay
changeTime = millis();
return;
}