Home > Enterprise >  Problem with BME280 and char arrays on Arduino
Problem with BME280 and char arrays on Arduino

Time:02-22

I am trying to fill a char array with 1800 characters from digital pin 7 (data from a rain gauge) before reading the air pressure from a BME280 using Ardino UNO. The results are printed with Serial.println over USB.

#include <Adafruit_BME280.h>
#define DATA 7

Adafruit_BME280 bme;

void setup() 
{
    Serial.begin(9600);
    bme.begin(0x76);
    pinMode(DATA, INPUT);  
}

void loop() 
{ 
   int rmax = 1800;      //1460
   char r[rmax 1];       // changed from r[rmax]
   int i;
   for (i = 0; i < rmax; i  )
   {
      if (digitalRead(DATA) == 1)
         r[i] = '1';
      else
         r[i] = '0';
   }
   r[rmax] = '\0';
   Serial.println(r);
   Serial.println(bme.readPressure());
   delay(1000);
}

If the size of the array is greater than 1460, the data is not read from BME280, and array is printed without lineshift. Can anybody tell me why, and what can be done to get succeeded if the size of the array is 1800?

CodePudding user response:

Looks like you have a stack overflow problem.

As you can see in ATMega328 datasheet here or here, you have 2KB RAM only. ATMega328/ATMega328P is usually used as a base MCU for the Arduino UNO board.

To check that, you can make array char r[rmax]; as static & fix the buffer overflow problem, as Ian told.

Here you can find your code with fixes:


// ...
// you previous code here

// NOTE: next row was added
#define rmax 1800

void loop() 
{ 
   // NOTE: next row was changed
   static char r[rmax   1];
   int i;
   for (i = 0; i < rmax; i  )
   {
      if (digitalRead(DATA) == 1)
         r[i] = '1';
      else
         r[i] = '0';
   }
   r[rmax] = '\0';
   Serial.println(r);
   Serial.println(bme.readPressure());
   delay(1000);
}

If you have problems with code compiling, you have a stack overflow problem (RAM ends).

  • Related