Help, I'm trying to get four leds to blink in a random pattern. This should be a trivial job but I haven't succeeded yet. I'm using int val; and val = random(1,4). Pinmodes and digitalwrite are OK but the leds only blink in sequence rather than randomly. Graybeard
What code are you using to turn on/ off pins (& therefore LEDs)?
How are the LEDs physically connected to the arduino?
Simon, Thanks for asking. There are 4 leds connected to the Arduino. Here's the code.
int redPin = 12;
int bluePin = 11;
int whitePin = 10;
int yellowPin = 9;
int val;
void setup () {
randomSeed(analogRead (1);
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(whitePin, OUTPUT);
pinMode(yellowPin, OUTPUT);
}
void loop(){
val = random(1,4);
if (val == 1)
digitalWrite(redPin, HIGH);
digitalWrite(bluePin, LOW);
digitalWrite(whitePin, LOW);
digitalWrite(yellowPin, LOW);
delay(1000);
digitalWrite(redPin, LOW);
delay(1000);
if (val == 2)
digitalWrite(redPin,LOW);
digitalWrite(bluePin, HIGH);
digitalWrite(whitePin, LOW);
digitalWrite(yellowPin, LOW);
delay(1000);
digitalWrite(bluePin, LOW);
delay(1000);
if (val == 3)
digitalWrite(redPin,LOW);
digitalWrite(bluePin, LOW);
digitalWrite(whitePin, HIGH);
digitalWrite(yellowPin, LOW);
delay(1000);
digitalWrite(whitePin, LOW);
delay(1000);
if (val == 4)
digitalWrite(redPin,LOW);
digitalWrite(bluePin, LOW);
digitalWrite(whitePin, LOW);
digitalWrite(yellowPin, HIGH);
delay(1000);
digitalWrite(yellowPin, LOW);
delay(1000);
}
graybeard
I didn't answer the second question. The Arduino and the leds are on a breadboard. I am using a 1K resistor on each. All leds light up but only in sequence. There is no random action.
graybeard
Hi graybeard, random returns a long, try: long val; instead of int val;
and if you want values between 1 and 4 you need 5 as the second parameter.
long val;
?
val=random(1,5);
hope that fixes it
Hi God Member, Thanks for the suggestions. Unfortunately, I still have sequential blinks- not random.
Graybeard
To God Member, After reviewing you suggestions and reworking the compiler it looks like I've solved the problem. Thank you very much. Graybeard
To Graybeard, good to hear you got it going. And welcome to the Arduino forum.
BTW, any implied deification of the member description has noting to do with any potential blessings that can be bestowed on other members, or even the quality of the posts. These titles are generated by the BBS software and become embarrassing as the post count increases over some arbitrary number, probably to discourage the continued appearance of heavy posters.
probably to discourage the continued appearance of heavy posters.
LOL. Perhaps that's why David2 started a new account...
--Phil.
Hi, sorry for not responding sooner. It looks like you'll still have a problem with the code you posted, and I'm surprised that you say it works. The blocks that set each LED will always execute in entirety except for each single line directly following an if (...) statement.
Gotta run to a meeting, but let me know if you need further clarification (or if you indeed figured this out before!)
Hi graybeard
Just saw your post and thought this might help. It currently lights up 20 leds in random order. I've also had it working for 40 leds on a Wiring board.
I used this program to teach myself some basics which is why it has some very obvious // comments //.
Hope it helps.
// Random LED Display 3 (common anode OR common cathode)
// by Sid Guglielmino 29/02/2008
// Turns 20 leds on in random order according to variable i
// SET UP --------------------------------------------------------------
int i; // variable that holds pin number for
// initialising pins as output
// and turning all pins off at startup
// and turning pins ON randomly
void turn_all_off() { // turn all pins OFF
for(int i=0; i<20; i++) {
digitalWrite(i, LOW);
}
}
void setup() {
for(int i=0; i<20; i++) { // initialize pins 0 to 20 as outputs
pinMode(i, OUTPUT);
}
}
// END OF SET UP --------------------------------------------------------
// START OF PROGRAM -----------------------------------------------------
void loop() {
turn_all_off(); // to turn all pins/leds OFF
i = random(20); // Sets i to a random number between 0 and 20
digitalWrite(i, HIGH); // turn random pin/led ON
delay(20); // wait for a bit
}
// GO BACK TO START OF PROGRAM AND REPEAT --------------------------------
Thanks for all the help. I know my posted code was in error. I fixed it but did not post that fact. Mea culpa. Graybeard
a note to all - Growing older is mandatory - Growing up is not