How to change the behaviour of a loop - questions

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 :slight_smile: ?
..and feel free to comment anything useful.

Many thanks in advance,

Fliffi

Have a look at the state change example in the IDE.

"digitalWrite(i + pinOffset, (eyes[value - 1] == 1)?HIGH:LOW); // what is happening here ???"
This is a programmer showing off.
It's an example of using the ternary operator (called the "Conditional Operator" in C++).
* *(condition) ? (if_true) : (if_false)* *
Which is basically the same as:
* *if (condition)    if_true; else    if_false;* *
In your example the ternary operator is setting the value of the digitalWrite function either HIGH or LOW.

Thank you all.

Is there any method to rewrite:
digitalWrite(i + pinOffset, (eyes[value - 1] == 1)?HIGH:LOW);

even if it cost more lines, to get a more uderstandable form ?

Fliffi

Fliffi:
Thank you all.

Is there any method to rewrite:
digitalWrite(i + pinOffset, (eyes[value - 1] == 1)?HIGH:LOW);

even if it cost more lines, to get a more uderstandable form ?

Fliffi

Using pinOffset to calculate the pin number is a bit odd, since there is already an array set up with the pin numbers, and the array is used in setup() when setting the pins as outputs.

int pin[] = {2, 3, 4, 5, 6, 7, 8}; // define a pin array
int pinOffset = 2; // first LED on pin 2.

As for the rest of the command, most arduino code defines HIGH as 1 and LOW as 0, so the value from the eyes array can be used directly (note that this may not work on all boards, some of the newer ones define HIGH and LOW differently).

The simpler form of that line would be:

digitalWrite(pin[i], eyes[value - 1][i]);

For the original question, of how to "tumble" the dice for three seconds, when the button is pressed, start a loop that displays a different random number on the dice periodically until a total of three seconds have elapsed.

Thank you all !

I succesfully implemented a simpler code form for the array:

digitalWrite(i + pinOffset, (eyes[value - 1] == 1)?HIGH:LOW);
is now:
digitalWrite(pin_, eyes[value - 1]);
and it works perfectly :slight_smile: ._

I will test Delta_G approach aswell.
Now Im looking at StateChangeDetection in the IDE to get the "tumble" effect, and/or David_2018 idea.

Fliffi:
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.

Does this mean the dice tumble while the button is pressed and then an additional three seconds after the button is released?