random display

hello I am trying to set up a simple display so when you push a botton a random light, lights up, or LED. i'm trying 3 LEDs right now but will need more later. I am new to this and am a [u]super novice[/u]. tryied writing this code but nothing happons. I read the info on "random" and this is what I came up with ... thanks

(ya I don't know much)

const int BUTTON = 9; long randNumber; int buttonState = 0; int loopwait = 0; int LEDpin (3); void setup() { pinMode (LEDpin, OUTPUT); pinMode (BUTTON, INPUT); randomSeed(analogRead(0)); } void loop() {

//random number to generate an led buttonState = digitalRead(BUTTON); (loopwait = 1000); if (buttonState == HIGH); randNumber = (3);

if (randNumber == 0) { LEDpin = 3; } if (randNumber == 1) { LEDpin = 4; } //20 if (randNumber == 2) { LEDpin = 5; }

buttonState = digitalRead(BUTTON); { if (buttonState == LOW); digitalWrite(LEDpin, HIGH); loopwait = 1000;

} if (buttonState == HIGH); { digitalWrite(LEDpin, LOW); loopwait = 1000; } }

Here's your problem:   randNumber = (3); You have to actually call the random() function to get a random value:   randNumber = random(3);

   if (buttonState == LOW);

None of the earlier "if"s had semicolons. Why does this one?

Please remember to use code tags when posting code.

OK so like this? it still doesn't work

const int BUTTON = 9; long randNumber; int buttonState = 0; int loopwait = 0; int LEDpin (3); void setup() { pinMode (LEDpin, OUTPUT); pinMode (BUTTON, INPUT); randomSeed(analogRead(0)); } void loop() {

//random number to generate an led buttonState = digitalRead(BUTTON); (loopwait = 1000); if (buttonState == HIGH); randNumber = random(3);

if (randNumber == 0) { LEDpin = 3; } if (randNumber == 1) { LEDpin = 4; } if (randNumber == 2) { LEDpin = 5; }

buttonState = digitalRead(BUTTON); if (buttonState == LOW);{ digitalWrite(LEDpin, HIGH); loopwait = 1000;

} if (buttonState == HIGH); { digitalWrite(LEDpin, LOW); loopwait = 1000; } }

Still the semicolons. See reply #2

Please, please remember to use code tags.

ok I removed them, still no good.

OK, thanks for letting us know.

loopwait = 1000;

What is this value that you keep setting to the same number but never once use for anything? We're you intending to try to have a delay at some point?

ya i guess i dont need them, accept trying to avoid button bounce

ok so I am trying something different. I just don't get the random thing. this sort of works.

const int BUTTON = 9; long randNumber; int buttonState = 0; int loopwait = 0; int LEDpin (3);

void setup() { pinMode (LEDpin, OUTPUT); pinMode (BUTTON, INPUT); randomSeed(analogRead(0));} void loop() {

//random number to generate an led buttonState = digitalRead(BUTTON); (loopwait = 0);{ if (buttonState == HIGH){ randNumber = random(3); if (randNumber == 0) { LEDpin = 3;} if (randNumber == 1) { LEDpin = 4;} if (randNumber == 2) { LEDpin = 5;} digitalWrite(LEDpin, HIGH); loopwait = 10;}

else { if (buttonState == LOW) { loopwait = 0; digitalWrite(LEDpin, LOW);

} } }

}

OK, let's return to the code tags later. So, the code a OK?

tophatt:
ya i guess i dont need them, accept trying to avoid button bounce

Your loopwait variable is not doing that or anything else. You’re just setting the value of a variable and doing nothing with it. You might as well have int fred = 6 in there. It means nothing.

If you want to introduce a delay, use the delay function.

int LEDpin (3);

This is more traditionally written:int LEDpin = 3;

This might set the mode for Pin 3 but it certainly won't set the mode for Pin 4 or Pin 5.  pinMode (LEDpin, OUTPUT); You should probably set the mode for all three:

  pinMode (3, OUTPUT);
  pinMode (4, OUTPUT);
  pinMode (5, OUTPUT);

Since the variable "loopwait" does nothing your LEDs will be blinking faster than you can see.

You are picking a new random pin every time buttonState is HIGH so your LED will be switching around too fast to see. You probably want to pick a new random number only when buttonState changes from LOW to HIGH.