After so many requests I attach my code, don't laugh too much !
//Global Variables //CLEANED UP
// NOT LUMEN CONTROLLED
// ADDed BACK DOOR FOR NIGHT LIGHTING
// blocked beam addition
//working version
const byte GATE1 = 2;
const byte BEAM = 3;
// const byte = 4; // not working
const byte GATE2 = 5;
const byte spare = 6;
const int beam = 3;
int buzzer = 7;
const byte RED_led1 = 8;
const byte YELLOW_led2 = 9;
const byte LED3 = 10;
const byte BLUE_led4 = 11;
const int Relay1 = 12;
int beamState = 0;
//int lastButtonState;
unsigned long gate1PushedMillis;
// when button was released
unsigned long RED_led1TurnedOnAt;
// when led was turned on
unsigned long turn1OnDelay = 10;
// wait to turn on LED
unsigned long turn1OffDelay = 15000;
// turn off LED after this time
bool RED_led1Ready = false;
// flag for when button is let go
bool RED_led1State = false;
// for LED is on or not.
unsigned long gate2PushedMillis;
// when button was released
unsigned long YELLOW_led2TurnedOnAt;
// when led was turned on
unsigned long turn2OnDelay = 10;
// wait to turn on LED
unsigned long turn2OffDelay = 15000;
// turn off LED after this time
bool YELLOW_led2Ready = false;
// flag for when button is let go
bool YELLOW_led2State = false;
// for LED is on or not.
unsigned long button4PushedMillis;
// when button was released
unsigned long BLUE_led4TurnedOnAt;
// when led was turned on
unsigned long turn4OnDelay = 10;
// wait to turn on LED
unsigned long turn4OffDelay = 15000;
// turn off LED after this time
bool BLUE_led4Ready = false;
// flag for when button is let go
bool BLUE_led4State = false;
// for LED is on or not.
unsigned long beamPushedMillis;
// when button was released
unsigned long Relay1TurnedOnAt;
// when relay was turned on
unsigned long turn5OnDelay = 10;
// wait to turn on relay
unsigned long turn5OffDelay = 50000;
// turn off relay after this time
bool Relay1Ready = false;
// flag for when beam contact is made
bool Relay1State = false;
// for relay is on or not.
void setup() {
Serial.begin(9600);
pinMode(GATE1, INPUT_PULLUP);
pinMode(BEAM, INPUT_PULLUP);
pinMode(GATE2, INPUT_PULLUP);
pinMode(spare, INPUT_PULLUP);
pinMode(beam, INPUT_PULLUP);
pinMode(RED_led1, OUTPUT);
pinMode(YELLOW_led2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(BLUE_led4, OUTPUT);
pinMode(Relay1, OUTPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(RED_led1, LOW);
digitalWrite(YELLOW_led2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(BLUE_led4, LOW);
digitalWrite(Relay1, LOW);
}
void loop() {
int vred = analogRead(A0); //vred = 0..1023
int beamState = digitalRead(beam);
Serial.println (vred);
delay(10);
// get the time at the start of this loop()
unsigned long currentMillis = millis();
if ( digitalRead(BEAM) == LOW) {
beamPushedMillis = currentMillis;
if ( vred >70 ) {
goto bailout;
} else {
Relay1Ready = true;
}
// make sure this code isn't checked until after button has been let go
if (Relay1Ready ) {
//this is typical millis code here:
if ((unsigned long)(currentMillis - beamPushedMillis) >= turn5OnDelay) {
// enough time has passed
digitalWrite(Relay1, HIGH);
// setup our next "state"
Relay1State = true;
// save when the relay turned on
Relay1TurnedOnAt = currentMillis;
// wait for next button press
Relay1Ready = false;
}
}
// time to turn off relay
if (Relay1State) {
// relay on, check for now long
if ((unsigned long)(currentMillis - Relay1TurnedOnAt) >= turn5OffDelay) {
Relay1State = false;
digitalWrite(Relay1, LOW);
}
}
bailout:
// get the time at the start of this loop()
if (digitalRead(GATE1) == LOW) {
// update the time when button was pushed
gate1PushedMillis = currentMillis;
RED_led1Ready = true;
}
// make sure this code isn't checked until after button has been let go
if (RED_led1Ready) {
//this is typical millis code here:
if ((unsigned long)(currentMillis - gate1PushedMillis) >= turn1OnDelay) {
// okay, enough time has passed since the button was let go.
digitalWrite(RED_led1, HIGH);
// setup our next "state"
RED_led1State = true;
// save when the LED turned on
RED_led1TurnedOnAt = currentMillis;
tone (buzzer, 1455, 1200);
delay(320);
tone (buzzer, 1400, 1200);
delay(300);
tone(buzzer, 600, 200);
noTone;
// wait for next button press
RED_led1Ready = false;
}
}
// see if we are watching for the time to turn off LED
if (RED_led1State) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - RED_led1TurnedOnAt) >= turn1OffDelay)
{ RED_led1State = false;
digitalWrite(RED_led1, LOW);
}
}
// check the button2
if (digitalRead(BEAM) == LOW) {
// update the time when button was pushed
beamPushedMillis = currentMillis;
BLUE_led4Ready = true;
}
// make sure this code isn't checked until after button has been let go
if (BLUE_led4Ready) {
//this is typical millis code here:
if ((unsigned long)(currentMillis - beamPushedMillis) >= turn2OnDelay) {
// okay, enough time has passed since the button was let go.
digitalWrite(BLUE_led4, HIGH);
// setup our next "state"
BLUE_led4State = true;
// save when the LED turned on
BLUE_led4TurnedOnAt = currentMillis;
tone (buzzer, 155, 1200);
delay(320);
tone (buzzer, 1600, 200);
delay(300);
tone(buzzer, 600, 200);
noTone;
// wait for next button press
BLUE_led4Ready = false;
}
}
// see if we are watching for the time to turn off LED
if (BLUE_led4State) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - BLUE_led4TurnedOnAt) >= turn2OffDelay) {
BLUE_led4State = false;
digitalWrite(BLUE_led4, LOW);
}
}
// check the gate2
if (digitalRead(GATE2) == LOW) {
// update the time when button was pushed
gate2PushedMillis = currentMillis;
YELLOW_led2Ready = true;
}
// make sure this code isn't checked until after button has been let go
if (YELLOW_led2Ready) {
//this is typical millis code here:
if ((unsigned long)(currentMillis - gate2PushedMillis) >= turn2OnDelay) {
// okay, enough time has passed since the button was let go.
digitalWrite(YELLOW_led2, HIGH);
// setup our next "state"
YELLOW_led2State = true;
// save when the LED turned on
YELLOW_led2TurnedOnAt = currentMillis;
tone (buzzer, 155, 1200);
delay(320);
tone (buzzer, 1600, 200);
delay(300);
tone(buzzer, 600, 200);
noTone;
// wait for next button press
YELLOW_led2Ready = false;
}
}
// see if we are watching for the time to turn off LED
if (YELLOW_led2State) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - YELLOW_led2TurnedOnAt) >= turn2OffDelay) {
YELLOW_led2State = false;
digitalWrite(YELLOW_led2, LOW);
}
}
}}