Sign up ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

i am trying to send data via Labview to my arduino board using serial communication.My problem is that the arduino reads only the first byte of sent data then when i change it ,it doesn't react.
For example when i start the communication i send '0' to arduino.I sees it.but while it is running i send '1' but it doesn't behave accordingly to it.
I noticed also that the board 'sees' sometime the new value after a random period of time.

    void setup() { 
      Serial.begin(9600); 
      pinMode(13,OUTPUT); 
     } 

  char data='F';
  void loop() 
    { 



 data=Serial.read();



 delay(50);
 if (data=='0')
  {
    digitalWrite(13,HIGH);
    delay(1000);
   digitalWrite(13,LOW);
 delay(1000);
 } 

if (data=='1')
 {
  digitalWrite(13,HIGH);
  delay(4000);
  digitalWrite(13,LOW);
  delay(4000);
 } 
 data='4';

           } 
share|improve this question

2 Answers 2

I suspect you problem stems from the use of delay's in your code. for example,

if (data=='1')
 {
  digitalWrite(13,HIGH);
  delay(4000);
  digitalWrite(13,LOW);
  delay(4000);
 } 

Those delays prevent any computations from happening for 4 seconds each. Your arduino will not read another character after it sees a '1' for 8 seconds. If you send too much data in that 8 seconds to fit in the serial buffer, the extra characters are lost.

The behavior you seem to want will require separating the code that reads serial from the code that runs the output, and making sure neither section ever stops execution with delays. Your blinks will need to be timed with something else, like frequent comparisons against the current time with millis()

share|improve this answer

JUST add the part ~ This flushes the serial buffer, which aids in error free data receiving. No need to implement the millis() protocol as you want the led to be active for that time so you do not want any data to enter the system, neither you are using interrupts service.

Serial.readString();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.