Hi, I have a problem with my Arduino code for my Arduino Nano clone. I am making a device that measures different things and prints these in the console and saves them on a micro SD Card.
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <Adafruit_BMP280.h>
#include <Wire.h>
//WIRING
//ARDUINO - MODULE
//----------------
// GPS
//4 TX
//3 RX
//----------------
// SD
//10 CS
//11 MOSI
//12 MISO
//13 SCK
//----------------
// MQ-4
//A0 AO
//----------------
// APC220
//----------------
// BMP280
//3.3v VCC !BELANGRIJK!
//A4 SDA
//A5 SCL
//----------------
// Buzzer
//7 VCC
//GND GND
//GPS
static const uint32_t GPSBaud = 9600;
static const int RXPin = 3, TXPin = 4;
TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);
// SD Card
const int chipSelect = 10; // CS pin
File dataFile;
//MQ-4
#define gasPin A0
//BMP280
Adafruit_BMP280 bmp;
float pressureAtZero = 0;
//adxl345
int ADXL345 = 0x53;
float X_out, Y_out, Z_out;
void setup() {
Serial.begin(9600);
ss.begin(GPSBaud);
pinMode(gasPin, INPUT);
// Initialize SD Card
if (!SD.begin(chipSelect)) {
Serial.println(F("SD Card initialization failed!"));
//while (1);
}
Wire.begin(); // Initiate the Wire library
// Set ADXL345 in measuring mode
Wire.beginTransmission(ADXL345); // Start communicating with the device
Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
// Enable measurement
Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable
Wire.endTransmission();
if (!bmp.begin(0x76)) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
//while (1);
}
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
pressureAtZero = bmp.readPressure() / 100;
dataFile = SD.open("data.csv", FILE_WRITE);
Serial.println("Opened");
if (dataFile) {
dataFile.println("Time;Date;Latitude;Longitude;Altitude_GPS;Speed;Satellites;Temperature;Pressure_HPa;Altitude_Baro;GasReading");
dataFile.close();
Serial.println("write succesfull");
} else {
Serial.println("failed");
}
}
void loop() {
while (ss.available() > 0) {
if (gps.encode(ss.read())) {
//Wire.beginTransmission(ADXL345);
// Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
//Wire.endTransmission(false);
//Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
//X_out = ( Wire.read()| Wire.read() << 8); // X-axis value
//X_out = X_out/256; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
//Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
//Y_out = Y_out/256;
//Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
//Z_out = (Z_out-56.32)/256;
//char xyz[20];
//xyz[0] = '\0';
//snprintf(xyz,20,"%s;%s;%s;",String(X_out).c_str(),String(Y_out).c_str(),String(Z_out).c_str());
char output[121];
output[0] = '\0';
snprintf(output, 121, "%s;%s;%s,%s;%s;%s;%s;%s;%s;%s;%s;", (gps.time.isValid() ? String(gps.time.value()).c_str() : "N/A"), //time
(gps.date.isValid() ? String(gps.date.value()).c_str() : "N/A"), //date
(gps.location.isValid() ? String(gps.location.lat(), 6).c_str() : "N/A"), //latitude
(gps.location.isValid() ? String(gps.location.lng(), 6).c_str() : "N/A"), //longitude
(gps.altitude.isValid() ? String(gps.altitude.value()).c_str() : "N/A"), //Altitude GPS
(gps.speed.isValid() ? String(gps.speed.mps()).c_str() : "N/A"), //Speed
(gps.satellites.isValid() ? String(gps.satellites.value()).c_str() : "N/A"), //Satellites
String(bmp.readTemperature()).c_str(), //Temp
String(bmp.readPressure() / 100).c_str(), //Pressure HPa
String(bmp.readAltitude(pressureAtZero)).c_str(), //Altitude Baro
String(analogRead(gasPin)).c_str()
);
//Serial.println(xyz);
Serial.println(output);
dataFile = SD.open("data.csv", FILE_WRITE);
if (dataFile) {
dataFile.println(output);
dataFile.flush();
dataFile.close();
Serial.println("write succesfull");
} else {
Serial.println("error opening file");
}
}
}
}
I have two problems:
The first one is that nothing is being written to the SD card.
The second problem is that when I uncomment the ADXL345 part, char xyz is being printed, but not the output for some reason. Also, when I try to print 'xyz' and 'output' in one Serial print line by doing 'Serial.println(String(xyz) + String(output));', a blank line is printed.
When I comment the ADXL345 part, the output is being printed.
I'm stuck here for a few days now, does anyone have suggestions?
The sketch is using 96% of the storage and 80% of RAM, could that have an influence?