Counter info

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?

Maybe it doesn't work fine because of using a millie inside another millie?

It "doesn't work" because the use of millis() (not millie) is to replace the blocking nature of delay().

Your tollbooth() function is called once. If it isn't time, then, to do something (and, it won't ever be), then nothing gets done, and the function ends.

You really need to rethink the complete structure of the program. Nothing blocks. So, it truly is an event driven system, and you are supplying the event loop. Think about what that means. A barge approaches. That's an event. Time passes. That's an event. LEDs need to be turned on. Those are events. Sounds need to be made. Those are events. But, none of the events should wait for another event.

Your event loop needs to take into consideration events that have occurred (the barge approached, the clock ticked, an LED was turned on, a sound was made), when those events occurred, and what needs to be done NOW, based on what events have already occurred, and when they occurred.

   case 0:
    if(var_Lstate == 0)
      digitalWrite (LED_L, HIGH);  
    if(read_LED_L == HIGH)
    {
        currentTime_WT = millis();
        var = 1;
    }   
   break;

That second if statement is pointless and only servers to delay movement into the second state for one more iteration of loop(). The time recording and state assignment should be within the first if statement.

As I said in my previous post, the blinking LED: The code to blink the LED without Delay should not be within the State machine structure. It should be in the main loop. Move it there, to make sure it actually works without any conditions. Then, instead of calling the function when you've reached the 5 second mark, set a "blinkLED" variable to 1. The modify your tollbooth's if statement to also check if that variable is set to 1.

  if(flag == 1)
  {
    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);     }   }         switch(var)   {   case 0:     if(var_Lstate == 0)     {       digitalWrite (LED_L, HIGH);        currentTime_WT = millis();       var = 1;     }

    break; 

  case 1:         if(currentTime_WT - previousTime_WT > interval_WT)     {       previousTime_WT = currentTime_WT;       flag = 1;            }       break;       } 

}




... and it's not working. 

If the sensor is triggered in the first 4.9 seconds after the program is uploaded on the board, it's just turning ON the marker and that's all.
If the sensor is triggered after 5 seconds, the marker's turning ON and in the same time, the tollbooth's warnings.

And I don't have any idea why is not working as I want: It should start the warnings at 5 seconds after the sensor sensor is tripped.
Do you?

Moving this back to the thread; going through PMs doesn't allow others who need similar help, to see the resolution.

The problem is that you are not properly setting the currentTime and previousTime variables. currentTime should always be set to millis() in every loop, it's the previousTime that should be updated when the state changes. Notice how previousTime is initialized to 0 and never set in the first state? This is a good example of why having more descriptive variable names can make your life a whole lot easier; the previousTime variable should really be called lastStateChangeTime. After doing that, it might help if you create little helper functions like this:

unsigned long timeSinceLastStateChange()
{
  return millis() - lastStateChangeTime;
}
void goToState(int newState)
{
  var = newState;
  lastStateChangeTime = millis();
}

It's not going to improve performance, but it might make it a little easier to understand:

case STATE_ONE:
  if (some condition is met to change states)
  {
    goToState(STATE_TWO);
  }
  break;
case STATE_TWO:
  if (timeSinceLastStateChange() > 5000)
  {
    // it's been 5 or more seconds!
  }
  break;

It's working perfectly now. Thank you very much

if(millis() - lastStateChangeTime > 700) 
    {
      lastStateChangeTime = millis();           // 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);
    }