Interactive Assignment

                                                                   

Electronic Die 
Step 1.) Gather supplies 
-1 BreadBoard
-1 Arduino Uno 
-1 USB cable (for Arduino) 
-1 7-segment display 
-1 tilt-sensor
-1 10K resistor 
-13 jumper wires 
Step 2.) Wire the 7-segment display 
-Secure display onto BreadBoard 

Common Anode Seven Segment Display with Pin Numbering

Display pins 
-Using jumper wires, connect the display to the Arduino as follows:
Arduino Pin #2: Pin #7 on display 
Arduino Pin #3: Pin #6 on display
Arduino Pin #4: Pin #4 on display
Arduino Pin #5: Pin #2 on display
Arduino Pin #6: Pin #1 on display
Arduino Pin #7: Pin #9 on display
Arduino Pin #8: Pin #10 on display 
Arduino Pin #9: Pin #5 on display 
  
(https://www.hacktronics.com/Tutorials/arduino-and-7-segment-led.html)-Easier chart 
Finished wiring for display 
Step 3.) Wire the tilt-sensor 
-Insert tilt-sensor on an empty row on the BreadBoard 
-Take a jumper and connect one leg of the tilt-sensor to pin 12 on the Arduino 
-Take a jumper and connect the other leg of the tilt-sensor to the 5V pin 
-Using the 10K resistor, place one side on the row connected to pin 12, and the other side on an empty row
-On the row that contains just the resistor, use a jumper and connect it to a ground pin 
Finished wiring with tilt-sensor 
Step 4.) Upload the code
-Open up Arduino and insert this code into a brand new sketch, then click upload 
Code: 
// Define the LED digit patters, from 0 – 9
// Note that these patterns are for common cathode displays
// For common anode displays, change the 1’s to 0’s and 0’s to 1’s
// 1 = LED on, 0 = LED off, in this order:
//                                    Arduino pin: 2,3,4,5,6,7,8
byte seven_seg_digits[10][7] = {
  { 1, 1, 1, 1, 1, 1, 0 }, // = 0
  { 0, 1, 1, 0, 0, 0, 0 }, // = 1
  { 1, 1, 0, 1, 1, 0, 1 }, // = 2
  { 1, 1, 1, 1, 0, 0, 1 }, // = 3
  { 0, 1, 1, 0, 0, 1, 1 }, // = 4
  { 1, 0, 1, 1, 0, 1, 1 }, // = 5
  { 1, 0, 1, 1, 1, 1, 1 }, // = 6
  { 1, 1, 1, 0, 0, 0, 0 }, // = 7
  { 1, 1, 1, 1, 1, 1, 1 }, // = 8
  { 1, 1, 1, 0, 0, 1, 1 } // = 9
};
const int tiltSensorPin = 12; // the number of the tilt Sensor pin
int tiltSensorState = 0;
int count = 0;
void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  writeDot(0);  // start with the “dot” off
  pinMode(tiltSensorPin, INPUT);
}
void writeDot(byte dot) {
  digitalWrite(9, dot);
}
void sevenSegWrite(byte digit) {
  byte pin = 2;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pin, seven_seg_digits[digit][segCount]);
    ++pin;
  }
}
void throwDice() {
  writeDot(1);
  for (byte count = 30; count > 0; –count) {
    delay(40);
    sevenSegWrite(random(1, 6));
  }
  writeDot(0);
}
void loop() {
  tiltSensorState = digitalRead(tiltSensorPin);
  if (tiltSensorState == LOW) {
    count++;
    delay(10);
  }
  if (count == 20) {
    throwDice();
    count = 0;
  }
}
Step 5.) Using the die
-In order for the “die” to work, you must shake the BreadBoard. The numbers on the display will begin to randomize and will eventually choose a number at random once you stop shaking the BreadBoard. 
Finished Result: