Need help with simple code

Hi,

I am tying to replace a time delayed relay with a arduino uno and a relay bank.

I have the relays hooked up to digital out puts 11 and 12.
And a button on digital input 2.

I can get the code to work the relays with the button, even with delay in the code it works.

What i need is when the button is pressed and held, both relays read low then a time delay of 1sec, then one relay drops to high and stays that way untill the button is lifted.

When the button is lifted both relays need to read high.

When button pressed and held again, the first step is repeated.

Below is the code of what i am trying to use, but does work correctly.

const int buttonPin = 2;
const int relay1 = 12;
const int relay2 = 11;
int buttonState = 0;

void setup() {
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
delay(1000);
digitalWrite(relay1,HIGH);
}
else {
digitalWrite(relay1, HIGH);
digitalWrite(relay2,HIGH);
}
}

Below is the code of what i am trying to use, but does work correctly

So what is it doing incorrectly?

Relay1 will not go high after delay.
Stays in low position.

You will need to change the pin numbers and the HIGH/LOW tests and outputs to match your hardware but try this

const byte buttonPin = A1;
const byte output1 = 10;
const byte output2 = 11;
byte currentButtonState = HIGH;
byte previousButtonState = HIGH;
boolean waiting = false;
unsigned long startTime = 0;
unsigned long period = 1000;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(output1, OUTPUT);
  pinMode(output2, OUTPUT);
}

void loop()
{
  currentButtonState = digitalRead(buttonPin);
  if (currentButtonState != previousButtonState && currentButtonState == LOW)
  {
    waiting = true;
    startTime = millis();
    digitalWrite(output1, LOW);
    digitalWrite(output2, LOW);
  }
  else if (currentButtonState == HIGH)
  {
    digitalWrite(output1, HIGH);
    digitalWrite(output2, HIGH);
  }
  if (waiting && millis() - startTime >= period)
  {
    digitalWrite(output1, HIGH);
  }
  previousButtonState = currentButtonState;
}

As i can understand from your description the function can be described by the attached state diagram. In that case the diagram results in the following code when using my state machine library

#include <SM.h>

SM relayTimer(bothHigh);
const byte buttonPin = A1;
const byte output1 = 10;
const byte output2 = 11;

void setup(){
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(output1, OUTPUT);
  pinMode(output2, OUTPUT);
  digitalWrite(output1, 1);
  digitalWrite(output2, 1);
  Serial.println("ready");
}//setup()

void loop(){
  EXEC(relayTimer);
}//loop()

State bothHigh(){
  if(!digitalRead(buttonPin)){
    relayTimer.Set(bothLow);
    Serial.println("btn activated");
    digitalWrite(output1, 1);
    digitalWrite(output2, 1);
  }//if(button pressd)
}//bothHigh()

State oneLow(){
  if(digitalRead(buttonPin)){
    Serial.println("btn relased");
    digitalWrite(output1, 1);
    digitalWrite(output2, 1);  
    relayTimer.Set(bothHigh);
  }//if(btn released)
}//oneLow()

State bothLow(){
  if(relayTimer.Timeout(1000)){
    Serial.println("1s");
    relayTimer.Set(oneLow);
    digitalWrite(output2, 1);
  }//if(Timeout)
  if(digitalRead(buttonPin)){
    Serial.println("btn relased");
    digitalWrite(output1, 1);
    digitalWrite(output2, 1);  
    relayTimer.Set(bothHigh);
  }//if(btn released)
}//bothLow()

dual relay timer stae diagram.png

That code works ok "UKhelibob"

But, the button push works opposite to what i was hoping.

When the button is depressed the relays should be on.

When the button i depressed the code has the relays off, and undepressed the relays on and the one turns off with the delay.

Any idea why?

Fixed it, Thatnks for the help. Much appreciated

const byte buttonPin = A1;
const byte output1 = 10;
const byte output2 = 11;
byte currentButtonState = HIGH;
byte previousButtonState = HIGH;
boolean waiting = false;
unsigned long startTime = 0;
unsigned long period = 1000;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(output1, OUTPUT);
  pinMode(output2, OUTPUT);
}

void loop()
{
  currentButtonState = digitalRead(buttonPin);
  if (currentButtonState != previousButtonState && currentButtonState == HIGH)
  {
    waiting = true;
    startTime = millis();
    digitalWrite(output1, LOW);
    digitalWrite(output2, LOW);
  }
  else if (currentButtonState == LOW)
  {
    digitalWrite(output1, HIGH);
    digitalWrite(output2, HIGH);
  }
  if (waiting && millis() - startTime >= period)
  {
    digitalWrite(output1, HIGH);
  }
  previousButtonState = currentButtonState;
}

But, the button push works opposite to what i was hoping.

Well, I did say

You will need to change the pin numbers and the HIGH/LOW tests

I am glad that you got it working.