Pages: [1]   Go Down
Author Topic: Arduino goes crazy on serial data incomming  (Read 1289 times)
0 Members and 1 Guest are viewing this topic.
Sweden
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello again,

so, now I have a new problem and because this doesn't have the slightest to do with interrupts I decided to make a new topic.

So, problem is: Then I send "?" to the Arduino via the serial monitor it resets or something like that, at least the display is cleared and only the first row has slight boxes.

Why? And why doesn't the board go up and running again?

Interesting parts of the code for this:
Code:
void updateSerial(){
  if(Serial.available()){
    char data = Serial.read();
    char data2 = Serial.read();
    if(data2 != 0x0D || data2 != '-') return;
    if(data == '?'){
        sendAllToComputer();
    return;
    }
    if(data == '?' && lastBib != 0 && lastTime != 0){
        sendResult(lastBib, lastTime);
        return;
    }
    else if(data == '+'){
        lastBib = 0, lastTime = 0;
        return;
    }
    else if(data == '!'){
        Serial.read();
        Serial.read();
        Serial.read();
        Serial.read();
        Serial.read();
        return;
    }
    return;
  }
}
Code:
void loop(){
  //sendAllToComputer();
  updateSerial();
  showTime();
 
  if(numberNow != 0){
    lcd.print(numberNow);
  }
 
  delay(250);
}

To be said is, the updateSerial also wants a carriage return (0x0D), and in the serial monitor I have chosen "Carriage return" from the dropdown menu left of the baudrate menu.

JanD
Logged

0
Offline Offline
Tesla Member
***
Karma: 148
Posts: 9753
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So, problem is: Then I send "?" to the Arduino via the serial monitor it resets or something like that, at least the display is cleared and only the first row has slight boxes.

Sounds like you probably have something electrically connected to your arduino that is killing it via overcurrent or a short.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Sweden
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've already thought of that, but why can it upload then?

JanD
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In this bit of code:

Code:
if(Serial.available()){
    char data = Serial.read();
    char data2 = Serial.read();

You are asuming that if data is available at least two bytes are available, this is not a valid assumption.
Arduino is very fast, so serial.available() might return true when just one byte is available, and you might try to read the second byte before it is available.

Better to use something like:

Code:
if(Serial.available()>=2){
    char data = Serial.read();
    char data2 = Serial.read();

Also this code:

  
Code:
if(data == '?' && lastBib != 0 && lastTime != 0){
        sendResult(lastBib, lastTime);
        return;
    }

Will never be executed because in the if block just above it you do a return if data=='?'

If your problem only happen when you send the ?, then i would look for the problem in the sendAllToComputer(); function, which you have not posted.
    
Finally it would be much better to use a switch statement than all the nested if / else If statements. It would make your code much easier to read and undertand, and much less errorprone.
« Last Edit: February 03, 2011, 05:14:57 am by MikMo » Logged

Pages: [1]   Go Up
Jump to: