I have been using the Arduino and a seeed studio relay shield to switch on 120V AC outlets and a solenoid for a specified interval when a button is pushed. It was working great for awhile then suddenly the outlets and solenoid started "freaking out" and would cycle on and off really fast. I tried re-downloading the program to the board and also tried an AC noise filter on the outlet it was plugged into with no effect. I suspect there is something wrong with the code, maybe a buffer overrun or something of the sort, but I cannot seem to figure it out. Any help would be appreciated.
I have attached the electrical diagram for reference.
const int Sol1= 12; //Solenoid output
const int button=8; //Button input
const int A_OUT_PIN=4; // Relay Shield control A
const int B_OUT_PIN=5; // Relay Shield control B
const int C_OUT_PIN=6; // Relay Shield control C
const int D_OUT_PIN=7; // Relay Shield contorl D
int buttonpushcounter = 0;
int buttonstate=0;
int lastbuttonstate = 0;
int solstate; // state of solenoid high/low
unsigned long previousmillis = 0;
const long interval=90000; //Time interval
unsigned long currentmillis = millis();
void setup() {
pinMode ( A_OUT_PIN , OUTPUT ); // To Relay Shield control A
pinMode ( B_OUT_PIN , OUTPUT ); // To Relay Shield control B
pinMode ( C_OUT_PIN , OUTPUT ); // To Relay Shield control C
pinMode ( D_OUT_PIN , OUTPUT ); // To Relay Shield control D
pinMode(Sol1,OUTPUT); // Output to solenoid
pinMode(button,INPUT_PULLUP);
Serial.begin(9600);}
void loop() {
buttonstate = digitalRead(button); // start counting the button pushes
if (buttonstate!=lastbuttonstate){
if (buttonstate == HIGH) {
buttonpushcounter++;
if (buttonpushcounter%2==1) { // sets time based on odd # of button pushes
currentmillis = millis();
Serial.print("number of button pushes: ");
Serial.println(buttonpushcounter);}}
delay(50);}
lastbuttonstate=buttonstate;
if (buttonpushcounter%2==1 && millis()-currentmillis <= interval) { // ouputs signal to relay sheild and solenoid within 90s time interval
solstate=HIGH;}
else {
solstate=LOW;}
digitalWrite(Sol1,solstate);
digitalWrite(A_OUT_PIN,solstate);
digitalWrite(B_OUT_PIN,solstate);
digitalWrite(C_OUT_PIN,solstate);
digitalWrite(D_OUT_PIN,solstate);
Serial.flush();}
