Circuit with 1 button and 3 leds

Hi,
I need your help for doing this circuit into what there is a button and 3 leds, if i press the button casually one of the three turns on for one secon, if i press it another time and the led is still the same it flashes for ten seconds, please help and thank for the support.

1 Like

What kind of help do you want? Do you want the forum to give you the code?

1 Like

This code has a circuit with a button and three leds:

hello, kinda i tried but i cant because i dont know how to turn on after when it has to flash for 10 secs

1 Like

So show your code without this last option

1 Like

here
'''
int led1 = 2;
int led2 = 3;
int led3 = 4;
int button = 5;
int lastLed = 0;

void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(button, INPUT_PULLUP);
randomSeed(analogRead(0));
}

void loop() {
if (digitalRead(button) == LOW) {
int currentLed = random(1, 4);
while (currentLed == lastLed) {
currentLed = random(1, 4);
}
lastLed = currentLed;
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(currentLed, HIGH);
delay(500);
if (digitalRead(button) == LOW && currentLed == lastLed) {
int endTime = millis() + 10000;
while (millis() < endTime) {
digitalWrite(currentLed, HIGH);
delay(500);
digitalWrite(currentLed, LOW);
delay(500);
}
}
}
}
'''
i tried fix but i think i ruined it

1 Like

Unfortunately you did not post your code in code tags:

Press the highlighted button in the editor window and copy your code between the two code tags.

image

It should look like this:

int led1 = 2;
int led2 = 3;
int led3 = 4;
int button = 5;
int lastLed = 0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  randomSeed(analogRead(0));
}

void loop() {
  if (digitalRead(button) == LOW) {
    int currentLed = random(1, 4);
    while (currentLed == lastLed) {
      currentLed = random(1, 4);
    }
    lastLed = currentLed;
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(currentLed, HIGH);
    delay(500);
    if (digitalRead(button) == LOW && currentLed == lastLed) {
      int endTime = millis() + 10000;
      while (millis() < endTime) {
        digitalWrite(currentLed, HIGH);
        delay(500);
        digitalWrite(currentLed, LOW);
        delay(500);
      }
    }
  }
}

P.S.:

  • currentLed is set by random function to 1 ,2 or 3.
  • The Led pins are 2 ,3, 4.

Do you see the difference?

random(min, max)`

Parameters

min: lower bound of the random value, inclusive (optional).
max: upper bound of the random value, exclusive.

And this not the recommended way to use millis():

      int endTime = millis() + 10000;
      while (millis() < endTime) {

It should look like this:

   unsigned long  startTime = millis();
   while (millis() - startTime < 10000UL) {

Furthermore you are using the delay() which is a blocking function (that means you "command" the microcontroller to do nothing else (except some interrupt driven functions). People writing small examples like to use the delay() function as it is easy to "burn" some time so that e.g. serial outputs are not too quick or for very simple blinking effects. As soon as buttons or other sensors come into play TOs who began with these examples get stuck ...

If you want to become a fair developer: Try to avoid delays! Use the millis() function in the right way. :wink:

1 Like

Hello andrewskos

Take a view here:

Have a nice day and enjoy coding in C++.

2 Likes

It look as if the logic of your sketch is as follows:

  • START
    • if the button is not pressed goto START.
    • If the button is pressed choose a Led and switch it on.
    • Let the Led light for 500 msec.
    • If the button is pressed after the 500 msec goto BLINK.
    • If the button is not pressed after the 500 msec goto START
  • BLINK
    • let the chosen Led blink for 10 sec and then goto START

This logic can be solved like this all functions in loop() and if-statements:

constexpr unsigned long overallBlinkTime = 10000;
constexpr unsigned long blinkDelay       =  500;
constexpr unsigned long lightDelay       =  500;

constexpr byte buttonPin = 5;

constexpr int noOfLeds = 3;
byte ledPin[noOfLeds] = {2, 3, 4};

int currentLed = 1;
int lastLed = 0;
byte button;

byte step = 0;

unsigned long lastBlinkTime = 0;
unsigned long startOfThisStepTime;


void setup() {
  Serial.begin(115200);
  Serial.println("Start");
  for (int i = 0; i < noOfLeds; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
  pinMode(buttonPin, INPUT_PULLUP);
  randomSeed(analogRead(0));
}


void loop() {
  button = buttonState();

  if (step == 0) {
    if (button == LOW) {
      for (int i = 0; i < noOfLeds; i++) {
        digitalWrite(ledPin[i], LOW);
      }
      while (currentLed == lastLed) {
        currentLed = random(0, noOfLeds);
      }
      lastLed = currentLed;
      digitalWrite(ledPin[currentLed], HIGH);
      step = 1;
      startOfThisStepTime = millis();
    }
  }

  if (step == 1) {
    if (millis() - startOfThisStepTime >= lightDelay) {
      if (button == LOW) {
        step = 2;
        startOfThisStepTime = millis();
      } else {
        digitalWrite(ledPin[currentLed], LOW);
        step = 0;
      }
    }
  }

  if (step == 2) {
    if (millis() - lastBlinkTime >= blinkDelay) {
      lastBlinkTime = millis();
      byte actState = digitalRead(ledPin[currentLed]);
      digitalWrite(ledPin[currentLed], !actState);
    }
    if (millis() - startOfThisStepTime >= overallBlinkTime) {
      digitalWrite(ledPin[currentLed], LOW);
      step = 0;
    }
  }
}

byte buttonState() {
  static unsigned long lastChangeTime = 0;
  static byte lastState = HIGH;
  static byte state = HIGH;
  byte actState = digitalRead(buttonPin);
  if (actState != lastState) {
    lastState = actState;
    lastChangeTime = millis();
  }
  if (state != lastState && millis() - lastChangeTime > 30) {
    state = lastState;
  }
  return state;
}

see also on Wokwi: https://wokwi.com/projects/366726613502440449

or like this with a "state machine" and separate functions:

constexpr unsigned long overallBlinkTime = 10000;
constexpr unsigned long blinkDelay       =  500;
constexpr unsigned long lightDelay       =  500;

constexpr byte buttonPin = 5;

constexpr int noOfLeds = 3;
byte ledPin[noOfLeds] = {2, 3, 4};

int currentLed = 1;
int lastLed = 0;
unsigned long nextStateTime;
byte button;

enum States {CHOOSELED, ON500, BLINK};
States state = CHOOSELED;

void setup() {
  Serial.begin(115200);
  Serial.println("Start");
  for (int i = 0; i < noOfLeds; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
  pinMode(buttonPin, INPUT_PULLUP);
  randomSeed(analogRead(0));
}

void loop() {
  button = buttonState();
  stateMachine();
}

void stateMachine() {
  switch (state) {
    case CHOOSELED:
      chooseLed();
      break;
    case ON500:
      on500();
      break;
    case BLINK:
      blink();
      break;
  }
}

void blink() {
  static unsigned long lastBlinkTime = 0;
  if (millis() - lastBlinkTime >= blinkDelay) {
    lastBlinkTime = millis();
    byte actState = digitalRead(ledPin[currentLed]);
    digitalWrite(ledPin[currentLed], !actState);
  }
  if (millis() - nextStateTime >= overallBlinkTime) {
    digitalWrite(ledPin[currentLed], LOW);
    changeStateTo(CHOOSELED);
    Serial.println("BLINK -> CHOOSELED " + String(currentLed));
  }
}

void on500() {
  if (millis() - nextStateTime >= lightDelay) {
    if (button == LOW) {
      changeStateTo(BLINK);
      Serial.println("ON500 -> BLINK " + String(currentLed));

    } else {
      digitalWrite(ledPin[currentLed], LOW);
      changeStateTo(CHOOSELED);
      Serial.println("ON500 -> CHOOSELED " + String(currentLed));
    }
  }
}

void chooseLed() {
  if (button == LOW) {
    for (int i = 0; i < noOfLeds; i++) {
      digitalWrite(ledPin[i], LOW);
    }
    while (currentLed == lastLed) {
      currentLed = random(0, noOfLeds);
    }
    lastLed = currentLed;
    digitalWrite(ledPin[currentLed], HIGH);
    changeStateTo(ON500);
    Serial.println("ON500 " + String(currentLed));
  }
}

void changeStateTo(States newState) {
  state = newState;
  nextStateTime = millis();
}

byte buttonState() {
  static unsigned long lastChangeTime = 0;
  static byte lastState = HIGH;
  static byte state = HIGH;
  byte actState = digitalRead(buttonPin);
  if (actState != lastState) {
    lastState = actState;
    lastChangeTime = millis();
  }
  if (state != lastState && millis() - lastChangeTime > 30) {
    state = lastState;
  }
  return state;
}

See on Wokwi: https://wokwi.com/projects/366726547664446465

Obviously the upper version can also be made easier to read by using separate functions ...

In both versions the button state is read as quick as possible but in a debounced way!

Hope you can get something useful out of it :wink:
Good luck!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.