Problem with 2 sensors and 3 relays(SOLVED by switch case)

Hi~I use 2 sensors to control 3 relays

when sensor1 on, turn on relay1 1 sec. ,than turn off.

wait 5 sec.

than turn on relay2 1 sec. ,than turn off,

wait 1 sec.

than "must" wait until sensor2 on,than relay3 turn on.

this cycle run 4 times.

but when I uploaded to board and run it, the relay3 always turn on when relay2 off
and sensor2 not on

how to fix it?

#include <EEPROM.h>



const int sensor1Pin = 2;     // the number of the sensor 1 pin
const int sensor2Pin = 3;     // the number of the sensor 2 pin
const int relay1Pin = 8;      // the number of the relay 1 pin
const int relay2Pin = 9;     // the number of the relay 2 pin
const int relay3Pin = 10;     // the number of the relay 3 pin
// variables will change:
int sensor1State = 0;         // variable for reading the sensor1 status
int sensor2State = 0;         // variable for reading the sensor2 status
int counter = 0;

void setup() 
{
  // initialize the RELAY pin as an output:
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT); 
  pinMode(relay3Pin, OUTPUT);  
  // initialize the sensor pin as an input:
  pinMode(sensor1Pin, INPUT);  
  pinMode(sensor2Pin, INPUT); 
}

void loop()
{
  // read the state of the pushbutton value:
  sensor1State = digitalRead(sensor1Pin);
  sensor2State = digitalRead(sensor2Pin);

  // check if the sensor1 is acted.
  // if it is, the button1State is HIGH:
  if ((sensor1State == HIGH) && (counter <= 0)){
  
      for(int ii = 0; ii <= 3; ii++)    
    {
      
      
    // turn RELAY1 on:    
    digitalWrite(relay1Pin, HIGH); 
    delay(1000);
    // turn RELAY1 off:
    digitalWrite(relay1Pin, LOW);
    delay(5000);
      
      
    // turn RELAY2 on 1 sec,turn off:
    digitalWrite(relay2Pin, HIGH);
    delay(1000);
    digitalWrite(relay2Pin, LOW);
    delay(1000);
      
      
    // if sensor2 acted , turn on relay3:
    
    if (sensor2State == HIGH)
    digitalWrite(relay3Pin, HIGH);
    delay(1000);
    digitalWrite(relay3Pin, LOW);
    delay(2000);
    counter++;
      
    }
  }
    if (sensor1State == HIGH)
   
      {
           counter = 0;
           delay(1000);
      }

}

sketch_jun13d.ino (1.73 KB)

That behaviour is because of the ";" in this line:

    if (sensor2State == HIGH);

yup,I found it and fix
but become relay1 and relay2 run circle self
and sensor2 no reply...
(I had checked sensor is good)

I want after relay2 off , wait until sensor2 on, than relay3 on

You are not "waiting". Try this:

#include <EEPROM.h>



const int sensor1Pin = 2;     // the number of the sensor 1 pin
const int sensor2Pin = 3;     // the number of the sensor 2 pin
const int relay1Pin = 8;      // the number of the relay 1 pin
const int relay2Pin = 9;     // the number of the relay 2 pin
const int relay3Pin = 10;     // the number of the relay 3 pin
// variables will change:
int sensor1State = 0;         // variable for reading the sensor1 status
int sensor2State = 0;         // variable for reading the sensor2 status
int counter = 0;

void setup()
{
  // initialize the RELAY pin as an output:
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
  pinMode(relay3Pin, OUTPUT); 
  // initialize the sensor pin as an input:
  pinMode(sensor1Pin, INPUT); 
  pinMode(sensor2Pin, INPUT);
}

void loop()
{
  // read the state of the pushbutton value:
  sensor1State = digitalRead(sensor1Pin);
  sensor2State = digitalRead(sensor2Pin);

  // check if the sensor1 is acted.
  // if it is, the button1State is HIGH:
  if ((sensor1State == HIGH) && (counter <= 0)){

    for(int ii = 0; ii < 4; ii++)   
    {


      // turn RELAY1 on:   
      digitalWrite(relay1Pin, HIGH);
      delay(1000);
      // turn RELAY1 off:
      digitalWrite(relay1Pin, LOW);
      delay(5000);


      // turn RELAY2 on 1 sec,turn off:
      digitalWrite(relay2Pin, HIGH);
      delay(1000);
      digitalWrite(relay2Pin, LOW);
      delay(1000);


      // if sensor2 acted , turn on relay3:

      while (sensor2State != HIGH);
      digitalWrite(relay3Pin, HIGH);
      delay(1000);
      digitalWrite(relay3Pin, LOW);
      delay(2000);
      counter++;
    }
  }
  
  if (sensor1State == HIGH){
    counter = 0;
    delay(1000);
  }
}

I replace the line:

    if (sensor2State == HIGH)

by:

      while (sensor2State != HIGH);

that means "do nothing" until the value of the sensor remains different than HIGH.

I solved~
wrote by switch case

#include <EEPROM.h>


byte led1 = 10; 
byte led2 = 11; 
byte led3 = 12;
byte button1 = 2;
byte button2 = 3;
byte counter = 0;

//define the available states that we can have for this sketch
enum States{
  stateOne, stateTwo};
//add more states as needed
States mState = stateOne;             //we start out in this machine state

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT); 
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);  
  digitalWrite(led3,LOW);
} //END of setup()


void loop()
{
       

  switch (mState)
  {
   case stateOne:
    {
      if(digitalRead(button1))
      {
        digitalWrite(led1,HIGH);
        delay(1000);
        digitalWrite(led1,LOW);
        delay(2000); 
        digitalWrite(led2,HIGH);
        delay(1000);
        digitalWrite(led2,LOW);
        delay(1000);  
        mState = stateTwo;    
      } 
    }
    break;

   
  case stateTwo:
    {
      if(digitalRead(button2))
      {
        digitalWrite(led3,HIGH);
        delay(1000);
        digitalWrite(led3,LOW); 
        delay(1000);      
        mState = stateOne;
      }
    }      
    break;

  default: 
    // default code goes here
    break;

  } //END of switch(mState)
  
}