Arduino Dice 7

A device that can use for many ways

There has two button, on is let the dice LED random shin

The other is let the LED shin in regular pattern

This project referred:https://www.instructables.com/id/Arduino-Dice-1/

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1:

prepare:

1.Bread board

2. 2 button

3.28 wire

4.7 resistance

Step 2:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #e4af09; min-height: 14.0px}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #454545} span.s1 {text-decoration: underline}

*///set up the button pinsvolatile int switchPin = 2;//digital pin for display button (on an interupt)int selectPin = 11;//digital pin for die rollbool spinReverse = false;//toggled per selection//create and populate the 2 dimensional array for the die numbersconst int leds[6][6] { {7, 0}, {3, 4, 0}, {1, 6, 7, 0}, {1, 2, 5, 6, 0}, {1, 2, 5, 6, 7, 0}, {1, 2, 3, 4, 5, 6}};//create and populate the 2 dimensional array for the spinner//this is the clockwise orderconst int spinnerLeds[6][1] { {1}, {2}, {4}, {6}, {5}, {3}};volatile int buttonMenu = 0;//variable to check if button is pressedint buttonState = 0;//condition of the button, on or offint randomRoll = 0;//the die roll random numberint loopDelay = 400;//delay used for looping through all numbersint rollDelay = 2000;//delay used for displaying die rollbool currentState = true;void setup() { Serial.begin(9600); pinMode(4 , OUTPUT);//topLeft pinMode(5 , OUTPUT);//topRight pinMode(6 , OUTPUT);//middleLeft pinMode(7 , OUTPUT);//middleRight pinMode(8 , OUTPUT);//bottomLeft pinMode(9 , OUTPUT);//bottomRight pinMode(10 , OUTPUT);//center /* pinMode(topLeft , OUTPUT);//setup the LED pins pinMode(topRight , OUTPUT); pinMode(middleLeft , OUTPUT); pinMode(middleRight , OUTPUT); pinMode(center , OUTPUT); pinMode(bottomLeft , OUTPUT); pinMode(bottomRight , OUTPUT); */ pinMode(selectPin , INPUT_PULLUP);//die roll button with pullup resistor pinMode(switchPin , INPUT_PULLUP);//loop button set up as interupt pin attachInterrupt(digitalPinToInterrupt(switchPin), SwitchButton, CHANGE);//point interupt to the SwitchButton function TurnOff();//start with all off}void loop() { SelectButton();//check for a die roll button press //if the interupt has detected a display button press, act on that here switch (buttonMenu) { case 1: { if (currentState) { TurnOff(); } currentState = false; break; } case 2://loop through all numbers { for (int i = 0; i < 6; i++) { TurnOn(i); } currentState = true; break; } case 3://spin clockwise { Spinner(); currentState = true; break; } case 4://spin anti clockwise { SpinnerReverse(); currentState = true; break; } }}//random roll mode//pressing button will cause all LEDs to flash then display numbervoid SelectButton(){ CheckButtonState();//was it pressed? if (buttonMenu == 99)//yes { spinReverse = !spinReverse; //toggle direction for (int x = 0; x < 6; x++)//run the spinner 3 times { if (spinReverse) { SpinnerReverse(); } else { Spinner(); } } randomSeed(analogRead(0));//reset the random seed on an unconnected analog pin randomRoll = random(0, 6);//get die roll Flash();//do the flashing TurnOn(randomRoll);//turn on the reauired LEDs delay(rollDelay);//keep the LEDs (die number) visible for a while TurnOff();//reset the LEDs buttonMenu = 1;//reset and wait for another die roll }}//loop through all numbers etcvoid SwitchButton(){ int reading = digitalRead(switchPin); if (reading != buttonState) { buttonState = reading; if (buttonState == HIGH) { buttonMenu++; if (buttonMenu > 4) { buttonMenu = 1; } } }}//checks the state of the die roll button pressvoid CheckButtonState(){ int currentReading = digitalRead(selectPin);//has the current button been pressed if (currentReading == 0)//has button been pressed { buttonMenu = 99;//yes, always set this to 99 }}//turns all LEDs offvoid TurnOff(){ for (int x = 4; x < 11; x++) { digitalWrite(x, LOW); }}//turn all LEDs onvoid TurnAllOn(){ for (int x = 4; x < 11; x++) { digitalWrite(x, HIGH); }}//flash all LEDs by turning them on and off with an intervalvoid Flash(){ for (int i = 0; i <= 6; i++)//do it six times { TurnAllOn(); delay(50); TurnOff(); delay(1000); }}void Spinner(){ for (int i = 0; i < 6; i++)//do six leds { TurnOnLed(spinnerLeds[i][0]); delay(1000); }}void SpinnerReverse(){ for (int i = 5; i >= 0; i--) //do six leds { TurnOnLed(spinnerLeds[i][0]); delay(1000); }}//turn on the required LEDs to display the correct number//input parameter (dieRoll) contains the random number//to understand the array (leds) look at the way in which it has been declared at the top of the program//each row of the array shows which LEDs will need to be turned on for a given die roll number//for example, if there was a dieRoll of 2, the second row of the array will be used ({3, 4, 0}),// so LEDs numbered 3 and 4 will need to be lit,// zero is the end of line marker which will break out of the loopvoid TurnOn(int dieRoll){ TurnOff();//start again with new die roll for (int i = 0; i <= 6; i++)//loop through the array { if (leds[dieRoll][i] == 0)//end of line marker so exit the loop { break; } TurnOnLed(leds[dieRoll][i]); } delay(loopDelay);}//turn on the individual LED according to the list in the array for that number//if the LED is already on, turn it off or vice-versa, used for the spinnersvoid TurnOnLed(int whichLed){ int newValue = whichLed + 3;//adjust for the Arduino pin number if (digitalRead(newValue) == LOW) { digitalWrite(newValue, HIGH); } else { digitalWrite(newValue, LOW); } CheckButtonState();//was the die roll button pressed?}

Step 3:

Youtube:

Be the First to Share

    Recommendations

    • Instrument Contest

      Instrument Contest
    • Make it Glow Contest

      Make it Glow Contest
    • STEM Contest

      STEM Contest

    Discussions