Electronic dice programming

Hello everyone!

I am working on making an electronic dice, it has 7 diode's in this shape:

0 0 0
0
0 0 0

With this I can make all the numbers like a dice has.
I got it configured in such a way that with the press of a button the dice will show the next number:
Example: The dice shows 1 -> PushButton -> 2.

I also got a 2nd button and I want that to work like this:
If I press the 2nd button, the dice will show the number that came before the current one.
Example: The dice shows 5 -> PushButton2 -> 4.

I'm using an Internal PULL UP for the 2nd one and a PULL UP transistor for the 1st one. This is my code:

//A program that simulates a real life dice.

//All the leds that are currently attached to the board
int led = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int led6 = 7;
int led7 = 8;
int btn = 9;
int btn2 = 10;


// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(btn, INPUT); //button 1
  pinMode(btn2, INPUT_PULLUP); //button 2
}

void clickTheButton() {  //Button 1. 
  while( digitalRead( btn ) == LOW ) {}
  while( digitalRead( btn ) == HIGH ) {}
}


// the loop routine runs over and over again forever:
void loop() {
    
    digitalWrite(led5, HIGH);
    clickTheButton();
    digitalWrite(led5, LOW);
    
    digitalWrite(led, HIGH);
    digitalWrite(led4, HIGH);
    
    clickTheButton();
    
    digitalWrite(led, LOW);
    digitalWrite(led4, LOW);
    
    digitalWrite(led3, HIGH);
    digitalWrite(led5, HIGH);
    digitalWrite(led6, HIGH);
    
    clickTheButton();
    
    digitalWrite(led3, LOW);
    digitalWrite(led5, LOW);
    digitalWrite(led6, LOW);
    
    digitalWrite(led3, HIGH);
    digitalWrite(led6, HIGH);
    digitalWrite(led4, HIGH);
    digitalWrite(led, HIGH);  
    
    clickTheButton();
    digitalWrite(led3, LOW);
    digitalWrite(led6, LOW);
    digitalWrite(led4,  LOW);
    digitalWrite(led, LOW);
    
    digitalWrite(led3, HIGH);
    digitalWrite(led6, HIGH);
    digitalWrite(led4, HIGH);
    digitalWrite(led, HIGH);  
    digitalWrite(led5, HIGH);
    
    clickTheButton();
    
    digitalWrite(led5, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led6, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led, LOW);
    
    digitalWrite(led3, HIGH);
    digitalWrite(led7, HIGH);
    digitalWrite(led4, HIGH);
    digitalWrite(led2, HIGH);  
    digitalWrite(led, HIGH);
    digitalWrite(led6, HIGH);
    
    clickTheButton();
    
    digitalWrite(led, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led7, LOW);
    digitalWrite(led4,  LOW);
    digitalWrite(led6, LOW);
    digitalWrite(led2, LOW);
}

hi

is the code that you posted working like you say for button1?
You say you are using pull-up transistors? do you mean pull-up resistors?

If you want your Dice to display RANDOM numbers, then you don't need two buttons. Just need one button to tell your Arduino when to make a new number.
And then maybe the easiest way would be to use Switch Case.

You can learn about SwitchCase here:

And here is another project of an Arduino Dice, which produces random numbers, and actually also used SwitchCase...

Good luck!
=)

Yeah that code is working.
And I meant transistors... but that is not so important right now.

I want the first button to go the next number when pressed. that's easy.. I use the clickTheButton() function to check for a HIGH or LOW state and when that is done it display's the next number...

But now I have to get the previous number on the led's when the 2nd button is pressed and I do not know how to do that...

Later on I want to make the dice display random numbers for a certain amount of time until it stops on a random number (just like a real dice)...

Take a look at switch/case
Yours dice will always be in one of 7 states (if you include it being blank). For each state use switch/case to display the appropriate LEDs. Read the buttons. If the NEXT button is pressed increment the state number. If the PREVIOUS button is pressed decrement the state number. Deal with the rollover either way.

You should consider only changing the state number when a button becomes pressed, not while it is held down, and debouncing the inputs would be a good idea too.

sander1095:
And I meant transistors... but that is not so important right now.

ah, sorry. i had never heard about it. Why do you use them? How are you connecting your dice?

I use the clickTheButton() function to check for a HIGH or LOW state and when that is done it display's the next number...

So, you need another function, clickTheOtherButton(), to deal with the other switch. Of course, that isn't going to work, since clickTheButton() blocks until the switch is pressed and released.

You need to ditch the clickTheButton() function, and look at the state change detection example. That will show you how to increment, or decrement, a counter when a switch BECOMES released. Increment when one switch becomes released. Decrement when the other switch becomes released.

Then, use a switch statement to display some value on the dice, based on the counter.

Sander.

PaulS:

I use the clickTheButton() function to check for a HIGH or LOW state and when that is done it display's the next number...

So, you need another function, clickTheOtherButton(), to deal with the other switch. Of course, that isn't going to work, since clickTheButton() blocks until the switch is pressed and released.

You need to ditch the clickTheButton() function, and look at the state change detection example. That will show you how to increment, or decrement, a counter when a switch BECOMES released. Increment when one switch becomes released. Decrement when the other switch becomes released.

Then, use a switch statement to display some value on the dice, based on the counter.

Hello Paul.

Thanks for your reply on my topic....
This is going to sound greedy but I got my exam in a couple of hours and I still need my sleep... Could you write the code(Or an example)?

I'd be really grateful,

Sander.

I don't think the Arduino Forum and it's members are here to help "cheating" on your exams... 8)

have you looked in Google? There are many Arduino Dice sketches out there.