While loop during LED function

Hello all. I am new to IDE and trying to configure two sets of LEDs to function. I am running through a Seeeduino XIAO, need the small size for my current project. My two sets of LEDs are 5 color sequence that runs continually, and a set of 3 LEDs that I want to illuminate sequentially with a push button. Here is the current code I am using. Each of the codes works independently, but I cannot figure out how to marry the two together. Any ideas or assistance is appreciated!

int whiteLED=0;
int blueLED=1;
int blackLED=2;
int redLED=3;
int greenLED=4;

int cast1=7;
int cast2=8;
int cast3=9;

int buttonPin=10;

int count=0;
int newcount;

void setup() {

Serial.begin(9600);

pinMode(whiteLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(blackLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(cast1, OUTPUT);
pinMode(cast2, OUTPUT);
pinMode(cast3, OUTPUT);
pinMode(buttonPin, INPUT);

}

void loop () {

while (digitalRead(buttonPin)==LOW) {
LEDsequence();
if (digitalRead(buttonPin)==HIGH){
Countsequence();
}
}
}

void Countsequence() {
if(digitalRead(buttonPin)==HIGH) {
newcount=count+1;
if(newcount!=count)
{
Serial.println(newcount);
switch (newcount)
{
case 1: digitalWrite(cast1,HIGH);
break;
case 2: digitalWrite(cast2,HIGH);
break;
case 3: digitalWrite(cast3,HIGH);
break;
default: digitalWrite(cast1,LOW);
digitalWrite(cast1,LOW);
digitalWrite(cast2,LOW);
digitalWrite(cast3,LOW);

newcount=0;
break;
}
count=newcount;
}

delay(50);
}
}

void LEDsequence() {

digitalWrite(whiteLED, HIGH);
delay(3000);
digitalWrite(whiteLED, LOW);
delay(50);

digitalWrite(blueLED, HIGH);
delay(3000);
digitalWrite(blueLED, LOW);
delay(50);

digitalWrite(blackLED, HIGH);
delay(3000);
digitalWrite(blackLED, LOW);
delay(50);

digitalWrite(redLED, HIGH);
delay(3000);
digitalWrite(redLED, LOW);
delay(50);

digitalWrite(greenLED, HIGH);
delay(3000);
digitalWrite(greenLED, LOW);
delay(50);

digitalWrite(whiteLED, HIGH);
digitalWrite(blueLED, HIGH);
delay(3000);
digitalWrite(whiteLED, LOW);
digitalWrite(blueLED, LOW);
delay(50);

digitalWrite(blueLED, HIGH);
digitalWrite(blackLED, HIGH);
delay(3000);
digitalWrite(blueLED, LOW);
digitalWrite(blackLED, LOW);
delay(50);

digitalWrite(blackLED, HIGH);
digitalWrite(redLED, HIGH);
delay(3000);
digitalWrite(blackLED, LOW);
digitalWrite(redLED, LOW);
delay(50);

digitalWrite(redLED, HIGH);
digitalWrite(greenLED, HIGH);
delay(3000);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
delay(50);

digitalWrite(greenLED, HIGH);
digitalWrite(whiteLED, HIGH);
delay(3000);
digitalWrite(greenLED, LOW);
digitalWrite(whiteLED, LOW);
delay(50);

digitalWrite(blueLED, HIGH);
digitalWrite(redLED, HIGH);
delay(3000);
digitalWrite(blueLED, LOW);
digitalWrite(redLED, LOW);
delay(50);

digitalWrite(blackLED, HIGH);
digitalWrite(greenLED, HIGH);
delay(3000);
digitalWrite(blackLED, LOW);
digitalWrite(greenLED, LOW);
delay(50);

digitalWrite(redLED, HIGH);
digitalWrite(whiteLED, HIGH);
delay(3000);
digitalWrite(redLED, LOW);
digitalWrite(whiteLED, LOW);
delay(50);

digitalWrite(blueLED, HIGH);
digitalWrite(greenLED, HIGH);
delay(3000);
digitalWrite(blueLED, LOW);
digitalWrite(greenLED, LOW);
delay(50);

digitalWrite(blackLED, HIGH);
digitalWrite(whiteLED, HIGH);
delay(3000);
digitalWrite(blackLED, LOW);
digitalWrite(whiteLED, LOW);
delay(50);
}

Help us help you.

Like this...

nt whiteLED = 0;
int blueLED = 1;
int blackLED = 2;
int redLED = 3;
int greenLED = 4;

int cast1 = 7;
int cast2 = 8;
int cast3 = 9;

int buttonPin = 10;

int count = 0;
int newcount;

void setup() {

  Serial.begin(9600);

  pinMode(whiteLED, OUTPUT);
  pinMode(blueLED, OUTPUT);
  pinMode(blackLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(cast1, OUTPUT);
  pinMode(cast2, OUTPUT);
  pinMode(cast3, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {

  while (digitalRead(buttonPin) == LOW) {
    LEDsequence();
    if (digitalRead(buttonPin) == HIGH) {
      Countsequence();
    }
  }
}

void Countsequence() {
  if (digitalRead(buttonPin) == HIGH) {
    newcount = count + 1;
    if (newcount != count) {
      Serial.println(newcount);
      switch (newcount) {
        case 1:
          digitalWrite(cast1, HIGH);
          break;
        case 2:
          digitalWrite(cast2, HIGH);
          break;
        case 3:
          digitalWrite(cast3, HIGH);
          break;
        default:
          digitalWrite(cast1, LOW);
          digitalWrite(cast1, LOW);
          digitalWrite(cast2, LOW);
          digitalWrite(cast3, LOW);

          newcount = 0;
          break;
      }
      count = newcount;
    }

    delay(50);
  }
}

void LEDsequence() {

  digitalWrite(whiteLED, HIGH);
  delay(3000);
  digitalWrite(whiteLED, LOW);
  delay(50);

  digitalWrite(blueLED, HIGH);
  delay(3000);
  digitalWrite(blueLED, LOW);
  delay(50);

  digitalWrite(blackLED, HIGH);
  delay(3000);
  digitalWrite(blackLED, LOW);
  delay(50);

  digitalWrite(redLED, HIGH);
  delay(3000);
  digitalWrite(redLED, LOW);
  delay(50);

  digitalWrite(greenLED, HIGH);
  delay(3000);
  digitalWrite(greenLED, LOW);
  delay(50);

  digitalWrite(whiteLED, HIGH);
  digitalWrite(blueLED, HIGH);
  delay(3000);
  digitalWrite(whiteLED, LOW);
  digitalWrite(blueLED, LOW);
  delay(50);

  digitalWrite(blueLED, HIGH);
  digitalWrite(blackLED, HIGH);
  delay(3000);
  digitalWrite(blueLED, LOW);
  digitalWrite(blackLED, LOW);
  delay(50);

  digitalWrite(blackLED, HIGH);
  digitalWrite(redLED, HIGH);
  delay(3000);
  digitalWrite(blackLED, LOW);
  digitalWrite(redLED, LOW);
  delay(50);

  digitalWrite(redLED, HIGH);
  digitalWrite(greenLED, HIGH);
  delay(3000);
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, LOW);
  delay(50);

  digitalWrite(greenLED, HIGH);
  digitalWrite(whiteLED, HIGH);
  delay(3000);
  digitalWrite(greenLED, LOW);
  digitalWrite(whiteLED, LOW);
  delay(50);

  digitalWrite(blueLED, HIGH);
  digitalWrite(redLED, HIGH);
  delay(3000);
  digitalWrite(blueLED, LOW);
  digitalWrite(redLED, LOW);
  delay(50);

  digitalWrite(blackLED, HIGH);
  digitalWrite(greenLED, HIGH);
  delay(3000);
  digitalWrite(blackLED, LOW);
  digitalWrite(greenLED, LOW);
  delay(50);

  digitalWrite(redLED, HIGH);
  digitalWrite(whiteLED, HIGH);
  delay(3000);
  digitalWrite(redLED, LOW);
  digitalWrite(whiteLED, LOW);
  delay(50);

  digitalWrite(blueLED, HIGH);
  digitalWrite(greenLED, HIGH);
  delay(3000);
  digitalWrite(blueLED, LOW);
  digitalWrite(greenLED, LOW);
  delay(50);

  digitalWrite(blackLED, HIGH);
  digitalWrite(whiteLED, HIGH);
  delay(3000);
  digitalWrite(blackLED, LOW);
  digitalWrite(whiteLED, LOW);
  delay(50);
}
1 Like

Thanks, I couldn't figure out how to do that.

So looking at the while() you want to while as long as buttonPin is LOW but you want to do another thing when the button is HIGH while remaining in the while()?

The while() should run as long as buttonPin == LOW but when buttonPin is HIGH the while loop will exit. The things in the if (digitalRead(buttonPin) == HIGH may not ever have a chance to run.

Appreciate the response, I'm new and not sure exactly how to do all this. Attaching a picture and the formatted code was provided below.

Copy your code...
Click this button in the editor window...


Paste your code

2 Likes

Exactly! I do not mind if the while loop restarts upon button push, as long as the if LED remain illuminated. I understand this may not be possible on one Seeeduino XIAO (the chip I am using in replacement of the UNO R3, but trying to exhaust all avenues before resigning to adding another XIAO to the project.

This is a picture of the project. I will have the CR2032, XIAO, and LEDs nested throughout the cards to illuminate the art.


LEDs require current limiting resistors !

A common LED needs a series resistor to limit the current flow to a safe level ≈ 10mA.

  • Driving a LED directly from a controller output will damage your controller.

Nice clear images !
:+1:

1 Like

Even with the XIAO being a 3.3v output? I used the resistors on the 5v output of the UNO R3, but didn't think they were necessary with the lower voltage output on the XIAO. (it is 5v when plugged into computer, but runs off a CR2032 when used in the project)

Yes

example:

Assume the LED has a forward voltage drop of 2V and we need 10mA of current.

3V3——[ Resistor ]——[A—>|—K]——GND

R = (3v3 - 2v) / .010A = 130Ω

2 Likes

Thank you! Added 330 resistors to the set.

330 Ω will allow ≈ 4mA, most common LEDs will be bright enough at this current.

1 Like

Hello ftfa

Do you have experience with programming in C++.

The task can easily be realised by using array´s .
A structured array contains all the information, such as the pin addresses for the I/O devices, as well as the information for the timing.
A single service takes care of this information and initiates the intended action.
The structured array makes the sketch scalable until all I/O pins are used up without having to adapt the code for the service.
It is cool stuff, isn´t it?

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

I do not have any experience with C++. Talking with a colleague he thinks Python is the way to go for programming. Any experience with that?

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