Go Down

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

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