Home Arduino random value on a TIL311

random value on a TIL311

Another Arduino example, mainly again to demonstrate generating random numbers with a random seed.

In this case we want a number between 0 and 16.

Code

/* Control lines */
#define BLANK_INPUT 6

/* Data bus */
#define LATCH_DATA_A 2
#define LATCH_DATA_B 3
#define LATCH_DATA_C 4
#define LATCH_DATA_D 5

void display (uint8_t value)
{
/* Send data to the latch */
digitalWrite (LATCH_DATA_A, bitRead (value, 0));
digitalWrite (LATCH_DATA_B, bitRead (value, 1));
digitalWrite (LATCH_DATA_C, bitRead (value, 2));
digitalWrite (LATCH_DATA_D, bitRead (value, 3));
}

void setup ()
{
Serial.begin(9600);
// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));
//setup the pins
pinMode (BLANK_INPUT, OUTPUT);
pinMode (LATCH_DATA_A, OUTPUT);
pinMode (LATCH_DATA_B, OUTPUT);
pinMode (LATCH_DATA_C, OUTPUT);
pinMode (LATCH_DATA_D, OUTPUT);
digitalWrite (BLANK_INPUT, LOW);
}

void loop ()
{
long randomX;
//random number between 0 and 84
randomX = random(0,16);
display (randomX);
delay (1000);
}

You may also like