//(Diagram to follow)
/*
Basic 7-seg LED dice
Generates a random integer between 0 and 6,
then outputs it to an HDSP-C3G3 7 segment display every 5 seconds.
Kate A. Glover – kateglover@gmail.com
*/
/* This matches up each lettered pin-out on the 7-seg display
with a given port number on the Arduino Uno*/
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
void setup() {
// initialize each digital pin as an output.
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
}
// loops to produce a number every 5 seconds (5000 milliseconds)
void loop()
{
//creates an int from a number returned by the generateNumber() method.
int randNo = generateNumber();
//calls a different method depending on which number was generated
switch (randNo)
{
case 1:
displayOne();
break;
case 2:
displayTwo();
break;
case 3:
displayThree();
break;
case 4:
displayFour();
break;
case 5:
displayFive();
break;
case 6:
displaySix();
break;
/* this default calls a method which displays an “E” (for error)
and only shows when a number is called which is out of range. */
default:
displayFault();
break;
}
//5000 millisecond (5 second) delay before it calls another number
delay(5000);
}
//generates the random number and returns it to the main loop() method.
int generateNumber()
{
randomSeed(analogRead(0));
int randomNumber = random(1, 6);
return randomNumber;
}
// Fires up pins to create a number 1 and shuts off the other pins
void displayOne()
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
// Fires up pins to create a number 2 and shuts off the other pins
void displayTwo()
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
// Fires up pins to create a number 3 and shuts off the other pins
void displayThree()
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
// and so on and so forth
void displayFour()
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
// You get the idea
void displayFive()
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
// … and six…
void displaySix()
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
// Sets the display to show an E (for error)
void displayFault()
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}