Étape 1: Dé électronique
Ce programme utilise le moniteur de la série pour afficher le lancer de deux dés. Copiez/collez le programme dans l’IDE Arduino et ouvrir le moniteur de la série. Entrez n’importe quel caractère pour lancer les dés. Le personnage que vous entrez ne sert pas pour n’importe quoi. La première fois dans la boucle il graines le générateur de nombres aléatoires à l’aide de millis().
/******************************************************* * Filename: ArdDice.ino * * An electronic dice roller. Open the serial monitor, * enter any character and press Enter to roll the dice. * *******************************************************/ int firsttime = 1; // Set first time flag void setup() { Serial.begin(9600); } void loop() { Serial.println("Enter any character and press Enter"); char choice = ' '; // Unused character. while(Serial.available() == 0); choice = Serial.read(); // Get character. if(firsttime==1) // If first time through loop, { randomSeed(millis()); // seed random number generator, firsttime=0; // and clear first time flag. } Serial.println(" "); Serial.println(" "); dice(); // Roll and print first die. dice(); // Roll and print second die. } /*********************************************** * dice() - Function to roll and print one die. ***********************************************/ void dice() { int rnd=random(1,7); switch(rnd) { case 1: Serial.println(" "); Serial.println(" * "); Serial.println(" "); Serial.println(" "); Serial.println(" "); break; case 2: Serial.println(" * "); Serial.println(" "); Serial.println(" *"); Serial.println(" "); Serial.println(" "); break; case 3: Serial.println(" * "); Serial.println(" * "); Serial.println(" *"); Serial.println(" "); Serial.println(" "); break; case 4: Serial.println(" * *"); Serial.println(" "); Serial.println(" * *"); Serial.println(" "); Serial.println(" "); break; case 5: Serial.println(" * *"); Serial.println(" *"); Serial.println(" * *"); Serial.println(" "); Serial.println(" "); break; case 6: Serial.println(" * *"); Serial.println(" * *"); Serial.println(" * *"); Serial.println(" "); Serial.println(" "); break; } }