This is my first post and Im stuck with a script that should blink 8 LEDs on my Arduino Uno randomly but all with a different time interval I can set with variables in a script that runs for 10 minutes. Ive been reading the forum and found this:5 LEDs blink 5 times in a minute at random times - #22 by robtillaart - Programming Questions - Arduino Forum. It looks a bit to what I need but I can't set the interval for each led to blink.
I now tryed to wrote a script that should works for 1 LED to test. But allready there it doesnt work like I want. It doesnt sort my random numbers which I can use later to make the LED blink randomly.
unsigned int timeSlot = 600000; // ten minutes
int L_1_tweets[12]; // amount of blinking each ten minutes
int L_1_sorted[0]; // array to sort list of random numbers from low to highest
int test = 0; // make it loop ones (for testing now)
void setup(){
Serial.begin(9600);
for(int i=0; i < sizeof(L_1_tweets)/sizeof(int); i++){
L_1_tweets[i] = random(timeSlot);
}
}
void loop(){
if(test != 1){
test = 1;
Serial.println("--------------");
for(int w=0; w < sizeof(L_1_tweets)/sizeof(int); w++){ //printlns random made numbers
Serial.println(L_1_tweets[w]);
}
sorteerArray(L_1_tweets, L_1_sorted);
Serial.println("==============");
for(int w=0; w < sizeof(L_1_sorted)/sizeof(int); w++){ //(should) println all sorted numbers
Serial.println(L_1_sorted[w]);
}
}
}
void sorteerArray(int array[], int sortedArray[]){
Serial.println("- - - - START SORTING - - - -");
Serial.println(sizeof(array)/sizeof(int));
for (int j=0; j < sizeof(array)/sizeof(int); j++){
sortedArray[j] = array[j];
}
int done = 0;
while(done != 1){
done = 1;
for (int j = 0; j < (sizeof(array)/sizeof(int) - 1); j++){
if (sortedArray[j] > sortedArray[j + 1]){
int temp = sortedArray[j + 1];
sortedArray [j+1] = sortedArray[j] ;
sortedArray [j] = temp;
Serial.println("RRRRRREEEEEEEAAAAAAAADDDDDYYYYYY!");
done = 0;
}
}
}
}
I really hope there is someone who can help with this!
Cause I don't know where to look and don't know how to continue.
It also can be 60.000 it just has to keep looping over and over so I can use it for an hour+.
The thing is I think my code could be much easier but all the scripts I have tryed so far don't work. One only blinks one at the time because is is in a for loop. So first the first LED is blinking randomly then the second and so on...
1- But I want the LEDs to be chosen random but the one LED more often than an other (LED1 20 times and LED5 4 times is a minute) :~
@UKHeliBob will this also work for the Random LEDs I just typed above (1)? Cause don't see where I should put this pick a random LED in the BlinKwithoutDelay example.
@UKHeliBob will this also work for the Random LEDs I just typed above (1)? Cause don't see where I should put this pick a random LED in the BlinKwithoutDelay example.
How about something like this
create an array of 8 random intervals
create an array of 8 start times set to millis()
create an array of 8 LEDs
define LEDs as outputs
start of loop
for (x = 0; x < 8; x++)
if millis() - ledStart[x] >= randInterval[x]
change state of LED[x]
set ledStart[x] = millis()
end of if
end of for
end of loop
If you want the random intervals to vary whilst the program is running then define a new random interval for the LED after changing its state.
You can wrap the whole thing in another test to see whether 10 minutes have elapsed and turn all the LEDs off and make sure that they stay off.
I was not sure what you meant by 'blink' in your original post, so my pseudo code suggests turning the LED off, then on. If you want them to be mostly off and flash on for a fixed short time, then you will need to implement the on times using millis() again by saving the state of each LED and setting the interval to a fixed period during which time the LED is on, then setting it back to the random value in the array to set the off time.
I'm confused by this. What does the "done" variable achieve here?
I would have used a boolean, and I would have called it "swapped". Then, I think it is clearer what is happening.
If any two elements in the array are out of order, the array was not in order, so another pass is needed. Maybe on that pass there are still elements out of order. Maybe not.
When a pass is completed that finds no elements that need to be swapped, then the array is sorted and the while loop is done.
Your'e really close, but there are several problems.
First, you need to make your sorted array bigger than size 0. There won't be enough room to store the numbers.
Second, you can't use sizeof() take the size of an array if you pass it as a parameter. You can only use sizeof() when you use named arrays or variable types. The compiler simply doesn't know how big the array is in the target function. The standard way around this is to declare something like #define ARRAY_SIZE and then set it to 12 or something.
Third, you can't use 600000 if your timeslot is unsigned int. Also, your arrays should be the same type as your timeslot. And your temp variable as well. If you redefine them all as unsigned ints, and change your timeslot to 60000, it will work.
#define ARRAY_SIZE 12
unsigned int timeSlot = 60000; // one minute
unsigned int L_1_tweets[ARRAY_SIZE]; // amount of blinking each ten minutes
unsigned int L_1_sorted[ARRAY_SIZE]; // array to sort list of random numbers from low to highest
int test = 0; // make it loop ones (for testing now)
void setup(){
Serial.begin(9600);
for(int i=0; i < sizeof(L_1_tweets)/sizeof(int); i++){
L_1_tweets[i] = random(timeSlot);
}
}
void loop(){
if(test != 1){
test = 1;
Serial.println("--------------");
for(int w=0; w < sizeof(L_1_tweets)/sizeof(int); w++){ //printlns random made numbers
Serial.println(L_1_tweets[w]);
}
sorteerArray(L_1_tweets, L_1_sorted);
Serial.println("==============");
for(int w=0; w < sizeof(L_1_sorted)/sizeof(int); w++){ //(should) println all sorted numbers
Serial.println(L_1_sorted[w]);
}
}
}
void sorteerArray(unsigned int array[], unsigned int sortedArray[]){
Serial.println("- - - - START SORTING - - - -");
Serial.println(sizeof(array)/sizeof(int));
for (int j=0; j < ARRAY_SIZE; j++){
sortedArray[j] = array[j];
}
int done = 0;
while(done != 1){
done = 1;
for (int j = 0; j < ARRAY_SIZE - 1; j++){
if (sortedArray[j] > sortedArray[j + 1]){
unsigned int temp = sortedArray[j + 1];
sortedArray [j+1] = sortedArray[j] ;
sortedArray [j] = temp;
Serial.println("RRRRRREEEEEEEAAAAAAAADDDDDYYYYYY!");
done = 0;
}
}
}
}