mercredi 22 novembre 2017

Arduino C++ 'random' function predictable

I tried making a dice on arduino for an assignment with random() to find a number between 1 and 6, but on the first shot after a restart it always lands on a 2. Another group also has this problem, maybe something is wrong with the random() function?

Code:

// Main code (looping function)
void loop() {
  // Reads the button state and sets as variable 'button'
  button = digitalRead(buttport);
  // Triggers when button is pressed
  if (button == LOW) {
    // Sets variable 'choice' as a random integer between 1 and 6 (7 because it is not inclusive of the end number)
    choice = random(1,7);

    // Displays '1' on the dice if choice = '1'
    if(choice == 1){
      digitalWrite(mid, HIGH);
      // Makes a randomly generated noise on the buzzer (same for all choices)
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      delay(4000);
    }

    // Displays '2' on the dice if choice = '2'
    if(choice == 2){
      digitalWrite(tlbr, HIGH);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      tone(buzzer, rand() % 1000 + 100);
      delay(100);
      noTone(buzzer);
      delay(50);
      delay(4000);
    }

    // And so on...
  }
}

Cheers

(P.S Please reply as soon as is humanly possible to ensure that I can complete this task on time thank you for your help :))




Aucun commentaire:

Enregistrer un commentaire