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);
}
}
#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.