I have no prior knowledge of programming/coding and don't know how to make my code efficient so its not glitching. The Sketch uses 23194 bytes (71%) out of 32256 bytes and Global variables use 1624 bytes (79%) of dynamic memory out of 2048 bytes. The error shows; Low memory available, stability problems may occur.
I have air quality sensor, temperature&humidity sensor, gps and LCD screen with SD card reader attached to arduino uno and I would like the sensors to be recorded on the SD card as at the moment it doesn't read all the sensors and I think its because of the low memory issue. The lcd screen is to just read the results for air quality mainly but if it will help increase the memory it can be rid off.
Any help will be appreciated!!! thank you!
CODE:
#include <Wire.h>
#include <LiquidCrystal.h>
#include <dht.h>
#include <SDS011.h>
#include <SD.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
dht DHT;
#define DHT11_PIN 3
int cs = 4; // Set Chip Select to pin ten
float p10, p25;
int error;
SDS011 my_sds;
LiquidCrystal lcd( 8,9,4,5,6,7 ); //interfacing pins
File myFile;
char myFileName[] = "test.txt";
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
int displaymode;
void setup()
{
Serial.begin(9600);
while (!Serial) { }
Serial.println("Initializing SD card...");
Serial.println();
pinMode(cs, OUTPUT);
pinMode(SS, OUTPUT); // Documentation says you're supposed to do this
// see if the card is present and can be initialized:
if (!SD.begin(cs)) {
Serial.println("SD did not initialize");
while (1) ;
}
Serial.println("SD initialized.");
ss.begin(GPSBaud);
my_sds.begin(2, 6); //RX, TX
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(0,0);
lcd.print("LCD Key Shield");
lcd.setCursor(0,1);
lcd.print("Press Key:");
displaymode=1;
}
void loop()
{
int x;
int chk = DHT.read11(DHT11_PIN); // read temp & humidty
error = my_sds.read(&p25, &p10); // reaad air quality
gps.encode(ss.read()); // read gps
Serial.print("DisplayMode = "+String(displaymode));
Serial.print("Temperature = ");
Serial.println(DHT.temperature);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
Serial.println("P2.5: " + String(p25));
Serial.println("P10: " + String(p10));
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
myFile = SD.open(myFileName, FILE_WRITE);
// This next statement checks to see if the file
myFile.println("==================="); // Send Your First Line to that file
myFile.print("Temperature = ");
myFile.println(DHT.temperature);
myFile.print("Humidity = ");
myFile.println(DHT.humidity);
myFile.println("P2.5: " + String(p25));
myFile.println("P10: " + String(p10));
myFile.print("Latitude= ");
myFile.print(gps.location.lat(), 6);
myFile.print(" Longitude= ");
myFile.println(gps.location.lng(), 6);
myFile.flush(); // Save it.
if (displaymode==1) {
lcd.print("Temp: ");
lcd.println(DHT.temperature);
lcd.print("Hum: ");
lcd.println(DHT.humidity);
} else if (displaymode == 2) {
lcd.println("P2.5: " + String(p25));
lcd.println("P10: " + String(p10));
} else if (displaymode == 3) {
lcd.print("Latitude= ");
lcd.print(gps.location.lat(), 6);
lcd.print(" Longitude= ");
lcd.println(gps.location.lng(), 6);
}
delay(2000);
x = analogRead (0);
lcd.setCursor(10,1);
/* if (x < 60) {
displaymode=1; //lcd.print ("Right ");
}
else if (x < 200) {
displaymode=2; //lcd.print ("Up "); //analog voltage 145
}
else if (x < 400){
displaymode=3; //lcd.print ("Down "); //analog voltage 329
}
else if (x < 600){
displaymode=4; //lcd.print ("Left "); //analog voltage 585
}
else if (x < 800){
lcd.print ("Select"); //analog voltage 741
*/
}