Thanks to PaulMurrayCbr I have something which works fine for one door:
int treuil1 = 11;
int treuil2 = 12;
int porte_glis = 1;
int PG;
int butee1 = 7;
int butee2 = 8;
int stop1 = 0;
int stop2 = 0;
enum State { CLOSED, OPENING, OPENED, CLOSING } state = OPENED;
const uint32_t checkInterval_ms = 5000; // check every 5 seconds
uint32_t most_recent_check_ms;
int IR_THRESHOLD = 850;
void setup() {
pinMode(treuil1, OUTPUT);
pinMode(treuil2, OUTPUT);
}
void loop() {
stop1 = digitalRead(butee1);
stop2 = digitalRead(butee2);
PG = analogRead(porte_glis);
/*if(millis() - most_recent_check_ms >= checkInterval_ms) {
//turn on the IR unit
// wait a few millis for the IR to warm up
PG = analogRead(porte_glis);
//turn off the IR unit
}*/
switch(state) {
case CLOSED:
if(PG < IR_THRESHOLD) {
//turn on the motor to open the doors
digitalWrite(treuil1, HIGH);
digitalWrite(treuil2, LOW);
state = OPENING;
}
break;
case OPENING:
if(stop2 == HIGH)//the door has hit the open stop button
{
//turn off the motor to open the doors
digitalWrite(treuil1, LOW);
digitalWrite(treuil2, LOW);
delay(10000);
state = OPENED;
}
break;
case OPENED:
if(PG > IR_THRESHOLD) {
//turn on the motor to close the doors
digitalWrite(treuil1, LOW);
digitalWrite(treuil2, HIGH);
state = CLOSING;
}
break;
case CLOSING:
if(stop1 == HIGH) //the door has hit the close stop button
{
//turn off the motor to close the doors
digitalWrite(treuil1, LOW);
digitalWrite(treuil2, LOW);
state = CLOSED;
}
break;
}
}
Unfortunately it doesn’t extend nicely for the two doors case. When I try to run the following code only one door responds:
int treuil1 = 11;
int treuil2 = 12;
int treuil3 = 9;
int treuil4 = 10;
int porte_glis = 1;
int porte_bat = 0;
int PB, PG;
int butee1 = 7;
int butee2 = 8;
int stop1 = 0;
int stop2 = 0;
int butee3 = 2;
int butee4 = 3;
int stop3 = 0;
int stop4 = 0;
enum State { CLOSED, OPENING, OPENED, CLOSING } state = OPENED;
int IR_THRESHOLD = 850;
void setup() {
pinMode(treuil1, OUTPUT);
pinMode(treuil2, OUTPUT);
pinMode(treuil3, OUTPUT);
pinMode(treuil4, OUTPUT);
}
void loop() {
stop1 = digitalRead(butee1);
stop2 = digitalRead(butee2);
stop3 = digitalRead(butee3);
stop4 = digitalRead(butee4);
PB = analogRead(porte_bat);
PG = analogRead(porte_glis);
switch(state) {
case CLOSED:
if(PG < IR_THRESHOLD) {
//turn on the motor to open the doors
digitalWrite(treuil1, HIGH);
digitalWrite(treuil2, LOW);
state = OPENING;
}
if(PB < IR_THRESHOLD) {
//turn on the motor to open the doors
digitalWrite(treuil3, HIGH);
digitalWrite(treuil4, LOW);
state = OPENING;
}
break;
case OPENING:
if(stop2 == HIGH)//the door has hit the open stop button
{
//turn off the motor to open the doors
digitalWrite(treuil1, LOW);
digitalWrite(treuil2, LOW);
delay(10000);
state = OPENED;
}
if(stop4 == HIGH)//the door has hit the open stop button
{
//turn off the motor to open the doors
digitalWrite(treuil3, LOW);
digitalWrite(treuil4, LOW);
delay(10000);
state = OPENED;
}
break;
case OPENED:
if(PG > IR_THRESHOLD) {
//turn on the motor to close the doors
digitalWrite(treuil1, LOW);
digitalWrite(treuil2, HIGH);
state = CLOSING;
}
if(PB > IR_THRESHOLD) {
//turn on the motor to close the doors
digitalWrite(treuil3, LOW);
digitalWrite(treuil4, HIGH);
state = CLOSING;
}
break;
case CLOSING:
if(stop1 == HIGH) //the door has hit the close stop button
{
//turn off the motor to close the doors
digitalWrite(treuil1, LOW);
digitalWrite(treuil2, LOW);
state = CLOSED;
}
if(stop3 == HIGH) //the door has hit the close stop button
{
//turn off the motor to close the doors
digitalWrite(treuil3, LOW);
digitalWrite(treuil4, LOW);
state = CLOSED;
}
break;
}
}
When I tried to have different set of cases for each door:
int treuil1 = 11;
int treuil2 = 12;
int treuil3 = 9;
int treuil4 = 10;
int porte_glis = 1;
int porte_bat = 0;
int PB, PG;
int butee1 = 7;
int butee2 = 8;
int stop1 = 0;
int stop2 = 0;
int butee3 = 2;
int butee4 = 3;
int stop3 = 0;
int stop4 = 0;
enum State { CLOSED, OPENINGg, OPENED, CLOSINGg, OPENINGb, CLOSINGb } state = OPENED;
int IR_THRESHOLD = 850;
void setup() {
pinMode(treuil1, OUTPUT);
pinMode(treuil2, OUTPUT);
pinMode(treuil3, OUTPUT);
pinMode(treuil4, OUTPUT);
}
void loop() {
stop1 = digitalRead(butee1);
stop2 = digitalRead(butee2);
stop3 = digitalRead(butee3);
stop4 = digitalRead(butee4);
PB = analogRead(porte_bat);
PG = analogRead(porte_glis);
switch(state) {
case CLOSED:
if(PG < IR_THRESHOLD) {
//turn on the motor to open the doors
digitalWrite(treuil1, HIGH);
digitalWrite(treuil2, LOW);
state = OPENINGg;
}
if(PB < IR_THRESHOLD) {
//turn on the motor to open the doors
digitalWrite(treuil3, HIGH);
digitalWrite(treuil4, LOW);
state = OPENINGb;
}
break;
case OPENINGg:
if(stop2 == HIGH)//the door has hit the open stop button
{
//turn off the motor to open the doors
digitalWrite(treuil1, LOW);
digitalWrite(treuil2, LOW);
delay(10000);
state = OPENED;
}
break;
case OPENINGb:
if(stop4 == HIGH)//the door has hit the open stop button
{
//turn off the motor to open the doors
digitalWrite(treuil3, LOW);
digitalWrite(treuil4, LOW);
delay(10000);
state = OPENED;
}
break;
case OPENED:
if(PG > IR_THRESHOLD) {
//turn on the motor to close the doors
digitalWrite(treuil1, LOW);
digitalWrite(treuil2, HIGH);
state = CLOSINGg;
}
if(PB > IR_THRESHOLD) {
//turn on the motor to close the doors
digitalWrite(treuil3, LOW);
digitalWrite(treuil4, HIGH);
state = CLOSINGb;
}
break;
case CLOSINGg:
if(stop1 == HIGH) //the door has hit the close stop button
{
//turn off the motor to close the doors
digitalWrite(treuil1, LOW);
digitalWrite(treuil2, LOW);
state = CLOSED;
}
break;
case CLOSINGb:
if(stop3 == HIGH) //the door has hit the close stop button
{
//turn off the motor to close the doors
digitalWrite(treuil3, LOW);
digitalWrite(treuil4, LOW);
state = CLOSED;
}
break;
}
}
I do not get the expected response on each door.