Ok, so I tryed to make the first 2 steps:
1st step:
- if the left sensor (placed on the left of the bridge at a distance let's say...20cm) detects movement (that means a cargo-boat is aproaching the bridge)
- then turn ON the left marker LED (LED_L - left sensor) [ and leave it ON]
2nd step:
- then wait 5 seconds, start the audio ( one mini-speaker set at a specific frequency) and visual warnings on the bridge (2 red LEDs blinking)
but it doesn't work fine and I got a question:
int var = 0;
// TOLLBOOTH
int ledState13 = LOW;
int ledState12 = LOW;
const int ledPin13 = 13;
const int ledPin12 = 12;
long previousTime_IT = 0;
long interval_IT = 700; // intervat between flashing LED's inside the tollbooth warning system
// SHARP SENSOR
const int LED_L = 10;
const int LED_R = 11;
const int Lstate = 2; // pin 2 - citire
const int Rstate = 4; // pin 4 - citire
int var_Lstate = 0; // reset to zero the variable used to read the state of the OUT pin of the sensor
int var_Rstate = 0;
int read_LED_L = 0;
int read_LED_R = 0;
long previousTime_WT = 0;
long interval_WT = 2000; // interval between after reading the sensor's LED and activating the tollbooth
unsigned long currentTime_WT;
void setup() {
// TOLLBOOTH
pinMode(ledPin13, OUTPUT);
pinMode(ledPin12, OUTPUT);
// SHARP SENSOR
pinMode (LED_L, OUTPUT); // led stanga
pinMode (LED_R, OUTPUT); // led dreapta
pinMode (Lstate, INPUT); // sets pin 2 as digital input
pinMode (Rstate, INPUT);
}
// the loop routine runs over and over again forever:
void loop()
{
var_Lstate = digitalRead(Lstate); // reads the status of the sensor
read_LED_L = digitalRead(10);
switch(var)
{
case 0:
if(var_Lstate == 0)
digitalWrite (LED_L, HIGH);
if(read_LED_L == HIGH)
{
currentTime_WT = millis();
var = 1;
}
break;
case 1:
if(currentTime_WT - previousTime_WT > interval_WT)
{
previousTime_WT = currentTime_WT;
tollbooth();
}
break;
}
}
void tollbooth()
{
unsigned long currentTime_IT = millis(); // check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
if(currentTime_IT - previousTime_IT > interval_IT)
{
previousTime_IT = currentTime_IT; // save the last time you blinked the LED
if (ledState13 == LOW) // if the LED is off turn it on and vice-versa:
{
ledState13 = HIGH;
ledState12 = LOW;
tone(8, 2200, 250);
}
else
{
ledState13 = LOW;
ledState12 = HIGH;
tone(8, 1800, 250);
}
digitalWrite(ledPin13, ledState13); // set the LED with the ledState of the variable:
digitalWrite(ledPin12, ledState12);
}
}
Maybe it doesn't work fine because of using a millie inside another millie?