5 LED Lights and 2 Aspirin for my headache

Thanks for any help.

Objective:
Have one of the five LED lights turn on and then off (i.e. blink) randomly. This is to be repeated 25 times before stopping.

What is wrong with my code?

// Declare pin names
int light_one = 1;
int light_two = 2;
int light_three = 3;
int light_four = 4;
int light_five = 5;

int light;
int t_delay;

void setup(){
//Serial.begin(9600); // Uncomment this if you want to print things, otherwise it doens't matter
// Initialize Lights -
//The numbers correspond to the switch that the lights are connected to (change based on your config)
pinMode(light_one, OUTPUT);
pinMode(light_two, OUTPUT);
pinMode(light_three, OUTPUT);
pinMode(light_four, OUTPUT);
pinMode(ligth_five, OUTPUT);
}

// Program continually loops
void loop(){
// this is really easy if you have them in pins 1 thru 4 because you can just generate a random in in that space
light = random(1,5);

// If statement isn't necessary if pins are 1-4, however, if arbitrary pins (4,6,8,13) etc.. then just
// re-assign the digitalOutput pin number to the pin/light it's connected to

if(light == 1){
digitalOutput(light_one, HIGH);
}
else if(light == 2){
digitalOutput(light_two, HIGH);
}
else if(light == 3){
digitalOutput(light_three, HIGH);
}
else if(light == 4){
digitalOutput(light_four, HIGH);
}
else if(light == 5){
digitalOutput(light_five, HIGH);
}

// randomize how long the lights are on for between 3 to 6 seconds
t_delay = random(3000, 6000);
delay(t_delay);

// Turn off all lights
// Change the numbers to the corresponidng pin outputs
digitalOutput(light_one, LOW);
digitalOutput(light_two, LOW);
digitalOutput(light_three, LOW);
digitalOutput(light_four, LOW);
digitalOutput(light_five, LOW);
}

I don't see a digitalOutput() function defined anywhere. Did you, perhaps, mean digitalWrite()?

What happens when you run your code? How is that different from what you want?

It doesn't even compile so the compiler will have told you what's wrong.

E.g. that you keep using a function that doesn't exist and you have spelled "light" wrong at least once.

Steve

Please you code tags when posting your code. Read the sticky post at the beginning of this forum for how to do it.

You don't specify how your code is not working. Are the LEDs not turning on at all? Is something else not acting as you expect? You also don't say how your LEDs are wired up.

A couple of things to note:
Pins 0 and 1 are Serial RX/TX for the board so best to avoid using those.

Second: random(min,max) gives you a number that doesn't include 'max' so you will never light up your LED 5

Also read the post on page 1, the one titled "Read this before posting a programming question ..."

Next time post your code inside tags. If you don't understand why you're being asked to do it, try it first and see.

gusfraser27:
What is wrong with my code?

What makes you think that there is anything wrong with it?

You'll probably make your headache even worse using pin 1, since that's one of the serial pins. It's way better to use an array to store pin numbers.

const int NUM_PINS = 5;
int lightPins[] = {2,3,4,5,6};

Turn on an LED with:

  light = random(0, NUM_PINS);
  digitalWrite(lightPins[light], HIGH);

After the delay just turn the one LED off.

    digitalWrite(lightPins[light], LOW);