Sunday, June 20, 2010

Arduino learning-9

randomSeed(seed)
Sets a value, or seed, as the starting point for the random() function.
randomSeed(value); // sets ‘value’ as the random seed
Because the Arduino is unable to create a truly random number, randomSeed allows you to place a variable, constant, or other function into the random function, which helps to generate more random "random” numbers. There are a variety of different seeds, or functions, that can be used in this function including millis() or even analogRead() to read electrical noise through an analog pin.
random(max)
random(min, max)
The random function allows you to return pseudo-random numbers within a range specified by min and max values.
value = random(100, 200); // sets 'value' to a random
// number between 100-200
Note: Use this after using the randomSeed() function.
The following example creates a random value between 0-255 and outputs a PWM signal on a PWM pin equal to the random value:
int randNumber; // variable to store the random value
int led = 10; // LED with 220 resistor on pin 10
void setup() {} // no setup needed
void loop()
{
randomSeed(millis()); // sets millis() as seed
randNumber = random(255); // random number from 0-255
analogWrite(led, randNumber); // outputs PWM signal
delay(500); // pauses for half a second
}
random
Related Posts Plugin for WordPress, Blogger...
Disclaimer: All the information in this blog is just gathered from different sites in the web and placed here and I am not the owner for these content

Popular Projects

Followers

My Blog List

Give Support

Give Support
Encourage me Through Comments & by Following