Go Down

Topic: (LED Flash) Need help finding an alternating quadruple flash pattern (Read 857 times) previous topic - next topic

LightChaser

I have two LEDs I want to flash back and forth between each other. But I want each led do somthing like a quadruple flash. Like make it do a fast strobe for one second before switching to the other led it dose the same and switch back. The only part of the code I have is making it flash back and forth slowly, I need help making the strobe effect. Could help me out?

Code: [Select]
void setup(){
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop(){
  digitalWrite(13, HIGH);
  digitalWrite(12, LOW);
  delay(500);
  digitalWrite(13,LOW);
  digitalWrite(12,HIGH);
  delay(500);
}

Grumpy_Mike

That standard code uses the delay function. As long as you are satisfied to only do one thing at one time that is fine. If not the code needs to be way more complex.
This code will give a short blink on pin 13 and then a long one.

Code: [Select]

void setup(){
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop(){
  digitalWrite(13, HIGH);
  digitalWrite(12, LOW);
  delay(100);
  digitalWrite(13, LOW);
  delay(100);
  digitalWrite(13, High);
  delay(500);
  digitalWrite(13,LOW);
  digitalWrite(12,HIGH);
  delay(500);
}

codlink

//LiNK

Grumpy_Mike

Sorry about the spelling error, I was doing it on my iPad. You seem to have the idea. However there is a lot more you can do when you get going but it is more complicated. Look at using arrays to store your blinking pattern and the time between blinking and you can reduce almost any length of flash sequence to less than ten lines.

dwightthinker


Jobi-Wan

Here is a different approach:


The flashing pattern of each LED is a function of time.




This plays that same pattern:
Code: [Select]
void loop()
{
  boolean flash;
  boolean ledOne;
 
  flash = (millis() % 250) < 50;      // Flash LED on in the first 50ms of each 250ms
  ledOne = (millis() % 2000) < 1000;  // LED1 on even seconds, LED2 on odd seconds.
 
  digitalWrite(ledOnePin,flash && ledOne);
  digitalWrite(ledTwoPin,flash && !ledOne); // not Led1, so Led2.
}


On each loop, it looks at the clock and decides whether any LED should be on and which one.



... and this light I'm making is being used in my car ...
^^ You should look into what is and isn't legal. Impersonating police is not.

Grumpy_Mike

This code will flash as many lights as you want in any order by only changing the numbers in the first two lines.

Code: [Select]
// Flashing lights demo by Grumpy Mike

#define inSequence 9
int timeDealy [] = { 400,200,100,800,200,1000,300,400,600 }; // time delay between change
byte pinToggle [] = { 9,5,8,10,9,5,8,10,9 };  // pin to change - matches up with above times

void setup() {
 for(int i =0; i<inSequence; i++){
  pinMode(pinToggle[i],OUTPUT); // set all the pins you will use to outputs
 }

}

void loop() {
static int nextLight = 0;
byte temp = digitalRead(pinToggle[nextLight]);
if( temp ) {
  digitalWrite(pinToggle[nextLight], LOW);
}
else {
  digitalWrite(pinToggle[nextLight], HIGH);
}
delay(timeDealy[nextLight]);
nextLight++;
if(nextLight >= inSequence) nextLight = 0;

}


The line
Code: [Select]
#define inSequence 9
Defines the number of steps in the sequence, this number must match the number of elements in the next two lines.
Code: [Select]
int timeDealy [] = { 400,200,100,800,200,1000,300,400,600 }; // time delay between change
Each number is the delay before the next change.
Code: [Select]
byte pinToggle [] = { 9,5,8,10,9,5,8,10,9 };  // pin to change - matches up with above times
These are the pins to toggle ( change from on to off or off to on ) on each change.

Have fun playing about with this.
When you have it under your belt ask again about changing the flashing pattern.

Grumpy_Mike

The whole point is that you can increase the number of LEDs and complexity of the pattern with no more code, just by adding extra numbers into the arrays. There is no need to add any more code.

If you are having a hard time understanding any aspect of this then just ask.


The way you are doing it now is like the way that a five year old would speak. If you intend to grow up you have to change your view of code. If you don't intend to learn anything then why post here?

Go Up