Pressure sensor - arduino low memory (SOLVED)
-
I had some problems using F() in some variables like Serial.println(F(somevariable))
A good start is to change all Serial.println("Mytext") with Serial.println(F("Mytext"))Andreas
-
I had some problems using F() in some variables like Serial.println(F(somevariable))
A good start is to change all Serial.println("Mytext") with Serial.println(F("Mytext"))Andreas
@sundberg84 said:
I had some problems using F() in some variables like Serial.println(F(somevariable))
A good start is to change all Serial.println("Mytext") with Serial.println(F("Mytext"))Andreas
you had those problems because you cannot store a variable in flash (program space) only constants.
the compiler will automatically hold numeric constants in flash, but string constants have to be so identified for that to happen. otherwise the consume space in SRAM
variables initialized as const are also saved in program space:
const char *dayOfWeek[] = { "Null","Sunday ","Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}; -
I tried IDE 1.0.5 and codebender with same results - sensor stops responding after 3-4 hours. Is anyone running this sensor successfully?
Upon searching for arduino board with more SRAM I found arduino micro. It has 2.5 KB SRAM (0.5 KB more than Nano). Would it work with Mysensors? -
Why not start with a more simple sketch?
#include <SPI.h>
#include <MySensor.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>#define BARO_CHILD 0 #define TEMP_CHILD 1 unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in seconds) Adafruit_BMP085 bmp = Adafruit_BMP085(); // Digital Pressure Sensor MySensor gw; float lastPressure = -1; float lastTemp = -1; boolean metric; MyMessage tempMsg(TEMP_CHILD, V_TEMP); MyMessage pressureMsg(BARO_CHILD, V_PRESSURE); void setup() { gw.begin(); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("Pressure Sensor", "1.0"); if (!bmp.begin()) { Serial.println("Could not find a valid BMP085 sensor, check wiring!"); while (1) { } } // Register sensors to gw (they will be created as child devices) gw.present(BARO_CHILD, S_BARO); gw.present(TEMP_CHILD, S_TEMP); metric = gw.getConfig().isMetric; } void loop() { float pressure = bmp.readPressure()/100; float temperature = bmp.readTemperature(); if (!metric) { // Convert to fahrenheit temperature = temperature * 9.0 / 5.0 + 32.0; } // int forecast = sample(pressure); Serial.print("Temperature = "); Serial.print(temperature); Serial.println(metric?" *C":" *F"); Serial.print("Pressure = "); Serial.print(pressure); Serial.println(" Pa"); if (temperature != lastTemp) { gw.send(tempMsg.set(temperature,1)); lastTemp = temperature; } if (pressure != lastPressure) { gw.send(pressureMsg.set(pressure, 0)); lastPressure = pressure; } gw.sleep(SLEEP_TIME); } -
Here I am again.
The problem is in the sample() function. It saves the pressure every minute in the array pressureSamples[]. This one is declared for 180 values which means you can use pressureSamples[0] to pressureSamples[179]. Writing to pressureSamples[180] means you are writing outside the defined array and might be writing over some other stuff in your program.
In my case it resulted in radio messages which got status 'fail' all the time. First I thought it was a problem with the radio but after debugging I found the problem to be the sample() function. It always happened exactly 180 minutes after I restarted the Arduino...You have to change the following lines of code:
if (minuteCount > 180) minuteCount = 6;into:
if (minuteCount == 180) minuteCount = 5;The problem with the original code was that it wrote a value to pressureSamples[180] before it restarted with 6 again (which should be 5 according to me).
When looking at the code I also found that the wrong samples seem to be used for the first 5 minutes. If minuteCount has reached the value of 5 it means that samples 0 to 4 has been filled. The code is using 1 to 5 in stead.
To fix this, change the code under if (minuteCount == 5) into:
pressureAvg[0] = ((pressureSamples[0] + pressureSamples[1] + pressureSamples[2] + pressureSamples[3] + pressureSamples[4]) / 5);To be clear: this bug is not something @hek introduced, he just used the code supplied by the link in the sample()-function.
Edit: But maybe he can make sure the sketch is updated so no one else will get the same problems? :)
-
@hek: great, but not completely correct...
It should be: if (minuteCount == 180)
Could you also consider to update the sketch with my proposal in my other comment for the pressuresensor-sketch regarding changing
float pressure = bmp.readPressure()/100;to
float pressure = bmp.readSealevelPressure(205)/100; // 205 meters above sealevelI assume most people would like to have the same pressure values as the local wheather station or airport has.
-
Still this code is working?