I need to make this railwaycrossing for school.
The program should read the sensors to close and open the crossing.
I struggle with the fact that i cant use multiple loops and cant use delay because it would stop the leds flashing so i tried Chrono timers.
But it wont compile because it says they are not defined,
And i might not be using them correctly either.
after X=2 and the railway closes again the LED's are still flashing so could i use a timer to get back to
x=0 and go back to the default state (railwaycrossing OFF) ?
or is it possible to use the position of the servo's to turn the LED's off?
So it should be
Sensor L detects the train
- LED's start flashing with the piezo as an alarm
-After a delay the crossing should close
Sensor R detects the train
-Crossing opens
- LED's keep flashing and the alarm is on until the crossing is open
Default state
- crossing is open and LED's and alarm are off
How could i realize this ?
I hope the program is clear enough because its written in dutch
#include <Chrono.h>
#include <LightChrono.h>
#include <Servo.h>
int LEDLi1=11;
int LEDLi2=10;
int LEDRe1=4;
int LEDRe2=3;
Servo servo1;
Servo servo2;
int pos1=70;
int pos2=70;
int sensePinL=0; // analog Pin A0 on the arduino
int sensePinR=8; // analog Pin A1 on the arduino
int piezopin=6;
int x=0;
void setup() {
// put your setup code here, to run once:
// Instantiate Chronos
Chrono chronoA;
Chrono chronoB;
Chrono chronoC;
servo1.attach(12);
servo2.attach(13);
pinMode(LEDLi1,OUTPUT);
pinMode(LEDLi2,OUTPUT);
pinMode(LEDRe1,OUTPUT);
pinMode(LEDRe2,OUTPUT);
pinMode(sensePinL,INPUT);
pinMode(sensePinR,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int detectR = digitalRead(8);// lezen van sensors
int detectL = digitalRead(0);
Serial.print("x= ");
Serial.println(x);
// sensors
if(detectL == LOW){ // sensors setten een variabele die gebruikt worden om aan te sturen
x=1;
}
if(detectR == LOW){
x=2;
}
//Defaultstate
while(x == 0){
pos1=180;
servo1.write(pos1);
pos2=180;
servo2.write(pos2);
digitalWrite(LEDLi1,LOW);
digitalWrite(LEDRe1,LOW);
noTone (piezopin);
digitalWrite(LEDLi2,LOW);
digitalWrite(LEDRe2,LOW);
}
// sensor L neemt obstakel waar
// slagboom gaat dicht
while (x == 1){ //X is 1 bij de eerste sensor dit laat de overgang dichtgaan
if (chronoB.hasPassed(2000) { //slagboom gaat dicht na 2s
pos1=70;
servo1.write(pos1);
pos2=70;
servo2.write(pos2);
chronoA.restart(); //..and restart the chrono
if (chronoA.hasPassed(500) { //Every 0,5 seconds..
digitalWrite(LEDLi1,HIGH);// LED's en geluid
digitalWrite(LEDRe1,HIGH);
tone (piezopin,1000);
digitalWrite(LEDLi2,LOW);
digitalWrite(LEDRe2,LOW);
chronoA.restart(); //..and restart the chrono
}
else {
digitalWrite(LEDLi1,LOW);
digitalWrite(LEDRe1,LOW);
tone (piezopin,500);
digitalWrite(LEDLi2,HIGH);
digitalWrite(LEDRe2,HIGH);
}
// sensor R neemt obstakel waar
//lichten blijven branden
while(x == 2){
if (chronoA.hasPassed(500) { //Every 0,5 seconds..
digitalWrite(LEDLi1,HIGH);// LED's en geluid
digitalWrite(LEDRe1,HIGH);
tone (piezopin,1000);
digitalWrite(LEDLi2,LOW);
digitalWrite(LEDRe2,LOW); //..toggle..
chronoA.restart(); //..and restart the chrono
else {
digitalWrite(LEDLi1,LOW);
digitalWrite(LEDRe1,LOW);
tone (piezopin,500);
digitalWrite(LEDLi2,HIGH);
digitalWrite(LEDRe2,HIGH);
}
if (chronoC.hasPassed(2000) { //slagboom gaat open na 2s
pos1=70;
servo1.write(pos1);
pos2=70;
servo2.write(pos2);
chronoC.restart();
}}