Hi, I'm having a problem with this code. It is written to blink seven led lights randomly. But I would like it to work for a minute and than stop for minute or two, and start again in a loop. I've put a delay at the end of the code, did not work, lights were just blinking slower. Thank you for your help, I seem not to see the problem.
int leds[7] = {7,8,9,10,11,12,13};
void setup(){
for (int jj; jj<sizeof(leds)/sizeof(int);jj++){
pinMode(leds[jj],OUTPUT);
delay(30);
}
}
void loop(){
digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],HIGH);
delay(random(30,400));
digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],LOW);
}
Thank you for this article. I think I understand it, but I don't want to change the code, I like how quick and randomly LEDs are blinking. I would just like this code to work as it is working now, and than stop it for a minute or two (in millis) but I am not sure how to do that. Thank you for your help.
LOL. If you don't change the code, how do you expect it to do anything different?
If you want to do one thing for awhile, blink some LEDs, then another thing for awhile, as in do nothing, you'll probably have to use a millis() based timing mechanism, or at least involve some kind of variable that can track the switch between the two things.
You could switch after some time, or switch after N number of blinks.
BTW, during the do-nothing phase, shoukd the LEDs be off or left where they might be?
Sorry I am new to all this and maybe I wrote it wrongly, did not mean not to change the code at all, I just meant that I like how LEDs are working now, and would just like to stop them from working from time to time.
I would like them to turn off, all of them.
void loop() {
for (int yy = 0; yy < 42; yy++) {
digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],HIGH);
delay(random(30,400));
digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],LOW);
}
// here turn off all LEDs
delay(2000); // that could be random in some range also
}
Naturally we'd prefer more elegant and flexible solutions.
You could add time up in the for loop instead of ending at 42, use a while loop and exit the loop when you'd added up the time it had taken and it was above some (possibly random) threshold.
Do what you understand and have fun, but try to learn more about these more sophisticate techniques we all talking about.
Gonna have to try your unchangable code to see what that algorithm looks like.
I did not mean not to change te code, sorry for that. I only meant I like how randomly LEDs are blinking. I'm very new to all this and have never used codes that do more than one thing at the time, so it is a bit confusing.. I've tried delay(2000); at the bottom of my code and it is not working for me. Thank you for all your help.
#define ENABLED true
#define DISABLED false
#define LEDon HIGH //PIN---[220R]---A[LED]K---GND
#define LEDoff LOW
const byte leds[7] = {7, 8, 9, 10, 11, 12, 13};
const byte heartbeatLED = 2;
const byte maxLEDs = sizeof(leds) / sizeof(byte);
bool timingFlag = ENABLED;
//timing stuff
unsigned long heartbeatTime;
unsigned long toggleLedTime;
unsigned long disableTime;
unsigned long disableInterval = 10ul * 1000; //10 seconds (change as needed)
unsigned long LEDinterval;
// s e t u p ( )
//********************************************^************************************************
void setup()
{
for (byte x = 0; x < maxLEDs; x++)
{
pinMode(leds[x], OUTPUT);
}
pinMode(heartbeatLED, OUTPUT);
randomSeed(analogRead(0));
} //END of setup()
// l o o p ( )
//********************************************^************************************************
void loop()
{
//************************************************ T I M E R heartbeatLED
//is it time to toggle the heartbeat LED ?
if (millis() - heartbeatTime >= 500ul)
{
//restart this TIMER
heartbeatTime = millis();
//toggle the heartbeat LED
if (digitalRead(heartbeatLED) == HIGH) digitalWrite(heartbeatLED, LOW);
else digitalWrite(heartbeatLED, HIGH);
}
//************************************************ T I M E R toggle LED
//if enabled, is it time to toggle the a LED ?
if (timingFlag == ENABLED && millis() - toggleLedTime >= LEDinterval)
{
//restart this TIMER
toggleLedTime = millis();
digitalWrite(leds[random(0, maxLEDs)], LEDon);
digitalWrite(leds[random(0, maxLEDs)], LEDoff);
LEDinterval = random(30, 400);
}
//************************************************ T I M E R disable
//is it time to toggle the timingFlag ?
if (millis() - disableTime >= disableInterval)
{
//restart this TIMER
disableTime = millis();
//LEDs OFF
for (byte x = 0; x < maxLEDs; x++)
{
digitalWrite(leds[x], LEDoff);
}
//toggle the timing Flag
if (timingFlag == ENABLED)timingFlag = DISABLED;
else timingFlag = ENABLED;
}
//************************************************
//other non blocking code goes here
//************************************************
} //END of loop()
There is a fundamental difference between delay() and non-blocking timing.
delay() is linear
void loop() {
do 1
do 2 which is the delay()
do 3
do 4
do 5
do 6
do 7
} // jump up to "do 1"
.
non-blocking timing is circular
void loop() {
do 1
do 1b check if time is over - - - but only in case time **IS REALLY** over
do 2
and update Timing-variable
do 3
do 4
} // jump up to do 1
In a similar way you can make code-execition conditional
Counting (using a for loop) and adding time up for a time limit (using a while loop) both work well I find.
Counting:
void loop() {
for (int yy = 0; yy < 23; yy++) {
digitalWrite(leds[random(0, sizeof(leds) / sizeof(int))], HIGH);
delay(random(30, 200));
digitalWrite(leds[random(0, sizeof(leds) / sizeof(int))], LOW);
}
// here turn off all LEDs
for (int jj = 0; jj < sizeof(leds) / sizeof(int);jj++)
digitalWrite(leds[jj], LOW);
delay(2000); // that could be random in some range also
}
I was hacking with your algorithm, liking the nature of the effect paused or no, when I discovered an error in the above code line.
It may be working for you now, but it is not correct, and may bite you one day. You have failed to initialise the loop index variable, viz:
Be careful out there! This might have provoked a warning from the compiler. In any case, please go to the IDE preferences and crank up all warnings and verbosity, then heed the red ink that spills when you do (some) things that are syntactically correct but not what you need or want to do.
BTW, spaces are free. Using them makes code easier to read. The IDE Autoformat tool will put them in the same places I did. Use it if goof formatting doesn't yet live in your fingertips.
I've got this code here: Arduino - Randomly Blinking Multiple LEDs With Only 9 Lines of Code — Maker Portal
I always check any code before uploading to LilyPad. If it is coloured RED I always correct it.
I am new to all this, this is my first code doing more than one thing at the time. Would you recommend me to use another code? I would just like it to pause for a minute and then work again. What I like about this code is that LEDs a re blinking randomly, for different period of time and that more then one is ON and the same time.
There is no such thing as 10 or 100 other codes. In the sense of a limited number.
There are zillions of zillion codes. You can modify each and every code for 0,000001% or change it 10000000% Just as you like.
This means your initial code is re-used to a certain part and modified in other parts.
Thank you for this sooo much. I've tried it and it does not work for me. I blinks each LED separately. They are ON for a long period, not blinking like before. Should I add something to the code you've posted?
Sorry, might be annoying, but as I said, I am truly new to this, trying to understand everything. I should study this more, but now I am in a hurry and just try to solve this code to do what I would like it to do:)