With below code i make it working, For small delay it give properly output. if i make delay of 1 minute its get reset MCU. Please let me know how to resolve.
#include <avr/wdt.h>
#include"glob.h"
String Old_string;
String New_String;
String Received_String;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
static int OverRide_flag = 0;
// variables to hold the parsed data
char messageFromPC[numChars] = {
0
};
int integerFromPC = 0;
float floatFromPC = 0.0;
boolean newData = false;
int array[8][3] = {
{
0, 0, 0
}
, {
0, 0, 1
}
, {
0, 1, 0
}
, {
0, 1, 1
}
, {
1, 0, 0
}
, {
1, 0, 1
}
, {
1, 1, 0
}
, {
1, 1, 1
}
};
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()
{
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
integerFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Length_String = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Start_Stop = atoi(strtokIndx); // convert this part to an integer
Received_String = Received_String + messageFromPC + "," + integerFromPC;
}
void showParsedData()
{
// HV_value= Take_HV_Reading();
// temp_int = Take_Temp_Reading();
// temp_int = (uint16_t)temp_int;
New_String = Old_string + "SMCB1" + "," + Device_ID + "," + Mod_current[0] + "," + Mod_current[1] + "," + Mod_current[2] + "," + Mod_current[3] + "," + Mod_current[4] + "," + Mod_current[5] + "," + Mod_current[6] + "," + Mod_current[7] + "," + Mod_current[8] + "," + Mod_current[9] + "," + Mod_current[10] + "," + Mod_current[11] + "," + Mod_current[23] + "," + Mod_current[22] + "," + Mod_current[21] + "," + Mod_current[20] + "," + Mod_current[19] + "," + Mod_current[18] + "," + Mod_current[17] + "," + Mod_current[16] + "," + Mod_current[15] + "," + Mod_current[14] + "," + Mod_current[13] + "," + Mod_current[12] + "," + temp_int + "," + SPD + "," + DISCONNECTOR + "," + HV_value;
// Serial.println();
String firstString = Received_String.substring(0, 14);
Serial.println(firstString);
String SecondString = New_String.substring(0, 7);
// Serial.println(Length_String);
if ((firstString == SecondString) && (Length_String == 0))
{
Serial.println(New_String);
// OverRide_flag=1;
}
else
{
//Serial.println("wrong command send");
}
if ((Length_String > 8 ) && (firstString == SecondString))
{
// Serial.println("Length condition");
Serial.println(New_String.substring(0, Length_String));
OverRide_flag = 0;
// newData = false;
}
if ((Length_String > 8 ) && (firstString == SecondString) && (Start_Stop == 1))
{
// Serial.println(New_String);
//delay(Length_String);
newData = false;
OverRide_flag = 1;
}
else
{
newData = true;
OverRide_flag = 0;
}
Received_String = "";
New_String = "";
}
void setup()
{
wdt_enable(WDTO_8S);
Serial.begin(57600);
//analogReference(INTERNAL);
pinMode(3, OUTPUT);
pinMode(SO_enable, OUTPUT) ;// pin can enable/disable using digital IO 7 of arduino
pinMode(S1_enable, OUTPUT) ;// pin can enable/disable using digital IO 6 of arduino
pinMode(S2_enable, OUTPUT) ;// pin can enable/disable using digital IO 5 of arduino
// pinMode(Enablepin, OUTPUT) ;// pin can enable/disable using digital IO 4 of arduino
pinMode(A0, INPUT) ;
pinMode(A5, INPUT) ;
//spd & DC STATUS
pinMode(SPD_STATUS_PIN, INPUT);
pinMode(DC_STATUS_PIN, INPUT);
Serial.println("Format1: <SMCB1,1>");
Serial.print("Format2: <SMCB1,1,Length>");
Serial.print("\t");
Serial.println("Send serial command in this format <SMCB1,1,100>");
Serial.print("Format3: <SMCB1,1,Timer,On_OFF(1-ON,0-OFF)>");
Serial.print("\t");
Serial.println("Send serial command in this format <SMCB1,1,1000,1>");
}
void loop() {
wdt_reset();
recvWithStartEndMarkers() ;
HV_value = Take_HV_Reading();
Take_Reading();
SPD = SPD_Check();
DISCONNECTOR = DC_Status();
temp_int = Take_Temp_Reading();
temp_int = (uint16_t)temp_int;
if (newData == true)
{
strcpy(tempChars, receivedChars);
parseData();
showParsedData();
newData = false;
}
if ( OverRide_flag == 1)
{
Device_ID = 1;
New_String = Old_string + "SMCB1" + "," + Device_ID + "," + Mod_current[0] + "," + Mod_current[1] + "," + Mod_current[2] + "," + Mod_current[3] + "," + Mod_current[4] + "," + Mod_current[5] + "," + Mod_current[6] + "," + Mod_current[7] + "," + Mod_current[8] + "," + Mod_current[9] + "," + Mod_current[10] + "," + Mod_current[11] + "," + Mod_current[23] + "," + Mod_current[22] + "," + Mod_current[21] + "," + Mod_current[20] + "," + Mod_current[19] + "," + Mod_current[18] + "," + Mod_current[17] + "," + Mod_current[16] + "," + Mod_current[15] + "," + Mod_current[14] + "," + Mod_current[13] + "," + Mod_current[12] + "," + temp_int + "," + SPD + "," + DISCONNECTOR + "," + HV_value;
Serial.println(New_String);
delay(Length_String);
New_String = "";
}
}