Go Down

Topic: Arduino code stops after a while of usage (Read 240 times) previous topic - next topic

justStarting

Jul 09, 2013, 02:06 am Last Edit: Jul 09, 2013, 02:08 am by justStarting Reason: 1
So my Arduino code seems to stop after a while, and I don't see why it's doing that. Now... when I say a "while" of usage, I generally mean a couple hours
So here's what is going on, after running the following code:
Code: [Select]
//TMP36 Pin Variable
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
                       //the resolution is 10 mV / degree centigrade with a
                       //500 mV offset to allow for negative temperatures
int tweetBluePin = 2;


void setup()
{
 pinMode(tweetBluePin, OUTPUT);
 Serial.begin(9600);  //Start the serial connection with the computer
}

char inData[40]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0;// this tracks position in a char array that serial is writing to
int reading; //variable used for AdaFruit code
float voltage; //variable used for AdaFruit code
float temperatureC; //variable used for AdaFruit code
void loop()
{
 //following code from http://forum.arduino.cc/index.php/topic,45629.0.html
 inData[0] = '\0';
 index = 0; // Index into array; where to store the character
 while(Serial.available() > 0) // Don't read unless
                               // there you know there is data
 {
   if(index < 39) // One less than the size of the array
   {
     inChar = Serial.read(); // Read a character
     inData[index] = inChar; // Store it
     index++; // Increment where to write next
     inData[index] = '\0'; // Null terminate the string
   }
 }
    // Now do something with the string (but not using ==)
    //end code from http://forum.arduino.cc/index.php/topic,45629.0.html
   if (strcmp(inData, "temperatRoomAI") == 0)
   {
     //AdaFruit code for use with: http://www.adafruit.com/products/165
     //though much of this is probably going to be moved to computer side with python
     reading = analogRead(sensorPin);  
     
     // converting that reading to voltage, for 3.3v arduino use 3.3
     voltage = reading * 5.0;
     voltage /= 1024.0;
     // now convert to the temperature in Celsius
     temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree with 500 mV offset
                                             //to degrees ((volatge - 500mV) times 100)
     //end of AdaFruit code
     Serial.print("temperatureRoomAI: ");//display the temp in celsius to serial
     Serial.print(temperatureC);
     Serial.print("degreesC\n");
   }
   
   if (strcmp(inData, "newTweetRoomAI: 1") == 0)//from a twitter python program, when new tweets are received it turns on an led until it receives the code to turn it off
   {
     digitalWrite(tweetBluePin, HIGH);
   }
   if (strcmp(inData, "newTweetRoomAI: 0") == 0)//this is the code to turn off the led, for when all tweets are read
   {
     digitalWrite(tweetBluePin, LOW);
   }
   
 delay(1000);
}

So this code hooks up for Serial communication with a Raspberry Pi, turns on a blue LED through the Arduino if I receive a new tweet, turns it off once read, and through a temperature sensor pin can relay back that temperature information. However after a couple hours of running, suddenly the Arduino stops receiving/transmitting data.
I'm EXTREMELY new to this (and coding in general) so my question is: if anyone has any idea whats happening could you please point me in the right direction? I can include (almost) any other setup information if necessary. (note, this is Arduino 1.0.4 IDE)

Nick Gammon

I'm a bit troubled by this:

Code: [Select]

  while(Serial.available() > 0) // Don't read unless
                                // there you know there is data
  {
    if(index < 39) // One less than the size of the array
    {
      inChar = Serial.read(); // Read a character
      inData[index] = inChar; // Store it
      index++; // Increment where to write next
      inData[index] = '\0'; // Null terminate the string
    }
  }


You are expecting a string starting with "temperatRoomAI" to arrive, but if it gets out of step by a byte even, then you might get "emperatRoomAI" instead. I suggest using some sort of delimeter (eg. newline) and proceeding when you receive that. Read this for hints:

http://www.gammon.com.au/serial
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

holmes4

I'm just worried by the spelling!
Code: [Select]
if (strcmp(inData, "temperatRoomAI") == 0)

Mark

wildbill

This has the potential to hang your sketch:
Code: [Select]
 
while(Serial.available() > 0) // Don't read unless
                                // there you know there is data
  {
    if(index < 39) // One less than the size of the array
    {
      inChar = Serial.read(); // Read a character
      inData[index] = inChar; // Store it
      index++; // Increment where to write next
      inData[index] = '\0'; // Null terminate the string
    }
  }

If there are more than 38 characters in the read buffer, that will loop forever. The 1000ms delay will permit that to happen if you send enough data. An else break; clause would at least allow exit from the loop, but you'd clearly be out of sync with your expected data at that point - see Nick's delimiter remark.

PeterH


This has the potential to hang your sketch:


Very true - better to read the character from the interface and then do the length check to decide whether to store it or throw it away.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up