[vc_row][vc_column width=”1/1″][vc_column_text]The components used in this experiment are shown below:
The components are seven LEDs (colors can be chosen according to your taste,seven 470 ohm resistors,one button,11 jumper wires and one breadboard.
[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Schematics” tab_id=”1392617429-1-80″][vc_column_text][/vc_column_text][/vc_tab][vc_tab title=”Wiring Diagram” tab_id=”1392617429-2-100″][vc_column_text][/vc_column_text][/vc_tab][vc_tab title=”Visualization picture” tab_id=”1392618058724-2-0″][vc_column_text][/vc_column_text][/vc_tab][vc_tab title=”Arduino Code” tab_id=”1392618645255-3-2″][vc_column_text]Arduino Code:
int ledPins[7] = {2, 3, 4, 5, 6, 7, 8};
int dicePatterns[7][7] = {
{0, 0, 0, 0, 0, 0, 1}, // 1
{0, 0, 1, 1, 0, 0, 0}, // 2
{0, 0, 1, 1, 0, 0, 1}, // 3
{1, 0, 1, 1, 0, 1, 0}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
{1, 1, 1, 1, 1, 1, 0}, // 6
{0, 0, 0, 0, 0, 0, 0} // BLANK
};
int switchPin = 9;
int blank = 6;
void setup()
{
for (int i = 0; i < 7; i++)
{
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
randomSeed(analogRead(0));
}
void loop()
{
if (digitalRead(switchPin))
{
rollTheDice();
}
delay(100);
}
void rollTheDice()
{
int result = 0;
int lengthOfRoll = random(15, 25);
for (int i = 0; i < lengthOfRoll; i++) {
result = random(0, 6); // result will be 0 to 5 not 1 to 6
show(result);
delay(50 + i * 10);
}
for (int j = 0; j < 3; j++) {
show(blank);
delay(500);
show(result);
delay(500);
}
}
void show(int result)
{
for (int i = 0; i < 7; i++) {
digitalWrite(ledPins[i], dicePatterns[result][i]);
}
}[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.