Skip to content

Instantly share code, notes, and snippets.

@janzeteachesit
Last active January 10, 2017 09:05
Show Gist options
  • Save janzeteachesit/b8df19375d92716482d39e299d2d3e80 to your computer and use it in GitHub Desktop.
Save janzeteachesit/b8df19375d92716482d39e299d2d3e80 to your computer and use it in GitHub Desktop.
LED Arduino Dice code updated for circuits.io by janzeteachesit - https://circuits.io/circuits/3668120-led-dice-v_02
/*
LED Arduino Dice
Original Arduino code by Davuzz11
http://www.instructables.com/id/Arduino-Led-Dice/
http://www.instructables.com/member/Davide311/
updated for circuits.io by janzeteachesit
https://circuits.io/circuits/3668120-led-dice-v_02
20170110
Parts List:
Breadboard x 1
Arduino x 1
LEDs of any kind (I use 5mm Red Leds) x 7
10k Resistor (brown black orange) x 1
220 or 330 Resistor (red red brown or orange orange brown) x 7
Pushbutton x 1
*/
//LED Digital Pin #s
int ledA = 2;
int ledB = 3;
int ledC = 4;
int ledD = 5;
int ledE = 6;
int ledF = 7;
int ledG = 8;
//Pushutton Digital pin and declare "State"
int button = 9;
int buttonState = 0;
//ran will be a random number betwen 1 and 6
long ran;
//wait is the time of delay
int wait = 2000;
// array to use for pre-random light show
// see https://www.arduino.cc/en/Reference/Array for info on Arrays
int ledSpin[] = {3, 7, 6, 4, 8, 5};
void setup ()
{
Serial.begin(9600);
//Set the mode of the LED Digital Pins as Output
pinMode (ledA, OUTPUT);
pinMode (ledB, OUTPUT);
pinMode (ledC, OUTPUT);
pinMode (ledD, OUTPUT);
pinMode (ledE, OUTPUT);
pinMode (ledF, OUTPUT);
pinMode (ledG, OUTPUT);
//Set the mode of the button Digital Pin as an Input
pinMode (button, INPUT);
//This code line is necessary to generate a correct random number
// 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));
}
void loop()
{
//Read the status of the button
buttonState = digitalRead(button);
if (buttonState == HIGH){
// setup up lightshow
// info on iteration (loops) using for, see https://www.arduino.cc/en/Reference/For
int i = 0;
int j = 0;
for (j = 1; j < 4; j++) {
for (i = 0; i < 7; i++) {
digitalWrite (ledSpin[i], HIGH);
Serial.println(i);
delay (wait/10);
digitalWrite (ledSpin[i], LOW);
}
Serial.println(j);
}
//Randomize from 1 to 6
ran = random(1, 7);
Serial.println(ran);
//Number 1!
if (ran == 1){
digitalWrite (ledA, HIGH);
delay (wait);
}
//Number 2!!
if (ran == 2){
digitalWrite (ledB, HIGH);
digitalWrite (ledC, HIGH);
delay (wait);
}
//Number 3!!!
if (ran == 3){
digitalWrite (ledA, HIGH);
digitalWrite (ledB, HIGH);
digitalWrite (ledC, HIGH);
delay (wait);
}
//Number 4!!!!
if (ran == 4){
digitalWrite (ledB, HIGH);
digitalWrite (ledC, HIGH);
digitalWrite (ledD, HIGH);
digitalWrite (ledE, HIGH);
delay (wait);
}
//Number 5!!!!!
if (ran == 5){
digitalWrite (ledA, HIGH);
digitalWrite (ledB, HIGH);
digitalWrite (ledC, HIGH);
digitalWrite (ledD, HIGH);
digitalWrite (ledE, HIGH);
delay (wait);
}
//Number 6!!!!!!
if (ran == 6){
digitalWrite (ledB, HIGH);
digitalWrite (ledC, HIGH);
digitalWrite (ledD, HIGH);
digitalWrite (ledE, HIGH);
digitalWrite (ledF, HIGH);
digitalWrite (ledG, HIGH);
delay (wait);
}
}
//If the button is not pressed, sets off the leds
digitalWrite (ledA, LOW);
digitalWrite (ledB, LOW);
digitalWrite (ledC, LOW);
digitalWrite (ledD, LOW);
digitalWrite (ledE, LOW);
digitalWrite (ledF, LOW);
digitalWrite (ledG, LOW);
}
//....Finish!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment