Go Down

Topic: Dice (Read 951 times) previous topic - next topic

fuqxkz

Hello,

for school I'm making an arduino dice. I programmed the animation when you push the button, and I did all of the rest. The only thing that is missing, is the random function. I have no idea how to put the random function in a dice. I want the dice to do the animation when pressing the button, then afterwards make a random light or random lights blink (the number of the dice you get). There are 7 LED's in order to make every number possible on a dice.

I hope someone can help me.

Regards,
fuqxkz

AWOL

http://arduino.cc/en/Reference/Random
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

fuqxkz

I've been on that page a lot, but I don't understand how to put it in the function with LED lights, etc. I just can't figure out what to do with it.

AWOL

I'd like to have been more specific, but you didn't post any code, so it is difficult to tell you how to use the function.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

fuqxkz

I'll post the code as soon as I have that lesson again.

Paul__B

The "standard" way to implement a "random" number is to read the clock value at the moment the button is pressed, divide it by six and take the remainder as your die reading.  ("Dice" means at least two of them. ;)  If you want two dice, you divide by 36, then divide by six to split it into two mod 6 values.)

You can do this using the milliseconds clock.  There is a slight loss of randomness when the counter - a power of two - rolls over, but this is so infrequent it will not matter in practice.


fuqxkz

@AWOL

This is the code:

Code: [Select]
/* Dobbelsteen - Arduino
   Informatica
   Alihan Deniz & Teng Derks
   
   
*/

// Bordje altijd met witte kant naar boven leggen

  int linksh = 10;   
  int linksm = 11;   
  int linksl = 12;   
  int midden = 9;     
  int rechtsh = 8; 
  int rechtsm = 4;   
  int rechtsl = 3; 
  int t = 50;
  int x = 195;
  int knop = 2;
 
  int buttonState = 0;
 
 
void setup ()
{
  pinMode(linksh,OUTPUT);
  pinMode(linksm,OUTPUT);
  pinMode(linksl,OUTPUT);
  pinMode(midden,OUTPUT);
  pinMode(rechtsh,OUTPUT);
  pinMode(rechtsm,OUTPUT);
  pinMode(rechtsl,OUTPUT);
 
  pinMode (knop, INPUT);
  Serial.begin(9600);
}

void loop ()
{
buttonState = digitalRead (knop);

if (buttonState == HIGH)

{
   
  dobbelsteen ();
delay(2000);

}


else

{
   
  normaal ();
   
}
   
}



void dobbelsteen ()
{
   
   //animatie vooraf
 
   digitalWrite (rechtsh,HIGH);
   digitalWrite (linksh,HIGH);
   delay(t);
   digitalWrite (linksh,LOW);
   digitalWrite (rechtsh,LOW);
   
   digitalWrite (midden,HIGH);
   digitalWrite (rechtsm,HIGH);
   digitalWrite (linksm, HIGH);
   delay(t);
   digitalWrite (linksm,LOW);
   digitalWrite (rechtsm,LOW);
   digitalWrite (midden,LOW);
   
   digitalWrite (linksl,HIGH);
   digitalWrite (rechtsl,HIGH);
   delay(t);
   digitalWrite (linksl,LOW);
   digitalWrite (rechtsl,LOW);
   
   delay(x);
   
   digitalWrite (rechtsl,HIGH);
   digitalWrite (linksl,HIGH);
   delay(t);
   digitalWrite (linksl,LOW);
   digitalWrite (rechtsl,LOW);
   
   digitalWrite (midden,HIGH);
   digitalWrite (rechtsm,HIGH);
   digitalWrite (linksm, HIGH);
   delay(t);
   digitalWrite (linksm,LOW);
   digitalWrite (rechtsm,LOW);
   digitalWrite (midden,LOW);
   
   digitalWrite (linksh,HIGH);
   digitalWrite (rechtsh,HIGH);
   delay(t);
   digitalWrite (linksh,LOW);
   digitalWrite (rechtsh,LOW);
   
   delay(x);
   
   digitalWrite (rechtsh,HIGH);
   digitalWrite (linksh,HIGH);
   delay(t);
   digitalWrite (linksh,LOW);
   digitalWrite (rechtsh,LOW);
   
   digitalWrite (midden,HIGH);
   digitalWrite (rechtsm,HIGH);
   digitalWrite (linksm, HIGH);
   delay(t);
   digitalWrite (linksm,LOW);
   digitalWrite (rechtsm,LOW);
   digitalWrite (midden,LOW);
   
   digitalWrite (linksl,HIGH);
   digitalWrite (rechtsl,HIGH);
   delay(t);
   digitalWrite (linksl,LOW);
   digitalWrite (rechtsl,LOW);
   
   delay(x);
   
   digitalWrite (rechtsl,HIGH);
   digitalWrite (linksl,HIGH);
   delay(t);
   digitalWrite (linksl,LOW);
   digitalWrite (rechtsl,LOW);
   
   digitalWrite (midden,HIGH);
   digitalWrite (rechtsm,HIGH);
   digitalWrite (linksm, HIGH);
   delay(t);
   digitalWrite (linksm,LOW);
   digitalWrite (rechtsm,LOW);
   digitalWrite (midden,LOW);
   
   digitalWrite (linksh,HIGH);
   digitalWrite (rechtsh,HIGH);
   delay(t);
   digitalWrite (linksh,LOW);
   digitalWrite (rechtsh,LOW);
   
   delay(x);
   
   digitalWrite (rechtsh, HIGH);
   digitalWrite (rechtsm, HIGH);
   digitalWrite (rechtsl, HIGH);
   delay(t);
   digitalWrite (rechtsh, LOW);
   digitalWrite (rechtsm, LOW);
   digitalWrite (rechtsl, LOW);
   
   digitalWrite (midden, HIGH);
   delay(t);
   digitalWrite (midden, LOW);
   
   digitalWrite (linksh, HIGH);
   digitalWrite (linksm, HIGH);
   digitalWrite (linksl, HIGH);
   delay(t);
   digitalWrite (linksh, LOW);
   digitalWrite (linksm, LOW);
   digitalWrite (linksl, LOW);
   
   delay (x);
   
   digitalWrite (linksh, HIGH);
   digitalWrite (linksm, HIGH);
   digitalWrite (linksl, HIGH);
   delay(t);
   digitalWrite (linksh, LOW);
   digitalWrite (linksm, LOW);
   digitalWrite (linksl, LOW);
 
   digitalWrite (midden, HIGH);
   delay(t);
   digitalWrite (midden, LOW);
   
   digitalWrite (rechtsh, HIGH);
   digitalWrite (rechtsm, HIGH);
   digitalWrite (rechtsl, HIGH);
   delay(t);
   digitalWrite (rechtsh, LOW);
   digitalWrite (rechtsm, LOW);
   digitalWrite (rechtsl, LOW);
   
   delay(x);
   
      digitalWrite (rechtsh, HIGH);
   digitalWrite (rechtsm, HIGH);
   digitalWrite (rechtsl, HIGH);
   delay(t);
   digitalWrite (rechtsh, LOW);
   digitalWrite (rechtsm, LOW);
   digitalWrite (rechtsl, LOW);
   
   digitalWrite (midden, HIGH);
   delay(t);
   digitalWrite (midden, LOW);
   
   digitalWrite (linksh, HIGH);
   digitalWrite (linksm, HIGH);
   digitalWrite (linksl, HIGH);
   delay(t);
   digitalWrite (linksh, LOW);
   digitalWrite (linksm, LOW);
   digitalWrite (linksl, LOW);
   
   delay (x);
   
   digitalWrite (linksh, HIGH);
   digitalWrite (linksm, HIGH);
   digitalWrite (linksl, HIGH);
   delay(t);
   digitalWrite (linksh, LOW);
   digitalWrite (linksm, LOW);
   digitalWrite (linksl, LOW);
 
   digitalWrite (midden, HIGH);
   delay(t);
   digitalWrite (midden, LOW);
   
   digitalWrite (rechtsh, HIGH);
   digitalWrite (rechtsm, HIGH);
   digitalWrite (rechtsl, HIGH);
   delay(t);
   digitalWrite (rechtsh, LOW);
   digitalWrite (rechtsm, LOW);
   digitalWrite (rechtsl, LOW);
   
   delay(x);
   
   // 1
   digitalWrite (linksh,LOW);
   digitalWrite (linksm, LOW);
   digitalWrite (linksl, LOW);
   digitalWrite (midden, HIGH);
   digitalWrite (rechtsh, LOW);
   digitalWrite (rechtsm, LOW);
   digitalWrite (rechtsl, LOW);
   
   delay(1000);
   
   //2
   
    digitalWrite (linksh,HIGH);
   digitalWrite (linksm, LOW);
   digitalWrite (linksl, LOW);
   digitalWrite (midden, LOW);
   digitalWrite (rechtsh, LOW);
   digitalWrite (rechtsm, LOW);
   digitalWrite (rechtsl, HIGH);
   
   delay(1000);
   
   //3
      digitalWrite (linksh,HIGH);
   digitalWrite (linksm, LOW);
   digitalWrite (linksl, LOW);
   digitalWrite (midden, HIGH);
   digitalWrite (rechtsh, LOW);
   digitalWrite (rechtsm, LOW);
   digitalWrite (rechtsl, HIGH);
   
   delay(1000);
   
   //4
   
      digitalWrite (linksh,HIGH);
   digitalWrite (linksm, LOW);
   digitalWrite (linksl, HIGH);
   digitalWrite (midden, LOW);
   digitalWrite (rechtsh, HIGH);
   digitalWrite (rechtsm, LOW);
   digitalWrite (rechtsl, HIGH);
   
   delay(1000);
   
   //5
   
      digitalWrite (linksh,HIGH);
   digitalWrite (linksm, LOW);
   digitalWrite (linksl, HIGH);
   digitalWrite (midden, HIGH);
   digitalWrite (rechtsh, HIGH);
   digitalWrite (rechtsm, LOW);
   digitalWrite (rechtsl, HIGH);
   
   delay(1000);
   
   //6
      digitalWrite (linksh,HIGH);
   digitalWrite (linksm, HIGH);
   digitalWrite (linksl, HIGH);
   digitalWrite (midden, LOW);
   digitalWrite (rechtsh, HIGH);
   digitalWrite (rechtsm, HIGH);
   digitalWrite (rechtsl, HIGH);
   
   delay(1000);
}
 
  void normaal()
  {
   digitalWrite (linksh,LOW);
   digitalWrite (linksm, LOW);
   digitalWrite (linksl, LOW);
   digitalWrite (midden, LOW);
   digitalWrite (rechtsh, LOW);
   digitalWrite (rechtsm, LOW);
   digitalWrite (rechtsl, LOW);
  }

AWOL

It looks to me like you're just cycling through the various die patterns.
I'd write a single function for each pattern (six functions), then use a switch/case to call just one based on the random number you've generated.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up