Pages: [1]   Go Down
Author Topic: Arduino code stops after a while of usage  (Read 183 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//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)
« Last Edit: July 08, 2013, 07:08:38 pm by justStarting » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm a bit troubled by this:

Code:
  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
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1883
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mark
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 51
Posts: 3438
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This has the potential to hang your sketch:
Code:
 
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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: