Go Down

Topic: random led blink code check (Read 14737 times) previous topic - next topic

markthespark

hi all
i got a simple code and have modified it myself just want some one to check if the code is ok
i'm new at this and still learning the code


// Random flasher by mark oldroyd
// Simple random number flasher
// generation and integration into your circuit on the
// arduino.
//
// Circuit build : Simply connect 8 LED's to pins 3,4,5,6,7,8,9,10
// Be sure to connect a resistor to each LED.  I used 330 ohms.
//
//
int ranNum;
int ranDel;
void setup() {
// Seed RNG from analog port.
randomSeed(analogRead(0));
// Setup 8 output ports for LED's
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop() {
    //Generate random number between 8 and 10
    ranNum=random(3,11);
    // Generate random delay time
    ranDel=random(25,300);
    //Turn on the LED
    digitalWrite(ranNum, HIGH);
    delay(ranDel);
    //Turn off the LED
    digitalWrite(ranNum, LOW); 
}




thanks all
marko

Arrch

Code: [Select]
void loop() {
   //Generate random number between 8 and 10
   ranNum=random(3,11);
   // Generate random delay time
   ranDel=random(25,300);
   //Turn on the LED
   digitalWrite(ranNum, HIGH);
   delay(ranDel);
   //Turn off the LED
   digitalWrite(ranNum, LOW);  
}

The LED is turned on, delays for a random amount of time, turned off and then within a split second, turned on again. Look at the Blink example and notice the second delay.

markthespark

#2
Jun 08, 2012, 10:10 pm Last Edit: Jun 08, 2012, 10:16 pm by markthespark Reason: 1
is this ok as i say i'm new to arduino i'm used to pic chips


   //Generate random number between 8 and 10
   ranNum=random(3,11);
   // Generate random delay time
   ranDel=random(100,400);
   //Turn on the LED
   digitalWrite(ranNum, HIGH);
   delay(ranDel);
   //Turn off the LED
   digitalWrite(ranNum, LOW);  

AWOL

Quote
The LED is turned on, delays for a random amount of time, turned off and then within a split second, turned on again.

Yes, but hopefully, a different LED is turned on.

OP, what is the problem you're seeing, and what did you expect to see?

Arrch


Quote
The LED is turned on, delays for a random amount of time, turned off and then within a split second, turned on again.

Yes, but hopefully, a different LED is turned on.

OP, what is the problem you're seeing, and what did you expect to see?


Good point, I totally skipped over the whole random LED part.

markthespark

im looking to flash 8 led's in a slow random pattern possably fading between each flash

AWOL

OK, you're missing the fade, but are you happy with the randomness?

markthespark

yes im happy with the random bit i can modify it later
i would like it to fade between each phase but i'm not that good yet lol

AWOL

A problem you'll run into is that you're picking pin numbers between 3 and 10 inclusive, not all of which are capable of hardware PWM, which is useful for dimming.
I'd put the PWM pin numbers into an array, and use the random number to provide an index into that array.

markthespark

#9
Jun 08, 2012, 10:36 pm Last Edit: Jun 08, 2012, 10:38 pm by markthespark Reason: 1
pins 6 to 13 ok ?

AWOL

http://arduino.cc/en/Reference/AnalogWrite

markthespark

so do not use pins 5 and 6
so could i use pins 2,3,4,7,8,9,10,11 ???
i'll get there in the end

AWOL

Quote
so do not use pins 5 and 6

Why not?

Did you read the page I linked?

markthespark

yas i read the page and got a little confused i have a mega so it should not be a problem to me

markthespark

so how do i make them fade between each phase ?

Go Up