Rainbow Dice

You need to use a die, but you don't have one! No worries, you pull out your laptop, Launchpad, and Sidekick to roll a fair die.

BeginnerWork in progress2 hours527
Rainbow Dice

Things used in this project

Hardware components

EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
Breadboard (generic)
Breadboard (generic)
×1
TI Breadboard BoosterPack
×1
7 Segment LED Display, InfoVue
7 Segment LED Display, InfoVue
×1
Jumper wires (generic)
Jumper wires (generic)
×13
Tilt Switch
×1
3 Color LED
×1

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

Board Schematics

Code

Code

C/C++
/*
Rainbow Dice, created by team X.we11am
*/

#include <stdio.h>
#include <stdlib.h>
// Define the LED digit patterns for 1-6 in a 2 dimensional array.
// The 2D array (an array of arrays or a matrix) has 6 arrays that each
// contain 7 values.
// 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:
// Common Anode version
byte seven_segment_digits[6][7] = { { 1,0,0,1,1,1,1 }, // display '1'
                                     { 0,0,1,0,0,1,0 }, // display '2'
                                     { 0,0,0,0,1,1,0 }, // display '3'
                                     { 1,0,0,1,1,0,0 }, // display '4'
                                     { 0,1,0,0,1,0,0 }, // display '5'
                                     { 0,1,0,0,0,0,0 }, // display '6'
                                   };



/* Set up tilt switch as input, LED pins, and 7-segment display as output
 */
int tiltSwitchPin = 2; //set pin 2 as tiltSwitchPin
int switchStatus = 0; //initialize switchStatus
#define RLED RED_LED 
#define BLED BLUE_LED 
#define GLED GREEN_LED
#define RED 19 // pin 19 is always PWM capable according to LaunchPad standard
#define GREEN 18 // may need to change this for your LaunchPad
#define BLUE 17 // may need to change this for your LaunchPad 
void setup() {
  randomSeed(analogRead(0));   //Randomises the seed for random function by collecting noise from 0 
    
  //set Switch as input
  pinMode(tiltSwitchPin, INPUT); 

  //set 7-segment display as output
  pinMode(3, OUTPUT); // set segment A as output
  pinMode(4, OUTPUT); // set segment B as output
  pinMode(5, OUTPUT); // set segment C as output
  pinMode(6, OUTPUT); // set segment D as output
  pinMode(7, OUTPUT); // set segment E as output
  pinMode(8, OUTPUT); // set segment F as output
  pinMode(9, OUTPUT); // set segment G as output
  
  //set luanchpad LED as output
  pinMode(RLED, OUTPUT); 
  pinMode(BLED, OUTPUT); 
  pinMode(GLED, OUTPUT); 

  //set RGB LED as output
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  // start with display off
  for(int i = 3; i <= 9; i++) { 
    digitalWrite(i, HIGH);
  }
}

/* In the loop section we will begin displaying the different numbers.
 * Add delay() or sleep() to give some time between the numbers changing.
 */
int pin = 3; //First pin corresponding to the 7-segment display
int result = -1; //Int representing the die roll
bool rolled = false; //Boolean flag for state of tilt switch
void loop() {
  //Read tilt switch status
  switchStatus = digitalRead(tiltSwitchPin);
  
  //If the board is tilted up, shuffle
   if (switchStatus == HIGH){
    rolled = false;
    
    //shuffle betweeen 1 to 6 and between different colors until the board is tilted down
    for (int count = 0; count <= 5; ++count) {
      //shuffle between numbers
      pin = 3;
      //This loop goes through each pin and changes the display 
      for (int segCount = 0; segCount < 7; ++segCount) {
        digitalWrite(pin, seven_segment_digits[count][segCount]);
        ++pin;
        }  
      delay(50); // delay for 50 milliseconds between each number
      
      //shuffle between colors
      number_to_color_luanchpad(count); 
      number_to_color_LED(count);    
      
      //back to 0 
      if (count == 5){
        count = 0;

        }   
    
      //exits the loop if the board is tilted down
      switchStatus = digitalRead(tiltSwitchPin);
      if (switchStatus == LOW){
        break;
        }
      }
      
    }
    
  //If the board is tilted down, display result
  else if (switchStatus == LOW && rolled == false){
    rolled = true;
    result = random(6);
    pin = 3;
    for (int segCount = 0; segCount < 7; ++segCount) {
      digitalWrite(pin, seven_segment_digits[result][segCount]);
      ++pin;
      }
    delay(100); // delay for 0.1 second
   } 

   //color effects
   number_to_color_luanchpad(result+1);
   number_to_color_LED(result+1);     
}



//Function to display unique colors for each number rolled on the launchpad
void number_to_color_luanchpad(int num){
   if(num == 1){
    //Red
    digitalWrite(RLED, HIGH);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, LOW);
    }
   
   else if(num == 2){
    //Purple
    digitalWrite(RLED, HIGH);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, HIGH);
    }
   
   else if(num == 3){
    //Green
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, HIGH);
    digitalWrite(BLED, LOW);
    }
   
   else if(num == 4){
    //Cyan
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, HIGH);
    digitalWrite(BLED, HIGH);
    }
   
   else if(num == 5){
    //Yellow
    digitalWrite(RLED, HIGH);
    digitalWrite(GLED, HIGH);
    digitalWrite(BLED, LOW);
    }
   
   else if(num == 6){
    //White
    digitalWrite(RLED, HIGH);
    digitalWrite(GLED, HIGH);
    digitalWrite(BLED, HIGH);
    }    
}

//Function to display unique colors for each number rolled on the LED
void number_to_color_LED(int num){
   if(num == 1){
    //Red
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, HIGH);
    digitalWrite(BLUE, HIGH);
    }
   
   else if(num == 2){
    //Purple
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, HIGH);
    digitalWrite(BLUE, LOW);
    }
   
   else if(num == 3){
    //Green
    digitalWrite(RED, HIGH);
    digitalWrite(GREEN, LOW);
    digitalWrite(BLUE, HIGH);
    }
   
   else if(num == 4){
     //Cyan
    digitalWrite(RED, HIGH);
    digitalWrite(GREEN, LOW);
    digitalWrite(BLUE, LOW);
    }
   
   else if(num == 5){
    //Yellow
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, LOW);
    digitalWrite(BLUE, HIGH);
    }
   
   else if(num == 6){
    //White
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, LOW);
    digitalWrite(BLUE, LOW);
    }    
}

Credits

Daniel Han
1 project • 0 followers
Contact
Ben Thomas
1 project • 0 followers
Contact
Irpan Yierpan
1 project • 0 followers
Contact
Carla Sipahioglu
1 project • 0 followers
Contact
Gerardo Torres
2 projects • 1 follower
Contact
Eunice Tan
1 project • 0 followers
Contact
Shannon Campbell
1 project • 0 followers
Contact
Elijah Schwartz
2 projects • 1 follower
Contact
Ruby Polanco
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.