Go Down

Topic: Arduino goes crazy on serial data incomming (Read 1 time) previous topic - next topic

JanD

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: [Select]
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: [Select]
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

zoomkat

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.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

JanD

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

JanD

MikMo

#3
Feb 03, 2011, 11:09 am Last Edit: Feb 03, 2011, 11:14 am by MikMo Reason: 1
In this bit of code:

Code: [Select]
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: [Select]
if(Serial.available()>=2){
   char data = Serial.read();
   char data2 = Serial.read();


Also this code:

 
Code: [Select]
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.

Go Up