One flashing led in while loop and three flashing led's in three for loops.

Hi all,

At the moment, I have a red, yellow, green, and blue led's.Three are running at 5 flashes each through three for loops and the red colour is also flashing in each of the three for loops because the red is also typed into each of the three for loops. For an exercise, I am trying to keep three different led's running 5 flashes in the three for loops and the red led through a while loop so that it will flash none stop in the same sketch. Can anybody show me how the while loop is written to keep the red led flashing at all times.

Please see my working attached sketch.

Thank you in advance.

int redLEDPin = 9;
int yellowLEDPin = 10;
int blueLEDPin = 11;
int greenLEDPin = 12;


                       
int redOnTime = 250;    // Red LED never goes off in while
int redOffTime = 250;   
int yellowOnTime = 250;
int yellowOffTime = 250;
int blueOnTime = 250;
int blueOffTime = 250;
int greenOnTime = 250;
int greenOffTime = 250;

int numRedBlink = 5;
int numYellowBlink = 5;
int numBlueBlink = 5;
int numGreenBlink = 5;

void setup() {
  Serial.begin(115200);
  pinMode(redLEDPin, OUTPUT);  
  pinMode(yellowLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
}

void loop() {

  for (int j = 1; j <= numYellowBlink; j = j + 1) {
    digitalWrite(yellowLEDPin, HIGH);
    digitalWrite(redLEDPin , HIGH);
    delay(yellowOnTime);
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(redLEDPin, LOW);
    delay(yellowOffTime);
  }
  for (int k = 1; k <= numGreenBlink; k = k + 1) {
    digitalWrite(greenLEDPin, HIGH);
    digitalWrite(redLEDPin, HIGH);
    delay(greenOnTime);
    digitalWrite(greenLEDPin, LOW);
    digitalWrite(redLEDPin, LOW);
    delay(greenOffTime);
  }
  for (int k = 1; k <= numBlueBlink; k = k + 1) {
    digitalWrite(redLEDPin, HIGH);
    digitalWrite(blueLEDPin, HIGH);
    delay(blueOnTime);
    digitalWrite(redLEDPin, LOW);
    digitalWrite(blueLEDPin, LOW);
    delay(blueOffTime);
  }
  }

Throw away all the for loops and while loops and figure out how to let your loop function do the looping and get this all done with just if statements. No loops and no delay. When you get there you can do anything. But you're wasting your time learning to write blocking code in those for loops. It will make things harder on you later when you have to unlearn that.

I think that @Delta_G is spot on. Read through the Demonstration code for several things at the same time
This will go a long way towards coding this without blocking.

But I have to admit I am not sure what your goal is.
Currently you have 3 loops

  • Red and Yellow
  • Red and Green
  • Red and Blue

You describe what you want like this:

I am trying to keep three different led's running 5 flashes in the three for loops and the red led through a while loop so that it will flash none stop in the same sketch

That sounds very close to what you have now. I guess the one difference is you say you want 3 LEDs running in each loop. Like this?

  • Red and Yellow and Green
  • Red and Green and Blue
  • Red and Blue and Yellow

Hi again,

In reference to my recent request, I would like to simplify things. Firstly, I have greatly minimized my sketch. The modified sketch is now only showing one for loop and one if statement. I have been guided to use an if statement to solve my problem. (Having two actions in the one sketch.)

  1. What I'm trying to do now, is to run one for loop for one action with a flashing
    led with 5 blinks.

  2. The other action is a test if action operating within the same sketch, but all to
    itself.

My problem is not knowing what to write in the if line or just above it, from one
end to the other to make the chosen if test work. Like < = or > and to the end
of the line. I looked up some examples, but not enough is written in.

Eventually, I hope I'm able to use a button switch to create a false situation.

I have attached my greatly simplified sketch. It shows me an error that says

(expected unqualified-id before 'if')

Many thanks

int test = 5;    
int yellowLEDPin = 10;
                
  
int testOnTime = 250;     //constant flashing when on
int testOffTime = 250;   // Flash off time
int yellowOnTime = 250;
int yellowOffTime = 250;

int numYellowBlink = 5;

void setup() {
  
  pinMode(yellowLEDPin, OUTPUT);
 
  pinMode(test, OUTPUT); 
}

  if(test< = 5){ digitalWrite, HIGH);    //I am lost with this area. Please check all.
  
void loop() {

  for (int j = 1; j <= numYellowBlink; j = j + 1) {
    digitalWrite(yellowLEDPin, HIGH);
    delay(yellowOnTime);
    digitalWrite(yellowLEDPin, LOW);
    delay(yellowOffTime);
  }

That if statement needs to be inside one of the functions, either setup or loop.

vincescerri:

  1. What I'm trying to do now, is to run one for loop for one action with a flashing
    led with 5 blinks.

  2. The other action is a test if action operating within the same sketch, but all to
    itself.

I still do not have an understanding of what you are trying to do.

Oh and also <= is a single operator. You can't have a space in the middle of it.

At first, I had it all working well with the help of you, Vince, on this forum. Since then, a friend has asked me to run an if loop for an exercise. I am new at if statements and so is he. I shall continue with this task at a later time when I have more understanding of sketches.

Thank you for your help.

Vince

if statement. Not if loop. if doesn't loop. It's just a single statement and when the execution of the program gets to that statement it does what's inside the braces if and only if the thing in the parenthesis evaluates to true. It really is if this then do that. It's not hard to understand. If you're having trouble I would recommend any of the many online c++ tutorials.