Hi,
I am working with an Arduino Uno clone to read temperature of two RTC sensors. For this, I have created my own library NTC_3950_10k.h, which is just a simple interface to convert and parse the temperature read by the sensor.
My problem is that "mysteriously" two more lines on my loop function containing calls to strcat completely halts the processor and makes the
Here my code:
TemperatureReaderStandalone.ino
#include "NTC_3950_10k.h"
unsigned int inByte = 0;
NTC_3950_10k *tempSensor, *tempSensor2;
char telemetry[10];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
tempSensor = new NTC_3950_10k(A0);
tempSensor2 = new NTC_3950_10k(A1);
memset(telemetry, 0, sizeof(telemetry));
//pinMode(A0, INPUT);
while(!Serial);
Serial.print("pin on tempsensor1 "); // <---- EXECUTION STOPS HERE
Serial.println(tempSensor->pin);
}
void loop() {
memset(telemetry, 0, sizeof(telemetry));
strcat(telemetry,tempSensor->readTemperatureStr());
strcat(telemetry,"\t");
strcat(telemetry,tempSensor2->readTemperatureStr());
Serial.println(telemetry);
delay(100);
}
NTC_3950_10k.cpp
#include "NTC_3950_10k.h"
#include <math.h>
#include <Arduino.h>
float NTC_3950_10k::raw2celsius(unsigned int x){
return 5.5437465781E-16*pow(x,6) - 2.2822487822E-13*pow(x,5) - 1.5520552248E-9*pow(x,4) + 2.2362994944E-06*pow(x,3) - 1.2423493602E-03*pow(x,2) + 4.0356315840E-01*x - 5.1354957493E+01;
}
NTC_3950_10k::NTC_3950_10k(int pin){
this->pin = pin;
pinMode(pin, INPUT);
}
float NTC_3950_10k::readTemperature(){
return raw2celsius(analogRead(pin));
}
char* NTC_3950_10k::readTemperatureStr(){
float temp = raw2celsius(analogRead(pin));
dtostrf(temp, 6, 2, lastReadTemperature);
return lastReadTemperature;
}
NTC_3950_10k.h
class NTC_3950_10k {
public:
NTC_3950_10k(int);
float readTemperature();
char* readTemperatureStr();
char lastReadTemperature[6];
int pin;
private:
float raw2celsius(unsigned int x);
};
Serial Monitor output:
03:04:36.572 -> pin on tempsensor1 14
03:04:36.758 -> pin⸮⸮⸮ pi⸮⸮⸮⸮⸮pin
The funny thing is that if I comment the line 25, strcat(telemetry,tempSensor2->readTemperatureStr());, the Arduino runs perfectly: it prints the message on the setup() and continues gracefully to the loop() by printing the temperature. I have my suspicion on the way memory is handled but I do not know how to explain this behaviour.