Hello arduino community,
this project is about an LED dice: Arduino pins 2 - 8 are configured as
output pins and connect to 7 LEDs which resemble a pattern similar to
a dice. If a button switch is pressed the routine showdice is called
along with a random number between 1 and 7 passed to showdice.
The int "value" is for counting inside the loop.
Currently, only for the time the button is been kept pressed the LEDs are
cycled through, but it should keep cycling for a short time.
I understand part of the code e.g. setting up a 2-dimensional array for
the dice eyes, but i can not modify it so that when the button is pressed
once and let go, it keeps cycling the LEDs for e.g. 3 seconds before it stops,
to "simulate" the tumbling of the dice.
#define WAIT 20 // pause 20ms
int eyes[6][7] = {{0, 0, 0, 1, 0, 0, 0}, // dice shows 1
{1, 0, 0, 0, 0, 0, 1}, // dice shows 2
{1, 0, 0, 1, 0, 0, 1}, // dice shows 3
{1, 0, 1, 0, 1, 0, 1}, // dice shows 4
{1, 0, 1, 1, 1, 0, 1}, // dice shows 5
{1, 1, 1, 0, 1, 1, 1}}; // dice shows 6
int pin[] = {2, 3, 4, 5, 6, 7, 8}; // define a pin array
int pinOffset = 2; // first LED on pin 2.
int buttonPin = 13; // set button switch on pin 13
void setup() { // set all pins of
for(int i = 0; i < 7; i++) // the array as
pinMode(pin[i], OUTPUT); // OUTPUT pins
pinMode(buttonPin, INPUT);
}
void loop() {
if(digitalRead(buttonPin) == HIGH) // check button: if pressed generate a random no.
showdice(random(1, 7)); // between 1 and 7 and call showdice
}
void showdice(int value) { // showdice: 7 times in this loop do digitial write below:
for(int i = 0; i < 7; i++)
digitalWrite(i + pinOffset, (eyes[value - 1][i] == 1)?HIGH:LOW); // what is happening here ???
delay(WAIT); // a brief pause
}
Any idea how to the code needs to be changed, without rewriting everything from scratch ?
..and feel free to comment anything useful.
Many thanks in advance,
Fliffi