Serial send data show on LCD some time error?

Dear all
I’m made project counter control with Arduino board.
In my project have Arduino, LCD, Relay, Proximity.
Concept
PC (RS232) — send cycle —> Arduino board ( Show on LCD )
Proximity detect cycle on Machine if cycle = 1 then Relay is High.
Machine will stop = and Arduino LCD show “OK”

In my code, I can send cycle to many time no problem.

My problem come from after cycle = 1 and show “OK” ? After PC(rs232) send data again sometime OK,
Some time show error ( see in attach file picture )

Please advice to me

Thank all
=(

IMG_9093.jpg

IMG_20141002_163408434.jpg

sketch_jul04a.ino (7.77 KB)

Start here

readLen = Serial.readBytes(buffer, incomingLen);
        
            if (readLen!=0)

These lines seem a bit pointless since you already know incomingLen and you know that it is not 0.

I don't understand your problem description. Can you relate it to the line numbers in your code where you think the problem lies.

...R

Hi

I'm checking my error will happen after cycle = 1 and send data again from RS232.

some time OK , some time Error.

I'm try to clear memory before receive data from RS232, but this error not fix.

else if (cycle == 1)  
            {
              //show end 
              lcd.clear();
              lcd.setCursor(0,0); // 2-Oct-2014
              lcd.print("CYCLE"); // test print encoder0Pos
              lcd.setCursor(6,1);
              //lcd.print("                ");
              lcd.print("OK");
        
              digitalWrite(RELAY1,HIGH);           // Turns Off Relays 1
              delay(1000);                        // Wait 1 seconds
              //digitalWrite(RELAY1,LOW);          // Turns Relay Off
              
              //Serial.flush(); //test clear buffer after receive data 18-09-2014
              Serial.end();     //'26-09-2014 end cycle clear buffer and data testing
              delay(1000); 
              Serial.begin(9600);
              delay(1000); 
              QQ = 9; 
              delay(1000);
              //lcd.blink();
              //lcd.noBlink(); 
            }

Thank you

You Might carefully carefully examine the "Blink Without Delay" example provided in the IDE examples to avoid the issues you are building...

Doc

I suspect that your relay operation is causing intermittent power problem.

It is not clear what command operates the relays ( HIGH on/ LOW off ?) - the comments are contradicting. Are you driving the relay directly from pin 11 and if so do you have a reverse EMF diode in the circuit? Can you repeat the lcd outputs AFTER you operate the relay - like this:

    lcd.clear();
              lcd.setCursor(0,0); // 2-Oct-2014
              lcd.print("CYCLE"); // test print encoder0Pos
              lcd.setCursor(6,1);
              //lcd.print("                ");
              lcd.print("OK");
        
              digitalWrite(RELAY1,HIGH);           // Turns Off Relays 1
              delay(1000);                        // Wait 1 seconds
              //digitalWrite(RELAY1,LOW);          // Turns Relay Off


 repeat the lcd output again      
 lcd.clear();
              lcd.setCursor(0,0); // 2-Oct-2014
              lcd.print("CYCLE"); // test print encoder0Pos
              lcd.setCursor(6,1);
              //lcd.print("                ");
              lcd.print("OK");
        
repeat the lcd output again 
     
              //Serial.flush(); //test clear buffer after receive data 18-09-2014
              Serial.end();     //'26-09-2014 end cycle clear buffer and data testing
              delay(1000); 
              Serial.begin(9600);
              delay(1000); 
              QQ = 9; 
              delay(1000);
              //lcd.blink();
              //lcd.noBlink();

BTW - it is not a good practice to detect input ( button) from idle ground to active high. If the input get accidentally grounded you will not detect it as failure. But accidentally grounding circuit which is idle high gets detected if no button is pressed.

Moderator edit: code tags

              Serial.end();     //'26-09-2014 end cycle clear buffer and data testing
              delay(1000); 
              Serial.begin(9600);

Why are you doing this? What do you think you are doing by calling end() and begin() again?

If the intention is to empty the outgoing buffer, Serial.flush() will do that. If the intention is to empty the incoming buffer, STOP THAT! Only an idiot would dump random amounts of unread data. If you just insist, though:

while(Serial.available() > 0)
   Serial.read();