Thank you very much for the guidance. A Small change “delay(10);” in RS485 data acquisition correct the data order.
#include <SoftwareSerial.h>
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <LCD5110_Basic.h>
SoftwareSerial mySerial(8, 9); // RX, TX
LCD5110 myGLCD(5,6,7,3,2);
extern uint8_t SmallFont[];//LCD
tmElements_t tm;//time
int t_second,V=230,I=5;
String time_now;
void setup() {
myGLCD.InitLCD();
myGLCD.setFont(SmallFont);
pinMode(A0,OUTPUT);
Serial.begin(9600);
mySerial.begin(9600);
delay(1000);
if (!SD.begin(4))
{
myGLCD.clrScr();
myGLCD.print("SD failed", CENTER, 16);
delay(2000);
}
else
{
myGLCD.clrScr();
myGLCD.print("Status OK", CENTER, 16);
delay(1000);
}
}
void loop() {
if (RTC.read(tm))
{
if (t_second != tm.Second )
{
t_second = tm.Second;
SaveData();
//Serial.println(t_second);
}
}
}
//-----------------------------SD card Save---------------------------------------------
void SaveData(){
String VC_Data,E_Data;
time_now =String(tmYearToCalendar(tm.Year))+"-"+c2d(tm.Month)+"-"+c2d(tm.Day) + " " + c2d(tm.Hour)+ ":" + c2d(tm.Minute)+":"+ c2d(tm.Second);
File dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile)
{
if(SD.exists("data.txt")){
VC_Data =RS485_All();
E_Data=RS485_Energy();
dataFile.println(time_now+VC_Data+E_Data);
dataFile.close();
myGLCD.clrScr();
myGLCD.print("A: "+ String(StrToFloat(VC_Data.substring(1,8))*V)+" V", LEFT, 0);
myGLCD.print(" : "+ String(StrToFloat(VC_Data.substring(9,14))*I)+" A", LEFT, 8);
//myGLCD.print("B: "+ String(StrToFloat(VC_Data.substring(15,22))*V)+" V", LEFT, 16);
//myGLCD.print(" : "+ String(StrToFloat(VC_Data.substring(23,29))*I)+" A", LEFT, 24);
//myGLCD.print("C: "+ String(StrToFloat(VC_Data.substring(30,37))*V)+" V", LEFT, 32);
}
else
{
myGLCD.clrScr();
myGLCD.print("Insert SD", CENTER, 8);
//myGLCD.print("SD Card", CENTER, 24);
}
}
}
//-----------------------------------String To Float --------------------------------
float StrToFloat(String Str_Raw_Data)
{
return atof (Str_Raw_Data.c_str ());
}
//-----------------------------------Convert two Digit number------------------------
String c2d(int x){
String conv;
if (x < 10) {
conv ="0" + String(x);
}
else
{
conv= String(x);
}
return conv;
}
//----------------------------------Get data from RS485 ASCII-------------------------
String RS485_All(){
String a;
char data_byte;
digitalWrite(A0, HIGH);
mySerial.write("#01A\r");
digitalWrite(A0, LOW);
delay(10);
while(mySerial.available())
{
data_byte=mySerial.read();
a=a+(String)data_byte;
}
return a;
}
//-----------------Data02-----------------------------
String RS485_Energy(){
String b;
char Energy;
digitalWrite(A0, HIGH);
mySerial.write("#01W\r");
digitalWrite(A0, LOW);
delay(10);
while(mySerial.available())
{
Energy=mySerial.read();
b=b+(String)Energy;
}
return b;
}
But when I try to display in the LCD I encounter some issues, I suspect that it’s about Flash Memory and once it exceed 85%, mysterious characters appear again.
I cannot uncomment following lines in SaveData() (same result can observe though I add a code other than this if it exceed 85% (27,600 bytes) )
myGLCD.clrScr();
myGLCD.print("A: "+ String(StrToFloat(VC_Data.substring(1,8))*V)+" V", LEFT, 0);
myGLCD.print(" : "+ String(StrToFloat(VC_Data.substring(9,14))*I)+" A", LEFT, 8);
//myGLCD.print("B: "+ String(StrToFloat(VC_Data.substring(15,22))*V)+" V", LEFT, 16);
//myGLCD.print(" : "+ String(StrToFloat(VC_Data.substring(23,29))*I)+" A", LEFT, 24);
//myGLCD.print("C: "+ String(StrToFloat(VC_Data.substring(30,37))*V)+" V", LEFT, 32);
-
To get rid of this, how could I modify SD library (I don’t want other than write to SD card) and NOKIA-5110 LCD library (I just need simple font only)?
-
Does this approach feasible?
I have attached the libraries.
I tried to get rid of String library, but It seems cannot eliminate completely…
LCD5110_Basic.zip (388 KB)
SD.zip (57.9 KB)