Switching on outlets

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();}

Your button wiring is weird. You're using the internal pull-up, so just connect one side of the switch to ground and get rid of R1 & R2.

It also seems strange that you never reset buttonpushcounter, although it's probably OK if it counts forever 'till it rolls-over.

The protection diode on the solenoid isn't wired right either ([u]example[/u]).

What's the coil rating on those relays? It's unusual to find a 120VAC relay that can be driven directly by the Arduino. And your relay coils need protection diodes too.

You might add a few more Serial.print messages so you can "see" what the program is doing.

thanks for the reply. The relays are on a relay shield designed to directly attach to the arduino board.

I'm not sure if this is your problem but you need to be careful
about which operation execute first.

Your code:

(buttonpushcounter%2==1 && millis()-currentmillis <= interval)

Probably should be:

( (buttonpushcounter%2==1 ) && ( ( millis()-currentmillis ) <= interval ) )

I may have gotten a couple extra () in there but I think this is what you
want to be telling it to do.
I don't think:

( ( buttonpushcounter%2==1 && millis() ) - ( currentmillis <= interval ) )

is what you want it to do but is what is you told it to do
Dwight

Thanks Dwight, I tried out your change and the unit seems to be functioning normally now. This problem has been off and on for awhile so hopefully this was the fix and not just coincidence.

Hi,
Does your project run continuously 24hr/7days a week without the Arduino being turned on and off?

millis is declared as unsigned long, it will eventually rollover from 4,294,967,295 to 0.
This happens after about 50days.

Tom... :slight_smile:

I don't think the overflow is a problem. He only looks at the value
if he is running it for 90 minutes, otherwise he doesn't care.

Unless he needs a count of switches, he might be better
using a bool value instead of an int, since it only needs two
states for buttonpushcounter.
It is silly to do modulo math when the state is either true of false.
Just my opinion.
Dwight