I want to use an Arduino to light up some LEDS on a hexacopter that I am building. What I am going for is to run one pattern for say 20 seconds then move onto the next pattern and then keep repeating that until I take off. I plan on using a sonar sensor to tell the LED's to stay on all the time after I get 2 feet off of the ground. I am basing my program off of Circuit #4 in the SIK Guide since most of the code I need is there. I believe I want to use an "int" command but I'm not sure how to do that. Here is the code I have so far.
Does anyone know how to get the Marquee loop to run for 20 seconds then move Random then run Random for 20 seconds and repeat make to Marquee?
int ledPins[] = {2,3,4,5,6,7,8,9};
void setup()
{
int index;
for(index = 0; index <= 7; index++)
{
pinMode(ledPins[index],OUTPUT);
// ledPins[index] is replaced by the value in the array.
// For example, ledPins[0] is 2
}
}
void loop()
{
marquee(); // Chase lights like you see on signs
randomLED(); // Blink LEDs randomly
}
*/
marquee()
This function will mimic "chase lights" like those around signs.
*/
void marquee()
{
int index;
int delayTime = 200; // milliseconds to pause between LEDs
// Make this smaller for faster switching
// Step through the first four LEDs
// (We'll light up one in the lower 4 and one in the upper 4)
for(index = 0; index <= 3; index++) // Step from 0 to 3
{
digitalWrite(ledPins[index], HIGH); // Turn an LED on
digitalWrite(ledPins[index+4], HIGH); // Skip four, and turn that LED on
delay(delayTime); // Pause to slow down the sequence
digitalWrite(ledPins[index], LOW); // Turn the LED off
digitalWrite(ledPins[index+4], LOW); // Skip four, and turn that LED off
}
}
/*
randomLED()
This function will turn on random LEDs. Can you modify it so it
also lights them for random times?
*/
void randomLED()
{
int index;
int delayTime;
index = random(8); // pick a random number between 0 and 7
delayTime = 100;
digitalWrite(ledPins[index], HIGH); // turn LED on
delay(delayTime); // pause to slow down
digitalWrite(ledPins[index], LOW); // turn LED off
}
You need to look at blink without delay, and integrate it into your code. once you put in delays none of the sensors will work until the delay has been completed.
Yes, thanks for pointing out the smiley face. I deleted a bunch of informational commenting that came with the example that I am basing my program from and missed the smiley. I will be removing it and also looking into blink without delay. Thanks. Paul S, I did read the sticky about how to post questions I missed the part on how to post code. I pledge to try to get it right next time!
I believe I have the code posted correctly now. Thanks for the lessons on posting etiquette. Now hopefully people will offer suggestions on a solution for my original question.
This is some code I used to blink several Christmas tree LEDs on and off in a manor that you want.
First it sets up the data for the on and off time for each LED.
Then it does the blinking until 100 seconds has passed and then it calculates a new blinking pattern.
/* Random blinking lights
for use on a christmas tree
By Mike Cook
*/
#define numberOfLights 16
byte pins[] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
byte pinState[numberOfLights];
long int changeTime[numberOfLights];
int flashRate[numberOfLights];
long flashChange; // how often to change the flashing patterns
void setup() {
for(int i = 0; i< numberOfLights; i++) {
pinMode(pins[i], OUTPUT);
changeTime[i] = millis() + random(1000, 200);
pinState[i] = LOW;
}
setFlashTime();
}
void loop() {
for(int i = 0; i < numberOfLights; i++) {
if(changeTime[i] <= millis()) {
pinState[i] = ~pinState[i];
digitalWrite(pins[i], pinState[i]);
changeTime[i] = millis() + flashRate[i];
}
}
if(flashChange <= millis()) setFlashTime();
}
void setFlashTime(){
for(int i=0; i<numberOfLights; i++){
flashRate[i] = random(200, 1500);
}
flashChange = millis() + 100000; // next time to change pattern
}
It is based on the blink without delay sketch example in the IDE.