Hey guys, I'm making a data logger with a Nano Every. I have a BME280, a thermometer, a mic, and a Deek-Robot data logging shield (8105). I have everything working separately, but I can't seem to get it working when I try to put it all together. And to clarify, I do not mean physically, just in code.
Each section of code works separately, like the RTC will output correctly, or I can read and write to the sd card, or get the data from the BME280 in the serial output. I just can't get them to work nicely with each other.
I'd like to point out that I have absolutely no idea what I'm doing, and I did not write any of this code myself. However, I need to finish this project as I need to deploy it in real world scenarios quite soon.
I would also like some advice on putting the Nano to sleep for about 30 minutes or so in between each log, as this thing will be out in the wild for months at a time and needs to be ultra power saving when not in use, supplemented a bit by a solar panel.
Here is the code that I have Frankenstein'd together:
int ThermistorPin = A2;
int Vo;
float R1 = 10000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
int const AMP_PIN = A6; // Preamp output pin connected to A0
unsigned int sample;
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <RTClib.h>
#include <SD.h>
#define BME_SCK 6
#define BME_MISO 3
#define BME_MOSI 5
#define BME_CS 4
#define DS1307_I2C_ADDRESS 0x68
RTC_DS1307 rtc;
File myFile;
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
void setup() {
Serial.begin(9600);
while(!Serial); // time to get serial running
unsigned status;
status = bme.begin();
}
void loop()
{
printTime();
printValues();
printSound();
delay(1000);
}
void printValues() {
myFile = SD.open("data.txt", FILE_WRITE);
myFile.print("Temperature = ");
myFile.print(bme.readTemperature());
myFile.println(" °C");
myFile.print("Pressure = ");
myFile.print(bme.readPressure() / 100.0F);
myFile.println(" hPa");
myFile.print("Approx. Altitude = ");
myFile.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
myFile.println(" m");
myFile.print("Humidity = ");
myFile.print(bme.readHumidity());
myFile.println(" %");
myFile.print("Temperature: ");
myFile.print(T);
myFile.println(" F");
myFile.close();
}
void printTime() {
DateTime time = rtc.now();
//Full Timestamp
myFile.println(String("DateTime::TIMESTAMP_FULL:\t")+time.timestamp(DateTime::TIMESTAMP_FULL));
}
void printSound() {
myFile = SD.open("data.txt", FILE_WRITE);
unsigned long startMillis = millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS and then plot data
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(AMP_PIN);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
myFile.println();
myFile.println(peakToPeak);
//double volts = (peakToPeak * 5.0) / 1024; // convert to volts
//Serial.println(volts);
Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
T = T - 273.15;
T = (T * 9.0)/ 5.0 + 32.0;
myFile.close();
}
I got working code from the RTClib > timestamp example, the SD > ReadWrite example, and various sources across the internet and this forum for the other pieces.
Just to be ultra clear, this is not a hardware or wiring issue at all. It is only when I try to put the code together that anything fails.
Here is what it looks like if anyone is curious or needs to see for anything
Thank you guys so much in advance for your help!
