Pages: [1]   Go Down
Author Topic: Response redundance from serial !  (Read 468 times)
0 Members and 1 Guest are viewing this topic.
France
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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


* Capture du 2012-09-02 17:01:35.png (16.92 KB, 436x271 - viewed 10 times.)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
       mystere = int(random(1,101)) ; //tirage d'un entier entre 0 et 100 NON INCLUS
Your comment is wrong. The random() function, with those arguments does not return a value between 0 and 100.

What is sending data to the Arduino? If it is the Serial Monitor, what line ending setting (lower, right corner) are you using?

Edit: Never mind. I looked at your picture. It appears that the Serial Monitor is adding a carriage return to the string entered. Change that to nothing appended to the string to get rid of the duplicate response.
« Last Edit: September 02, 2012, 10:18:55 am by PaulS » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are sending lines from a Windows machine they may end in CRLF ("/r/n"). Serial.parseInt() may see that as two line endings.  The second line ending would be for an empty line so the numerical value would always be zero.

I don't see any easy way to fix the problem.  If you always throw away a character after parseInt() it will fail a different way with a non-Windows host.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

France
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Edit: Never mind. I looked at your picture. It appears that the Serial Monitor is adding a carriage return to the string entered. Change that to nothing appended to the string to get rid of the duplicate response.

That was it !! Shame on me, I could have find it by myself with a little care :-((

Thank you very much for pointing it !

But what do you mean by "my comment is wrong ?"

EDIT : never mind, I found it ( heedless again   smiley-roll-sweat ); the num range is 1 -100 INCLUDED, not 0-100 ....

Thanks again !
« Last Edit: September 02, 2012, 10:38:42 am by Oxirane » Logged

Pages: [1]   Go Up
Jump to: