Home ArduinoArduino Basics Random function example

Random function example

by shedboy71

In this example we use the RGB led again but are introducing a couple of new programming topis. We introduce the switch case structure and the random and randomSeed functions. We are using the same circuit as before, here it is as a reminder

RGB to ARduino

RGB to ARduino

The following is from the reference documentation, rather than rewrite the wheel here is an explanation.

The switch…case controls the flow of programs by allowing programmers to specify different code that should be executed in various conditions. In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run.

The break keyword exits the switch statement, and is typically used at the end of each case. Without a break statement, the switch statement will continue executing the following expressions (“falling-through”) until a break, or the end of the switch statement is reached.

randomSeed() initializes the pseudo-random number generator, causing it to start at an arbitrary point in its random sequence. This sequence, while very long, and random, is always the same.

If it is important for a sequence of values generated by random() to differ, on subsequent executions of a sketch, use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin.

The random function generates pseudo-random numbers.

Lets look at the example, we will generate a random number from 0 to 6 (remember the random number starts at 0 unless you state a min and max number), the randomSeed to help generate a truely random number and a switch case to flash the RGB led in a particular sequence

Code

[codesyntax lang=”cpp”]

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
 
 
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);  
  randomSeed(analogRead(0));
}
 
void loop()
{
  int randColour = random(7);
  switch(randColour)
  {
    case 0 :  setColour(0, 255, 255); 
              delay(1000);
              break;
    case 1 :  setColour(255, 255, 0); 
              delay(1000);
              break;   
    case 2 :  setColour(255, 0, 255);  
              delay(1000);
              break; 
    case 3 :  setColour(0, 255, 255); 
              delay(1000);
              break;   
    case 4 :  setColour(255, 0, 0); 
              delay(1000);
              break;  
    case 5 :  setColour(0, 0, 0); 
              delay(1000);
              break;   
    case 6 :  setColour(128, 128, 128); 
              delay(1000);
              break;  
    default:  setColour(255, 255, 255); 
              delay(1000);
              break;               
  }

}
 
void setColour(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

[/codesyntax]

You may also like