Go Down

Topic: A change in flashing light pulses (Read 614 times) previous topic - next topic

vincescerri

Hi all,

I have put together an Arduino sketch. It has three flashing lights which flash 5 times each in turn by means of a void loop. They flash at 250 MS being the on and off times.
I would like to keep the first light which is the red, constantly flashing without stopping while the yellow and blue continue to keep flashing in turns. Can somebody help me by showing a change to my sketch.

Thank you in advance.

Attached is my sketch.



Code: [Select]

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

int redOnTime=250;
int redOffTime=250;
int yellowOnTime=250;
int yellowOffTime=250;
int blueOnTime=250;
int blueOffTime=250;

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

void setup() {
pinMode(redLEDPin,OUTPUT);
pinMode(yellowLEDPin,OUTPUT);
pinMode(blueLEDPin,OUTPUT);

}

void loop(){
for (int j=1; j<=numRedBlink;j=j+1){ 
digitalWrite(redLEDPin, HIGH);
delay(redOnTime);
digitalWrite(redLEDPin,LOW);
delay(redOffTime);
}
for (int j=1; j<=numYellowBlink;j=j+1){
digitalWrite(yellowLEDPin,HIGH);
delay(yellowOnTime);
digitalWrite(yellowLEDPin, LOW);
delay(yellowOffTime);
}
for (int k=1; k<=numBlueBlink;k=k+1){
digitalWrite(blueLEDPin,HIGH);
delay(blueOnTime);
digitalWrite(blueLEDPin,LOW);
delay(blueOffTime);
}
}



vinceherman

#1
Nov 28, 2017, 04:34 pm Last Edit: Nov 28, 2017, 04:39 pm by vinceherman
Hi,
When you are coding in the IDE, hit CTRL-T.  This autoformats the code, making indents for loops.  It makes it much easier to read, and goes a long way to help hunt down mismatch curly brackets.

As for your requested change, let me clarify.  
You currently have 3 modes
  • blinky red
  • blinky yellow
  • blinky blue

but you want 2 modes
  • blinky red and yellow
  • blinky red and blue

Is that correct?
I suspect that you can do this with very little work.
Delete the loop that blinks the red.
In the loop that blinks yellow, every time you turn on or off the yellow, also turn on or off the red.
In the loop that blinks blue, every time you turn on or off the blue, also turn on or off the red.
Done!

Give it a try.  Let us know how it works out.

edit: I see that you made a duplicate post.
http://forum.arduino.cc/index.php?topic=514114
Can you go there and delete it?

edit2: for extra credit, go read Blink without Delay and Demonstration code for several things at the same time

vincescerri

Hi and thanks.

I have created an indent  to my sketch. I have also removed the red loop. But!

I am having a problem understanding what you mean,

You say,every time you turn on and off the yellow or blue, then also turn on and off the red. Can you please explain in stages what to do. Once the program starts running the is nothing I can touch but watch it without a red light since I have disconnected the red loop.

I am new at this coding and not up to speed yet.

Can you please guide me a little better in more simple 1,2,3,stages.

Attached is the sketch with the removed red loop. Now, the is no red light showing at any time.
Code: [Select]

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

int redOnTime = 250;
int redOffTime = 250;
int yellowOnTime = 250;
int yellowOffTime = 250;
int blueOnTime = 250;
int blueOffTime = 250;

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

void setup() {
  pinMode(redLEDPin, OUTPUT);
  pinMode(yellowLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);

 }

  void loop() {
 
  for (int j = 1; j <= numYellowBlink; j = j + 1) {
    digitalWrite(yellowLEDPin, HIGH);
    delay(yellowOnTime);
    digitalWrite(yellowLEDPin, LOW);
    delay(yellowOffTime);
  }
  for (int k = 1; k <= numBlueBlink; k = k + 1) {
    digitalWrite(blueLEDPin, HIGH);
    delay(blueOnTime);
    digitalWrite(blueLEDPin, LOW);
    delay(blueOffTime);
  }
  }
 

vinceherman

Quote
I am new at this coding and not up to speed yet.
No worries.  I am old at coding and I still need help from this forum to figure out things. :)

You used this to turn on the red
Code: [Select]
   digitalWrite(redLEDPin, HIGH);
And you used this to turn off the red
Code: [Select]
   digitalWrite(redLEDPin,LOW);

In your new sketch, can you find the line where you turn on the yellow?  It looks like this:
Code: [Select]
   digitalWrite(yellowLEDPin, HIGH);
Add a line after that.  on the new line, put in the code that turns on the red.
Then in the sketch, find the line that turns off the yellow.  Add a line after that.  Put in the code that turns off the red.

Repeat this in the blue loop.

vincescerri

Hi ,

I need more time.  I think I did what you suggested but I'm getting the red error line on the editions. This coming Saturday, I'm going to see a friend that knows a little more than I do. I am hoping he can carry out what you suggested without getting the error line like I do. I will be back in touch to let you know how we went.
Thank you

vinceherman

If you have made changes to the code and get errors, then you did something wrong.
You could post your code here, and also post the errors.

vincescerri

Hi Vince,

As I said earlier, I do not see my friend until next Saturday. As it turns out, I do not need to.

I carefully read you advice again and followed your instructions once more. It is now working successfully. I must have made one small error somewhere before, just as you thought. It is a great feeling to see it working as a learning exercise. I'm enclosing the final sketch that works. Thank you very much for your help.

Vince Scerri


Code: [Select]


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

int redOnTime = 250;
int redOffTime = 250;
int yellowOnTime = 250;
int yellowOffTime = 250;
int blueOnTime = 250;
int blueOffTime = 250;

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

void setup() {
  pinMode(redLEDPin, OUTPUT);
  pinMode(yellowLEDPin, OUTPUT);
  pinMode(blueLEDPin, 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 <= numBlueBlink; k = k + 1) {
    digitalWrite(blueLEDPin, HIGH);
    digitalWrite(redLEDPin, HIGH);
    delay(blueOnTime);
    digitalWrite(blueLEDPin, LOW);
    digitalWrite(redLEDPin, LOW);
    delay(blueOffTime);
  }
  }
 



vinceherman

Congrats for getting it working!
Thanks for posting your finished code.
And it is always fun when a project works like you intend.

Go Up