Help with using millis in place of delay in traffic management code

Guys i need some urgent help for my traffic management project. I have to get continuous values of 4 magnetic sensors and it won’t work continuously along with the switching lane functions. Can someone suggest me any other method to use the sensors continuously and the code too and also help me out with changing delay to millis??

Traffic_control_management_system.ino (20.1 KB)

also help me out with changing delay to millis??

That is so trivial that I'm surprised that you are having any difficulties.

Put the Arduino away for a while. Think about how YOU would perform the required task, given only a pad of paper, a pencil, and a clock. Suppose that you show up for your shift and the north-south light is red and the east-west light is red. You need to get traffic moving, so you need to turn one of the lights green for a while. After some time, you need to turn the green light off and turn the yellow light on. After some more time, you need to turn the yellow light off, and turn the red light on. At the same time, or near enough as to not matter, you need to turn the other light green. You're old as dirt, and can barely remember your own name.

What do you need to do? I'd think that some of the steps would involve writing down the time that you last changed which light(s) were on, writing down which light(s) are on, and periodically checking what time it is, and comparing that to the time the last light change happened.

For the Arduino, writing down the time that something happened involves some global or static variables and a call to millis(). Writing down which light(s) are on involves some global or static variables. Periodically checking the time means "on every pass through loop()".

So, what part of dumping delay() are you having problems with?

Rather than starting with the code, start by identifying, and enumerating specific states your system can be in. Each unique combination of outputs and inputs represents a state. For example a button that turns a LED on for a specific length of time can be broken into three states:

  1. The LED is off, you are waiting for button to be pressed.

  2. Button has been pressed, so you turn the LED on. Start the interval timer.

  3. The interval time is done, so turn the LED off. Return to the first state.

Use a switch() function with each state being a case.

Start the timer with a statement like this:

   startMillis = millis();

Test if it’s still running with:

   if (millis() - startMillis < intervalMillis) { ...

Where startMillis is an unsigned long variable, and intervalMillis is a long constant.

Now expand this example to use your four sensors and whatever annunciators you use for traffic controls. If you need different timers, use different variables if the intervals overlap. If it’s sequential - one state ends and another begins, you can use the same startMillis variable for them all, just use unique intervalMillis.

A complex system with lots of states can get lengthy, but the code is clear, and easy to change.

EDIT - Paul said the same thing while I was typing.

I am not using any buttons but using the sensors to give values, allot the time for individual lanes and then allot the lanes according to the density of vehicles on each lane. I need the sensor functions to work at the same time with the void loop to give me continuous sensor values from all 4 sensors.

In which case see my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once