newbie Crash

Howdy all,

I'm a newbie to Arduino, and have run into a problem that I'm hoping you wonderful folks out there might be able to help me solve.

I'm trying to read values from 2 sensors, then send a string out that I can read in Serial Monitor.

This code snippet works just fine :

String r; r=String(millis()); r+= ":A5:"; Serial.print(r); serialPrintFloat(lightSensorValue); Serial.println(" ");

so does this one :

String s; s=String(millis()); s+= ":A0:"; Serial.print(s); serialPrintFloat(degC); Serial.println(" ");

Both of these snippets spew out data continuously quite happily to the serial monitor.

However, when I combine them as shown in the function below, I only see the first 2 readings (1 from each sensor) in the serial monitor, then nothing! I'm guessing Arduino has crashed, but have no idea why.

Any thoughts are greatly appreaciated.

Thanks in advance,

Melbfella.

The complete code :

int temperatureSensorPin = 0; //temperature sensor is connected to analog pin 0 int temperatureValue; // variable to store the value coming from the temp sensor

int lightSensorPin = 5; // light sensor is connected to analog pin 5 int lightSensorValue; // variable to store the value coming from the light sensor

void setup(){ Serial.begin(9600); }

void loop() {

temperatureValue = analogRead(temperatureSensorPin); lightSensorValue = analogRead(lightSensorPin); // read the value from the sensor

float degC=temperatureValue/2.01;

String s; s=String(millis()); s+= ":A0:"; Serial.print(s); serialPrintFloat(degC); Serial.println(" ");

String r; r=String(millis()); r+= ":A5:"; Serial.print(r); serialPrintFloat(lightSensorValue); Serial.println(" ");

delay(1000);

}

void serialPrintFloat( float f){ Serial.print((int)f); Serial.print("."); int temp = (f - (int)f) * 100; //Serial.println( abs(temp) ); Serial.print( abs(temp) ); }

Two things:

  1. Strings. Just.... no. Strings are evil.
  2. Serial.print will do most of the work for you.

Try this massive simplification:

  temperatureValue = analogRead(temperatureSensorPin);  
  lightSensorValue = analogRead(lightSensorPin); // read the value from the sensor
  
  float degC=temperatureValue/2.01;
  
  Serial.print(millis());
  Serial.print(":A0:");
  Serial.println(degC);
  
  Serial.print(millis());
  Serial.print(":A5:");
  Serial.println(lightSensorValue);
  
  delay(1000);

And please, use code tags around your code posting.

Thanks for your fast response and solution Majenko.

I will make sure I use the code tags as you suggest.

I've used strings (probably incorrectly from a pure point of view) in my VB6/C# .Net background for years, so attempted the same approach with (whatever the native Arduino language is - Java?) which failed miserably.

To solve the issue, your solution is perfect and elegant - thank you.

Arduino is C++.

The String problem is something to do with memory allocation, but you'd have to read the many other posts around about it for more specifics.

Strings are perfectly fine when you have lots of memory to play with. Working with strings involves allocating memory, copying from one place to another, and freeing the old memory. That leaves the memory heap full of holes. Only variables of a smaller size than the hole will fit in it - you can't fragment variables. And when you only have a few K of memory to play with you soon start to run into problems.

Over time those holes get smaller and smaller and there are more and more of them to the extent that you cannot fit any decent size variables in memory any more - even though you may have in total enough free memory to do it.

All perfectly fine and reasonable when you have 4 gigabytes of RAM to play with, but not very nice on a microcontroller with a tiny fraction of that.

So the String class, and indeed any dynamic memory allocation, should be avoided like the plague on a microcontroller. All your string variables (char arrays) should be statically allocated to a predefined maximum size, and all your work should be done in those. I often have a single "temp" char array which I use over and over again for string manipulations.

Thanks Majenko, for your detailed information regarding strings.

Looks like it's time for me to start thinking in different paradigms, and take my .Net hat off completely when playing with the Arduino :)

Melbfella: Thanks Majenko, for your detailed information regarding strings.

Looks like it's time for me to start thinking in different paradigms, and take my .Net hat off completely when playing with the Arduino :)

Good plan. Personally I think you should take the .Net hat off and burn it :P