Hobby, Microcontrollers, Projects, Tutorial

Arduino tutorial (Part 4, random numbers)

In this forth part of Arduino tutorial, I will show how to use random commands to generate random numbers. One of the applications is to control devices randomly.

Program

Let’s implement a program which generate random numbers in the serial screen, here is the algorithm.

Clicking in the button in the upper right corner of Arduino`s window, to see the serial screen. You must see numbers being generated randomly each second.

A new command was used:

random(100)

This command serve to generate random numbers, the number between the brackets shows the random number generation band, in this exemple go from 0 to 99. The “long” in the first line is a “int” classification, which can occupy 4 or 8 memory bytes while the “int” can only have 4 bytes.

Led control with random numbers

This circuit control leds using random commands.

Here is the algorithm.

The leds in output 2, 10 and 12 turn on when the random number is 1, 2 or 4 respectively. When a led is turned on others stay turned off.

Random movement vehicle

Below we have another exemple, a car which randomly moves forward and backward.

Note that this project uses a H bridge, to know more click in the button below.

H bridge H bridgeClick here

 

Here we have the algorithm.

Here we have the car working.

 

 

About Pedro Ney Stroski

Leave a Reply

Your email address will not be published. Required fields are marked *