Hi everybody,
I wrote a simple sketch, an ask-for-the-num game ( you have to find out the num randomly choosen by Arduino ); the player enters his proposals through serial.
int mystere;//GLOBALE CAR INITIALISÉE DANS SETUP ET UTILISÉE DANS LOOP
void setup() {
Serial.begin(9600) ;
randomSeed(analogRead(5)); //initialisation générateur "aléatoire"
mystere = int(random(1,101)) ; //tirage d'un entier entre 0 et 100 NON INCLUS
}
void loop() {
int proposition ;
while ( Serial.available() == 0) { //boucle qui attend que la liaison série ne soit pas vide
}
proposition = Serial.parseInt() ; //récupération du nombre proposé par le joueur
//évaluation de la réponse
if ( proposition > mystere ) {
Serial.print(proposition);
Serial.println( " : Trop grand !") ;
}
else if ( proposition < mystere ) {
Serial.print(proposition);
Serial.println(" : Trop petit !") ;
}
else if ( proposition == mystere ) { // OU : else { } suffit !!
Serial.println("Gagné !") ;
}
}
The sketch works, except it returns two sentences ( it's designed to write back only one... ) :
- the reply to the player according to his proposal ( "Too high", "too low", "won !! " )
- another reply, always the same, as if the player enters the "0" number...
( see attached picture )
I tried to temporize the sketch, suspecting unsuitable timing...but no success !
Any advice ?
Thanks !!
Chris