Hi all, my first time posting here. Chances are I'm making a simple mistake, but been staring at the screen for a while and not figuring it out.
I want to read altitude from a BME280 sensor (eventually I'd like to read temp and log the relative time of the readings as well) and write it to an SD card. The research I've done leads me to think that taking my readings with an ISR and buffering them is the way to go. Even when I strip all the buffering and SD related code out I can't even seem to get a simple ISR to return a reading. The sensor is confirmed working by using the same code in my 'void loop ()' so I know it's something else.
I'm using an Arduino Pro Mini 3.3V 8MHz
#include "SparkFunBME280.h"
#include <Wire.h>
#include <SD.h>
#include <TimerOne.h>
#define buffSize 16
#define buffCount 2
//buffer related variables
volatile float writeBuffer [buffSize][buffCount]; //array to buffer sensor readings for write to SD
volatile float alt; //temp variable to check if interrupt function working properly
volatile byte bufferID = 0; //array of elements 1 or 2
//volatile unsigned long sensorTime; //future use to write time of sensor read to SD
volatile byte bufferCount = 0; //which array element is being indexed
volatile bool bufferFull = false; //trigger the main loop to write buffer to SD
//sensor related variables
BME280 sensor;
//float alt, temp;
// SD write related variables
File myFile;
int once=0;
String FileName = "";
String FileExt = ".csv";
String FilePre = "test";
int inc = 0;
//time related variables
long unsigned timer = 0;
int Delay = 50;
void setup()
{
sensor.settings.commInterface = I2C_MODE; //sensor setup calls
sensor.settings.I2CAddress = 0x76;
sensor.settings.runMode = 3;
sensor.settings.tStandby = 4;
sensor.settings.filter = 0;
sensor.settings.tempOverSample = 1;
sensor.settings.pressOverSample = 1;
delay(1000);
sensor.begin(); //make sure sensor has adequate time to come online
delay(1000);
Serial.begin(57600);
Serial.println("Begin");
Serial.print("Starting BME280....");
//if(!SD.begin(4)) //check for SD communication
//{
// Serial.println("Initialization failed.");
// return;
//}
Serial.println("Initialization done.");
//FileName = FilePre + inc + FileExt; //routine to determine filename, numbered sequentially from one run to the next
//while (SD.exists(FileName)) //disabled all SD functions to try and troubleshoot ISR
//{
// inc++;
// FileName + "";
// FileName = FilePre + inc + FileExt;
//}
//myFile = SD.open(FileName, FILE_WRITE);
//if (myFile)
//{
// Serial.println("File Open");
// myFile.println("time, alt, temp");
// myFile.close();
//} else
//{
// Serial.println("Error opening file.");
//}
Serial.println("time, alt, temp");
Timer1.initialize(500000);
Timer1.attachInterrupt(readSensor);
}
void loop()
{
if (bufferFull = true) //check if one of the buffers is full
{
for (int i = 0; i < buffSize; i++)
{
//Serial.println (writeBuffer[i][bufferID]); //write buffer values to screen, eventually will change to SD write
}
bufferFull = false; //reset buffer trigger
}
}
void readSensor() //ISR to read sensor values
{
Serial.print("Reading sensor value.... :"); //on screen notification
alt=sensor.readFloatAltitudeFeet(); //read sesnor value to temp variable
Serial.print(alt); //write to screen
/*if (bufferCount = buffSize) //check if buffer is full and increment to next element
{
if (bufferID = 0)
{
bufferID = 1;
bufferCount = 0;
bufferFull = true; //set buffer full trigger
}
else
{
bufferID = 0;
bufferCount = 1;
bufferFull = true;
}
}
else
{
bufferCount++; //increment buffer element counter
writeBuffer[bufferCount][bufferID]=sensor.readFloatAltitudeFeet(); //write sensor reading to buffer element
Serial.println(alt);
}
*/
}
The serial monitor window gets cut off part way through writing the initialisation messages, I'm assuming my ISR is being called?
Any help would be greatly appreciated.
Thanks,
Scott