Hello there!!
Currently i am working on a project where I am supposed to accept the inputs from the serial monitor in format <Chiran, 99, 99.9,....>(so on these values are just for example but the format would remain same) and load it on to DMD display. I am using 1 down 4 across DMD mono color. My problem is my code makes use too many of "String" and I don't know how to reduce them or is there any way to achieve it by minimum use of "String" also I am not quite sure if making use of "String these many times creates any problems or not. any help? Thanks in advance.
#include "SPI.h"
#include "DMD.h"
#include "TimerOne.h"
#include "Arial_black_16.h"
#define DISPLAYS_ACROSS 4
#define DISPLAYS_DOWN 1
DMD dmd( DISPLAYS_ACROSS , DISPLAYS_DOWN );
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int int_DD = 0;
int int_MM = 0;
int int_YY = 0;
int int_HR = 0;
int int_Min= 0;
int int_Sec= 0;
int int_Total_FB = 0;
int int_Total_Air= 0;
int int_Total_toilt = 0;
int int_Total_service = 0;
int int_Air_avg = 0;
int int_toilt_avg = 0;
int int_service_avg = 0;
int int_total_avg = 0;
boolean newData = false;
String mac;
String date;
String times;
String Total_fb;
String Total_Air;
String Total_toilt;
String Total_Service;
String Air_avg;
String Toilt_avg;
String Service_avg;
String Total_avg;
String ScrollDate = "Date: ";
String ScrollSemi = ":";
String ScrollTime = "Time: ";
String ScrollText1 = "Total Feedback: ";
String ScrollText2 = "Total Air Feedback: ";
String ScrollText3 = "Total Toilet Feedback: ";
String ScrollText4 = "Total Service Feedback: ";
String ScrollText5 = "Total Average Feedback: ";
String ScrollText6 = "Total Average Feedback for Air: ";
String ScrollText7 = "Total Average Feedback for Toilet: ";
String ScrollText8 = "Total Average Feedback for Overall Service: ";
void ScanDMD()
{
dmd.scanDisplayBySPI();
}
//============
void setup() {
Serial.begin(9600);
Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
Serial.println("Enter data in this style <HelloWorld, 12, 24.7> ");
Serial.println();
Timer1.initialize( 5000 );
Timer1.attachInterrupt( ScanDMD );
dmd.clearScreen( true );
}
void drawText( String dispString )
{
dmd.clearScreen( true );
dmd.selectFont( Arial_Black_16 );
char newString[256];
int sLength = dispString.length();
dispString.toCharArray( newString, sLength+1 );
dmd.drawMarquee( newString , sLength , ( 32*DISPLAYS_ACROSS )-1 ,0);
long start=millis();
long timer=start;
long timer2=start;
boolean ret=false;
while( !ret ){
if ( ( timer+50 ) < millis() ) {
ret=dmd.stepMarquee( -1 , 0 );
timer=millis();
}
}
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
drawText(messageFromPC);
drawText(mac);
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
int_DD = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
int_MM = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
int_YY = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
int_HR = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
int_Min = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
int_Sec = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
int_Total_FB = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
int_Total_Air = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
int_Total_toilt = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
int_Total_service = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
int_Air_avg = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
int_toilt_avg = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
int_service_avg = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
int_total_avg = atoi(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.print("Message ");
Serial.println(messageFromPC);
//day
Serial.print("Day ");
Serial.println(int_DD);
//month
Serial.print("Month ");
Serial.println(int_MM);
//year
Serial.print("Year ");
Serial.println(int_YY);
//hour
Serial.print("Hour ");
Serial.println(int_HR);
//min
Serial.print("Mins ");
Serial.println(int_Min);
//sec
Serial.print("Seconds ");
Serial.println(int_Sec);
//int_Total_FB
Serial.print("int_Total_FB");
Serial.println(int_Total_FB);
//int_Total_Air
Serial.print("int_Total_Air ");
Serial.println(int_Total_Air);
//int_Total_toilt
Serial.print("int_Total_toilt ");
Serial.println(int_Total_toilt);
//int_Total_service
Serial.print("int_Total_service ");
Serial.println(int_Total_service);
//int_Air_avg
Serial.print("int_Air_avg ");
Serial.println(int_Air_avg);
//int_toilt_avg
Serial.print("int_toilt_avg ");
Serial.println(int_toilt_avg);
//int_service_avg
Serial.print("int_service_avg ");
Serial.println(int_service_avg);
//int_total_avg
Serial.print("int_total_avg ");
Serial.println(int_total_avg);
/*
mac = String(integerFromPC);
Serial.print("Mac:");
Serial.println(mac);
*/
date = String(ScrollDate + int_DD + ScrollSemi + int_MM + ScrollSemi + int_YY);
times = String(ScrollTime + int_HR + ScrollSemi + int_Min + ScrollSemi + int_Sec);
Serial.print("date:");
Serial.println(date);
/*
Total_fb = String();
String Total_Air;
String Total_toilt;
String Total_Service;
-
String Air_avg;
String Toilt_avg;
String Service_avg;
String Total_avg;
*/
}