ON(LOW) in 1 second and then OFF(HIGH), using delay. anyone can help?

Hey guys, i just want to know about my code using delay instead of millis, my case 4 relay, one of them must be off after 1 second. my code here.

/*Multiple state handling
*/
#define capSwitch D4 //tombol
#define relay1 D0
#define relay2 D1
#define relay3 D2
#define relay4 D3

int state = 0;
int old = 0;
int buttonPoll = 0;

void setup() {

pinMode(capSwitch,INPUT);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
pinMode(relay3,OUTPUT);
pinMode(relay4,OUTPUT);

digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);

}

void loop() {

buttonPoll = digitalRead(capSwitch);
if(buttonPoll == 1){
delay(50);
buttonPoll = digitalRead(capSwitch);
if(buttonPoll == 0){
state = old + 1;
}}
else{
delay(100);
}
switch (state) {
case 1: //on
digitalWrite(relay1,LOW);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
old = state;
break;
case 2: //acc
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(relay3,LOW);
digitalWrite(relay4,HIGH);
old = state;
break;
case 3: //stater
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(relay3,LOW);
if (digitalWrite(relay2) == LOW) {
digitalWrite(relay4,LOW); //this must HIGH after 1000ms
delay(500);
digitalWrite(relay4,HIGH);
}
old = state;
break;
default: //posisi mati/off
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
old = 0;
break;
}
}

start-stop-ignition.ino (1.53 KB)

Do you have a question?

“ using delay instead of millis,” ???

We never use delay here :wink:

So sorry i'm new in arduino, can anyone help for my code, in my code case 3 relay1,2,3 and 4 should ON if press button 3 times, and i want to make relay 4 auto OFF after 1 second ON, and i don't know how to use millis and insert it to my code, using delay is looping that relay, and error compiling. i don't know how :frowning:

What do you think is happening here?

if (digitalWrite(relay2) == LOW)

How is ‘capSwitch’ wired?

To be tested.

What do you not understand?

// Multiple state handling
//Version 1.00

#define relayOFF  HIGH
#define relayON   !relayOFF

#define PUSHED    HIGH

#define relay1    2
#define relay2    3
#define relay3    4
#define relay4    5

#define capSwitch 6 //tombol  HIGH = pushed?

//when relay4Flag is true we are timing relay 4 
boolean relay4Flag = false;

byte state      = 0;
byte buttonPoll = 0;

byte lastButtonPoll;

//timing stuff
unsigned long switchMillis;
unsigned long relay4Millis;

const unsigned long relay4Interval = 1000;  //one second

//*************************************************************************
void setup()
{
  pinMode(capSwitch, INPUT);  //pushed = HIGH?
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);

  digitalWrite(relay1, relayOFF);
  digitalWrite(relay2, relayOFF);
  digitalWrite(relay3, relayOFF);
  digitalWrite(relay4, relayOFF);

} //END of setup()

//*************************************************************************
void loop()
{
  //********************************
  //is it time to check our switches? (every 50 millis seconds)
  if (millis() - switchMillis >= 50)
  {
    //restart this timer
    switchMillis = millis();

    //see what's happening with our switches
    checkSwitches();
  }

  //********************************
  //when we are timing relay 4, has the timer finished?
  if (relay4Flag == true && millis() - relay4Millis >= relay4Interval)
  {
    //disable timing
    relay4Flag = false;

    digitalWrite(relay4, relayOFF);
  }

  //********************************
  //check the StateMachine
  stateMachine();

  //********************************
  // other none blocking code goes here
  //********************************

} //END of loop()


//*************************************************************************
void checkSwitches()
{
  buttonPoll = digitalRead(capSwitch);

  //has the switch changed state?
  if (lastButtonPoll != buttonPoll)
  {
    //update to the new state
    lastButtonPoll = buttonPoll;

    //****************************
    //is the switch PUSHED
    if (buttonPoll == PUSHED)
    {
      //do nothing
    }

    //****************************
    //switch has been released
    else
    {
      state++;

      if (state > 4)
      {
        state = 0;

        digitalWrite(relay1, relayOFF);
        digitalWrite(relay2, relayOFF);
        digitalWrite(relay3, relayOFF);
        //disable timing
        relay4Flag = false;
        digitalWrite(relay4, relayOFF);
      }
    }

  } //END of if (lastButtonPoll != buttonPoll)

} //END of checkSwitches()

//*************************************************************************
void stateMachine()
{
  switch (state)
  {
    //******************
    case 0:
      //do nothing
      break;

    //******************
    case 1: //on
      digitalWrite(relay1, relayON);
      digitalWrite(relay2, relayOFF);
      digitalWrite(relay3, relayOFF);
      digitalWrite(relay4, relayOFF);

      break;

    //******************
    case 2: //acc
      digitalWrite(relay1, relayON);
      digitalWrite(relay2, relayON);
      digitalWrite(relay3, relayON);
      digitalWrite(relay4, relayOFF);

      break;

    //******************
    case 3: //stater
      digitalWrite(relay1, relayON);

      digitalWrite(relay2, relayON);

      //enable timing
      relay4Flag = true;
      digitalWrite(relay4, relayON);
      //start timer
      relay4Millis = millis();

      digitalWrite(relay3, relayON);

      state = 4;

      break;

    //******************
    case 4:
      //do nothing
      break;

    //******************
    default:

      break;

  } //END of switch/case

} //END of  stateMachine()

Thank you! Glad to be here! i just done re-wrote the code, and no way out, then i check the forum, finally that code work for me :smiley: big thanks to you @larryd. this is the code before i see the forum.

 /*Multiple state handling
 */
#define capSwitch D4 //tombol
#define relay1 D1
#define relay2 D2
#define relay3 D3
#define relay4 D5

int buttonPoll = 0;
int carRunState = 0;

void setup() {
  
  pinMode(capSwitch,INPUT);
  pinMode(relay1,OUTPUT);
  pinMode(relay2,OUTPUT);
  pinMode(relay3,OUTPUT);
  pinMode(relay4,OUTPUT);
  
  digitalWrite(relay1,HIGH);
  digitalWrite(relay2,HIGH);
  digitalWrite(relay3,HIGH);
  digitalWrite(relay4,HIGH);
  
}

void loop() {
  boolean success;
  buttonPoll = digitalRead(capSwitch);
  if (success) {
  if ((buttonPoll == 1) && (carRunState == 0)){
    digitalWrite(relay1,LOW);
    digitalWrite(relay2,HIGH);
    digitalWrite(relay3,HIGH);
    digitalWrite(relay4,HIGH);
    carRunState = carRunState++;
  }
  else if ((buttonPoll == 1) && (carRunState == 1)){
    digitalWrite(relay1,LOW);
    digitalWrite(relay2,LOW);
    digitalWrite(relay3,LOW);
    digitalWrite(relay4,HIGH);
    carRunState = carRunState++;
  }
  else if ((buttonPoll == 1) && (carRunState == 2)){
    digitalWrite(relay1,HIGH);
    delay(500);
    digitalWrite(relay1,LOW);
    digitalWrite(relay2,LOW);
    digitalWrite(relay3,HIGH);
    delay(500);
    digitalWrite(relay3,LOW);
    digitalWrite(relay4,LOW);
    delay(500);
    carRunState = carRunState++;
  }
  else if ((buttonPoll == 1) && (carRunState > 2)){
    digitalWrite(relay1,HIGH);
    digitalWrite(relay2,HIGH);
    digitalWrite(relay3,HIGH);
    digitalWrite(relay4,HIGH);
    carRunState = 0;
  }
}
}

then i see this, so speechless…

larryd:
To be tested.

What do you not understand?

// Multiple state handling

//Version 1.00

#define relayOFF  HIGH
#define relayON  !relayOFF

#define PUSHED    HIGH

#define relay1    2
#define relay2    3
#define relay3    4
#define relay4    5

#define capSwitch 6 //tombol  HIGH = pushed?

//when relay4Flag is true we are timing relay 4
boolean relay4Flag = false;

byte state      = 0;
byte buttonPoll = 0;

byte lastButtonPoll;

//timing stuff
unsigned long switchMillis;
unsigned long relay4Millis;

const unsigned long relay4Interval = 1000;  //one second

//*************************************************************************
void setup()
{
  pinMode(capSwitch, INPUT);  //pushed = HIGH?
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);

digitalWrite(relay1, relayOFF);
  digitalWrite(relay2, relayOFF);
  digitalWrite(relay3, relayOFF);
  digitalWrite(relay4, relayOFF);

} //END of setup()

//*************************************************************************
void loop()
{
  //********************************
  //is it time to check our switches? (every 50 millis seconds)
  if (millis() - switchMillis >= 50)
  {
    //restart this timer
    switchMillis = millis();

//see what’s happening with our switches
    checkSwitches();
  }

//********************************
  //when we are timing relay 4, has the timer finished?
  if (relay4Flag == true && millis() - relay4Millis >= relay4Interval)
  {
    //disable timing
    relay4Flag = false;

digitalWrite(relay4, relayOFF);
  }

//********************************
  //check the StateMachine
  stateMachine();

//********************************
  // other none blocking code goes here
  //********************************

} //END of loop()

//*************************************************************************
void checkSwitches()
{
  buttonPoll = digitalRead(capSwitch);

//has the switch changed state?
  if (lastButtonPoll != buttonPoll)
  {
    //update to the new state
    lastButtonPoll = buttonPoll;

//****************************
    //is the switch PUSHED
    if (buttonPoll == PUSHED)
    {
      //do nothing
    }

//****************************
    //switch has been released
    else
    {
      state++;

if (state > 4)
      {
        state = 0;

digitalWrite(relay1, relayOFF);
        digitalWrite(relay2, relayOFF);
        digitalWrite(relay3, relayOFF);
        //disable timing
        relay4Flag = false;
        digitalWrite(relay4, relayOFF);
      }
    }

} //END of if (lastButtonPoll != buttonPoll)

} //END of checkSwitches()

//*************************************************************************
void stateMachine()
{
  switch (state)
  {
    //******************
    case 0:
      //do nothing
      break;

//******************
    case 1: //on
      digitalWrite(relay1, relayON);
      digitalWrite(relay2, relayOFF);
      digitalWrite(relay3, relayOFF);
      digitalWrite(relay4, relayOFF);

break;

//******************
    case 2: //acc
      digitalWrite(relay1, relayON);
      digitalWrite(relay2, relayON);
      digitalWrite(relay3, relayON);
      digitalWrite(relay4, relayOFF);

break;

//******************
    case 3: //stater
      digitalWrite(relay1, relayON);

digitalWrite(relay2, relayON);

//enable timing
      relay4Flag = true;
      digitalWrite(relay4, relayON);
      //start timer
      relay4Millis = millis();

digitalWrite(relay3, relayON);

state = 4;

break;

//******************
    case 4:
      //do nothing
      break;

//******************
    default:

break;

} //END of switch/case

} //END of  stateMachine()

sketch_jan01d.ino (1.45 KB)

#define relay1 D1 <-----<<<<

D0 and D1 are used for the Serial communications.

Use D2-D13 and A0-A5 on an Arduino UNO.

Remember, you can only learn if you get answers to things you don’t understand :wink:

My fault no check again, i just wonder, you make void checkswitches for capSwitch and have 4 state, if i make one virtual pin from the state 3 how is this possible?

There are 5 states 0, 1, 2, 3, and 4.

“ if i make one virtual pin from the state 3 how is this possible?”

I do not understand, please rephrase your question.

I want to connect to blynk, just use state 3 to write all the relay just 1 times push button virtually from virtual pin. Is that possible

I have not used blynk.

Maybe this:

//Multiple state handling
//Versioin 1.1

#define RELAYoff  HIGH
#define RELAYon   !RELAYoff

#define PUSHED    HIGH

#define relay1    2
#define relay2    3
#define relay3    4
#define relay4    5

#define capSwitch 6 //tombol  HIGH = pushed?

//when relay4Flag is true, we are timing relay 4
boolean relay4Flag = false;

byte state      = 0;
byte buttonPoll = 0;

byte lastButtonPoll;

//timing stuff
unsigned long switchMillis;
unsigned long relay4Millis;

const unsigned long relay4Interval = 1000;  //one second

//*************************************************************************
void setup()
{
  pinMode(capSwitch, INPUT);  //pushed = HIGH?

  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);

  digitalWrite(relay1, RELAYoff);
  digitalWrite(relay2, RELAYoff);
  digitalWrite(relay3, RELAYoff);
  digitalWrite(relay4, RELAYoff);

} //END of setup()

//*************************************************************************
void loop()
{
  //********************************
  //is it time to check our switches? (every 50 millis seconds)
  if (millis() - switchMillis >= 50)
  {
    //restart this timer
    switchMillis = millis();

    //see what's happening with our switches
    checkSwitches();
  }

  //********************************
  //when we are timing relay 4, has the timer finished?
  if (relay4Flag == true && millis() - relay4Millis >= relay4Interval)
  {
    //disable timing
    relay4Flag = false;

    digitalWrite(relay4, RELAYoff);
  }

  //********************************
  //check the StateMachine
  stateMachine();

  //********************************
  // other none blocking code goes here
  //********************************

} //END of loop()


//*************************************************************************
void checkSwitches()
{
  buttonPoll = digitalRead(capSwitch);

  //has the switch changed state?
  if (lastButtonPoll != buttonPoll)
  {
    //update to the new state
    lastButtonPoll = buttonPoll;

    //****************************
    //is the switch PUSHED
    if (buttonPoll == PUSHED)
    {
      //do nothing
    }

    //****************************
    //switch has been released
    else
    {
      state++;
    }

  } //END of if (lastButtonPoll != buttonPoll)

} //END of checkSwitches()

//*************************************************************************
void stateMachine()
{
  switch (state)
  {
    //******************
    case 0:
      //do nothing
      
      break;

    //******************
    case 1: //on
      digitalWrite(relay1, RELAYon);
      digitalWrite(relay2, RELAYoff);
      digitalWrite(relay3, RELAYoff);
      digitalWrite(relay4, RELAYoff);

      break;

    //******************
    case 2: //acc
      digitalWrite(relay1, RELAYon);
      digitalWrite(relay2, RELAYon);
      digitalWrite(relay3, RELAYon);
      digitalWrite(relay4, RELAYoff);

      break;

    //******************
    case 3: //stater
      digitalWrite(relay1, RELAYon);
      digitalWrite(relay2, RELAYon);
      digitalWrite(relay3, RELAYon);

      //enable timing
      relay4Flag = true;
      digitalWrite(relay4, RELAYon);
      //start timer
      relay4Millis = millis();

      state = 4;

      break;

    //******************
    case 4:
      //do nothing
      
      break;

    //******************
    default:
      state = 0;

      digitalWrite(relay1, RELAYoff);
      digitalWrite(relay2, RELAYoff);
      digitalWrite(relay3, RELAYoff);

      //disable timing
      relay4Flag = false;
      digitalWrite(relay4, RELAYoff);
      
      break;

  } //END of switch/case

} //END of  stateMachine()


//*************************************************************************