0

I have my Arduino code as below.I am trying to send Serial request and get response for it. I can able to send request mentioned in Setup function and also able to switch cases. Problem i am facing Print character buffer after storing the values. can someone suggest me how to store integer value into charcter buffer and print result later.

#include "glob.h"
const int BUFFER_SZ = 32;
char sendBuffer[60]={'0'};
void Take_Reading() {
 for (int row = 0; row < 8; row++)
 {
 // // get rid of me:
 digitalWrite(SO_enable, array[row][0]);
 digitalWrite(S1_enable, array[row][1]);
 digitalWrite(S2_enable, array[row][2]);
 delay(50);
 analog_1_pv[row] = ANALOG_SCALING * analogRead(A0);
 analog_2_pv[row] = ANALOG_SCALING * analogRead(A1);
 analog_3_pv[row] = ANALOG_SCALING * analogRead(A2);
 if ( (analog_1_pv[row] <= MINV_RANGE || (analog_1_pv[row] >= MAXV_RANGE) ))
 {
 Current_Value1[row] = 0.0;
 }
 else
 {
 Current_Value1[row] = (analog_1_pv[row] * 12.5) - 31.25;
 }
 if ( (analog_2_pv[row] <= MINV_RANGE || (analog_2_pv[row] >= MAXV_RANGE) ))
 {
 Current_Value2[row] = 0.0;
 }
 else
 {
 Current_Value2[row] = (analog_2_pv[row] * 12.5) - 31.25;
 }
 if ( (analog_3_pv[row] <= MINV_RANGE || (analog_3_pv[row] >= MAXV_RANGE) ))
 {
 Current_Value3[row] = 0.0;
 }
 else
 {
 Current_Value3[row] = (analog_3_pv[row] * 12.5) - 31.25;
 }
 Mod_current[row] = (uint16_t)(Mul_Factor * Current_Value1[row]);
 Mod_current[row + 8] = (uint16_t)(Mul_Factor * Current_Value2[row]);
 Mod_current[row + 16] = (uint16_t)(Mul_Factor * Current_Value3[row]);
 }
}
int ID_Check() {
 int ID_value;
 for (int row = 0; row < 8; row++)
 {
 digitalWrite(SO_enable, array[row][0]);
 digitalWrite(S1_enable, array[row][1]);
 digitalWrite(S2_enable, array[row][2]);
 Status_Out[row] = digitalRead(Output_Read);
 }
 ID_value = 1 * Status_Out[7] + 2 * Status_Out[6] + 4 * Status_Out[5] + 8 * Status_Out[4] + 16 * Status_Out[3] + 32 * Status_Out[2] + 64 * Status_Out[1] + 128 * Status_Out[0];
 return (ID_value);
}
int Take_Temp_Reading()
{
 Temp_Total = Temp_Total - Temp_readings[Temp_index];
 Temp_readings[Temp_index] = analogRead(A5);
 Temp_Total = Temp_Total + Temp_readings[Temp_index];
 Temp_index = Temp_index + 1;
 if (Temp_index >= numReadings)
 {
 Temp_index = 0;
 Temp_Average = Temp_Total / numReadings;
 }
 temp = (Temp_Average * 5.0) / 1023.0;
 // temp = float(analogRead(A5)) * 5.0 / 1024.0;
 temp_int = (int)(temp * 100.0);
 return (temp_int);
 // Serial.print("Temp"); Serial.println(temp);
}
int SPD_Check()
{
 SPD_STATUS = digitalRead(SPD_STATUS_PIN);
 return (SPD_STATUS);
}
int DC_Status()
{
 DC_STATUS = digitalRead(DC_STATUS_PIN);
 return (DC_STATUS);
}
int Take_HV_Reading()
{
 //int analog_int= analogRead(A4);
 total = total - readings[index];
 // read from the sensor:
 readings[index] = analogRead(A4);
 // add the reading to the total:
 total = total + readings[index];
 // advance to the next position in the array:
 index = index + 1;
 // if we're at the end of the array...
 if (index >= numReadings1)
 {
 index = 0;
 average = total / numReadings1;
 }
 else
 {
 HV_voltage = (average * 5.0) / 1023.0;
 }
 if (HV_voltage <= 0.25)
 {
 Conv_HV_voltage = 0.0;
 }
 else
 {
 Conv_HV_voltage = 197.837837838 * HV_voltage + 10.8108108108;
 }
 HV_Reading = (uint16_t)(Conv_HV_voltage * 10);
 // HV_Reading = (uint16_t)(Conv_HV_voltage * 10);
 return(HV_Reading);
}
void Serial_Command1()
{
 Take_Reading();
 int Temp_read=Take_Temp_Reading();
 int HV_Read=Take_HV_Reading();
 SPD=SPD_Check();
 DISCONNECTOR=DC_Status();
 Serial.println("Command1 executed ");
 sendBuffer[1]=char(Mod_current[1]) ;
 sendBuffer[2]= ',';
 sendBuffer[2]= Mod_current[1];
 sendBuffer[3]= ',';
 sendBuffer[4]= Mod_current[2];
 sendBuffer[5]= ',';
 sendBuffer[6]= Mod_current[3];
 sendBuffer[7]= ',';
 sendBuffer[8]= Mod_current[4];
 sendBuffer[9]= ',';
 sendBuffer[10]= Mod_current[5];
 sendBuffer[11]= ',';
 sendBuffer[12]= Mod_current[6];
 sendBuffer[13]= ',';
 sendBuffer[14]= Mod_current[7];
 sendBuffer[15]= ',';
 sendBuffer[16]= Mod_current[8];
 sendBuffer[17]= ',';
 sendBuffer[18]= Mod_current[9];
 sendBuffer[19]= ',';
 sendBuffer[20]= Mod_current[10];
 sendBuffer[21]= ',';
 sendBuffer[22]= Mod_current[11];
 sendBuffer[23]= ',';
 sendBuffer[24]= Mod_current[12];
 sendBuffer[25]= ',';
 sendBuffer[26]= Mod_current[13];
 sendBuffer[27]= ',';
 sendBuffer[28]= Mod_current[23];
 sendBuffer[29]= ',';
 sendBuffer[30]= Mod_current[22];
 sendBuffer[31]= ',';
 sendBuffer[32]= Mod_current[21];
 sendBuffer[33]= ',';
 sendBuffer[34]= Mod_current[20];
 sendBuffer[35]= ',';
 sendBuffer[36]= Mod_current[19];
 sendBuffer[37]= ',';
 sendBuffer[38]= Mod_current[18];
 sendBuffer[39]= ',';
 sendBuffer[40]= Mod_current[17];
 sendBuffer[41]= ',';
 sendBuffer[42]= Mod_current[16];
 sendBuffer[43]= ',';
 sendBuffer[44]= Mod_current[15];
 sendBuffer[45]= ',';
 sendBuffer[46]= Mod_current[14];
 sendBuffer[47]= ',';
 sendBuffer[48]= Temp_read;
 sendBuffer[49]= ',';
 sendBuffer[50]= HV_Read;
 sendBuffer[51]= ',';
 sendBuffer[52]= SPD;
 sendBuffer[53]= ',';
 sendBuffer[54]= DISCONNECTOR;
 Serial.println(sendBuffer[1]);
}
void Serial_Command2(int a)
{
 Serial.print("Command2 executed: ");
 Serial.println(a);
}
void setup()
{
 Serial.begin(57600);
 Serial.println("Format 1: <SMCB1,1>");
 Serial.println("Format 2: <SMCB1,1,Length>");
}
// Parse the request, WITHOUT the '<' and '>' delimiters.
void parseRequest(char *request)
{
 // Check the request starts with the prefix "SMCB1,1".
 if (strncmp(request, "SMCB1,1", 7) != 0) {
 Serial.println(F("Error: bad request prefix"));
 return;
 }
 // Remove that known prefix.
 // Now we have either "", ",Length" or ",Timer,On_OFF"
 request += 7;
 // Format 1: ""
 if (request[0] == '0円') {
 //Serial.println(F("Received format 1"));
 Serial_Command1();
 return;
 }
 // Remove the leading comma.
 // Now we have either "Length" or "Timer,On_OFF"
 if (request[0] != ',') {
 Serial.println(F("Error: ',' expected"));
 return;
 }
 request++;
 // Format 2: "Length", no comma.
 char * comma = strchr(request, ',');
 if (!comma) {
 int length = atoi(request);
 Serial_Command2(length);
 //Serial.print(F("Received format 2, length = "));
 // Serial.println(length);
 return;
 }
}
void loop()
{
 static char buffer[BUFFER_SZ]; // received chars
 static size_t pos; // current position in buffer
 static bool insideRequest; // are we between '<' and '>'?
 if (Serial.available()) {
 char c = Serial.read();
 if (insideRequest) {
 if (c == '>') { // end of request received
 buffer[pos] = '0円'; // terminate the string
 parseRequest(buffer);
 pos = 0; // get ready for the next request
 insideRequest = false;
 }
 else if (pos < BUFFER_SZ-1) { // add char to buffer
 buffer[pos++] = c;
 }
 }
 else if (c == '<') { // start of request
 insideRequest = true;
 }
 }
}

Golb.h header file contains

#ifndef GLOB_H
#define GLOB_H
int counter=0;
int Mod_current[24];
const int numReadings = 10;
const int numReadings1 = 10;
int Temp_Total = 0;
int Temp_readings[numReadings];
int Temp_index = 0;
int Temp_Average = 0;
float reading[10];
float Total = 0.0;
static int i;
int readings[numReadings];
int index = 0;
int total = 0;
int average = 0;
// These PinS are use to enable and disable analog MUX
int SO_enable = 5;
int S1_enable = 4;
int S2_enable = 3;
// temprature sensor permeter
int TempPin = 5;
float temp;
static int temp_int;
// digital mux setting
int Output_Read = 2;
float Vref = 2.5;
// Used for sensor conversion
float ANALOG_SCALING = 0.004887585532746823;
//tempfloat ANALOG_SCALING=0.0049560117;
int Sensor_Value0 = 0;
int Sensor_Value1 = 0;
int Sensor_Value2 = 0;
float analog_1_pv[8];
float analog_2_pv[8];
float analog_3_pv[8];
int Status_Out[8];
//SPD & DC STATUS PIN
static int SPD_STATUS_PIN = 6;
static int DC_STATUS_PIN = 7;
int row;
int Mul_Factor = 10;
float Current_Value1[8] = {
 0.0
};
float Current_Value2[8] = {
 0.0
};
float Current_Value3[8] = {
 0.0
};
//HV perameter setting are defined here
int HV_SensorValue;
int HV_Reading;
float HV_voltage;
float Conv_HV_voltage;
unsigned int DC_STATUS = 0;
unsigned int SPD_STATUS = 0;
float MINV_RANGE = 2.52;
//float MIDV_RANGE1=1.875;
float MAXV_RANGE = 4.5;
int SPD;
int DISCONNECTOR;
int Serial_Status=0;
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
 }
};
#endif
asked Jun 6, 2016 at 11:47

1 Answer 1

3

The simple answer is: don't.

At the moment you are trying to assign integers to chars, and that just won't fit. It's like a round peg in a square hole. Or more like trying to force a bowling pin through a hose pipe. It just isn't going to fit.

So you either need to "print" the values into your final string (say with sprintf()) or just don't build up a string at all and print the values directly. And while you're at it, use some loops to make your code simpler:

for (int i = 0; i < 14; i++) {
 Serial.print(Mod_current[i]);
 Serial.print(",");
}
for (int i = 23; i > 13; i--) {
 Serial.print(Mod_current[i]);
 Serial.print(",");
}
Serial.print(Temp_read);
Serial.print(",");
Serial.print(HV_Read);
Serial.print(",");
Serial.print(SPD);
Serial.print(",");
Serial.println(DISCONNECTOR);

If you really need to create a string the easiest way is probably with sprintf (or snprintf to prevent buffer overflows):

char sendBuffer[200]; // Enough room for 28 integers of 1-5 characters, plus commas.
snprintf(sendBuffer, 200, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
 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[12], Mod_current[13], Mod_current[14], 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], Temp_read, HV_Read, SPD, DISCONNECTOR);
int len = strlen(sendBuffer);
Serial.println(sendBuffer);
Serial.print(len);
Serial.println(" bytes");

(note: I think I got the right number of parameters there)

answered Jun 6, 2016 at 11:54
3
  • Yes i know that. For my second case , I would like to print Serial statement based on length. It wont work above case. How can i print of specified length.thats reason i tried with String intially but memory is exceeding and controller gets restarts frequently Commented Jun 6, 2016 at 12:03
  • In that case you will need to use something like sprintf to print your values into the string. I will add an example. Commented Jun 6, 2016 at 12:04
  • @ in second command i am mentioning length of buffer . I am passing length int as argument So i can print Specified length of character Commented Jun 6, 2016 at 12:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.