# Two leds using millis

How to stop led1 and power on led2 using millis

Your post was MOVED to its current location as it is more suitable.

Hello
By writing a sketch for an Arduino.

What have you tried and where are you stuck ?

Please post what you have tried

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Yes sir, but i know how to use millis , i was stuck on the error where led 1 and led2 turns on at the same time. All i want is to turn one led1 for 2 seconds then off. Then turn on led2 while led1 is off

Hello
Post your sketch to see how we can help.

That looks like for a OOP class based solution

So you don't know how to use millis() after all

Please post the code that you have a problem with

@lefohj

may be you want read this:

https://werner.rothschopf.net/microcontroller/202109_millis_two_leds_en.htm

If you will just post your code then we will be able to tell you if you "know how to use millis", If it turns out you don't then we can help you learn a bit more.

Steve

I guess the TO is sleeping and dreaming about a proper solution.

``````
byte ledPins [] = { 10, 11 };

#define N_LEDS      sizeof(ledPins)
#define TwoSec      2000

enum { LedOff = HIGH, LedOn = LOW };

unsigned long msecLst;

void setup()
{
for (unsigned n = 0; n < N_LEDS; n++)  {
digitalWrite (ledPins [n], LedOff);
pinMode      (ledPins [n], OUTPUT);
}

msecLst = millis ();
digitalWrite (ledPins [0], LedOn);
}

void loop()
{
unsigned long msec = millis ();

if (LedOn == digitalRead (ledPins [0]) &&  (msec - msecLst) > TwoSec)  {
digitalWrite (ledPins [0], LedOff);
digitalWrite (ledPins [1], LedOn);
}
}
``````

consider; I do like this note

``````// you may need to change these constants to your hardware
constexpr byte LED[] {2, 3};
enum {One, Two};
void setup() {
for (auto LED_ : LED) digitalWrite (LED_, LOW),pinMode(LED_, OUTPUT);
digitalWrite (LED[One], HIGH);
while (millis()<2000);
digitalWrite (LED[One], LOW);
digitalWrite (LED[Two], HIGH);
}
void loop () {}
``````

Have a nice day and enjoy coding.

I'd correct the time handling, and dis-entangle it from the LED handling, ie change this:

to

``````void loop()
{
unsigned long msec = millis ();
if (msec - msecLst) > TwoSec)
{
msecLst = msec ;  // you forgot to update this.
if (LedOn == digitalRead (ledPins [0]))
{
digitalWrite (ledPins [0], LedOff);
digitalWrite (ledPins [1], LedOn);
}
else
{
... something is presumably supposed to happen if LED 0 was off?
}
}
}
``````

why? what's wrong with this?

not per the stated requriement

Separation of concerns is one of the important principles of programming. Mixing two different things into one line of code is less readable, less easy to maintain, and generally increases the entropy/chaos in the program.

Here's there's some logic to activate something at a given time or interval, there's also some business logic with LEDs. They are different things. And more importantly the time stuff is generically reusable in many situations - you might want to pull it off into a library or class to increase its reusability and simplify the business logic.

If its only supposed to happen once it probably belongs in setup(), not loop() ??

I do note the original code calls pinMode() after digitalWrite() - that's not guaranteed to work on every Arduino, pinMode should be set to OUTPUT before calling digitalWrite.

Other than that the original code seems plausible - is this a hardware issue then?

this is a novice user trying to do something trivial

this is my code

``````/*

// assign pin numbers for the leds

const int led1 = 13;
const int led2 = 12;
//set trigger and echo pin
const int trigger  = 11;
const int echo = 10;

long leddistance,
ledduration;

enum SWsensorDo {inittime,water, soap, water2,off}; // the states a single traffic light can be in
// the above line makes a variable data type "SWsensorDo" and assigns values 0, 1, 2, to water, soap, water2
SWsensorDo SWactivate;  //create a variable sensorDO

const int timings[] = {1000,2000, 400, 15000, 3000};
unsigned long timeStart, timeElapsed ;

void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
SWactivate = inittime;
timeStart = millis();
}

void loop() {
measure_distance();
while(leddistance<=23)
{
person_flag;
if(person_flag)
{
measure_distance();
if(leddistance>23)
{
person_flag=false;
break;
}
SWactivate = water;
}
else
{
OFF_All();
}
}

if(leddistance>23)
{
OFF_All();
}
switch (SWactivate) {
case inittime: {
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
//is it time for the next state?
timeElapsed = millis() - timeStart;
if (timeElapsed > timings[inittime]) {
//restart the timer and change the state
timeStart = millis();

}
}
break;
case water : {
//set the lights for this state
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);

//is it time for the next state?
timeElapsed = millis() - timeStart;
if (timeElapsed > timings[water]) {
//restart the timer and change the state
timeStart = millis();
SWactivate = soap;
}
}
break;

case soap: {
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);
//is it time for the next state?
timeElapsed = millis() - timeStart;
if (timeElapsed > timings[soap]) {
//restart the timer and change the state
//timeStart = millis();
SWactivate = water2;
}
}
break;

case water2: {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
//is it time for the next state?
timeElapsed = millis() - timeStart;
if (timeElapsed > timings[water2]) {
//restart the timer and change the state
timeStart = millis();
SWactivate = off;
}
}
break;
case off:
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
}
break;
}
}//loop

void measure_distance()
{
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(15);
digitalWrite(trigger,LOW);
ledduration = pulseIn(echo, HIGH);
leddistance = (ledduration/2)/29;
delay(5);
}
void OFF_All()
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}``````