Thread: Storing integer into character array

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    13

    Lightbulb Storing integer into character array

    Dear all.

    I have my code as below.I have written code for arduino Uno Board.
    I am trying to send Serial command in arduino

    command1:<SMCB1,1>
    Command2:<SMCB1,1,60>
    If command 1 request Send execute Serial_Command1() and if command 2 request send Execute Serial_Command2 () function.

    I can easily switch between the command. In command1 i am trying to store Integer values into character buffer and print the character at end of function. In below code i could not able to print values after stroing can someone help to sort out issue.

    In command 2 , if i mentioned length 60. Length of character 60 of that buffer should be printed . if length 10 buffer of 10 character should be printed.

    Code:
    #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[0]=' Mod_current{0]' ;
      sendBuffer[1]= ',';
      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[0]);
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }
    
    
    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;
        }
      }
    }
    GLob.h
    Code:
    #ifndef GLOB_H
    #define GLOB_H
    
    
    
    
    
    
    
    
    int counter=0;
    
    
    int Mod_current[24];
    //int Mod_current1[8];
    //int Mod_current2[8];
    //int Mod_current3[8];
    
    
    
    
    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,652
    First of all, you're using way too many global variables.

    For example, having DC_STATUS as a global variable when you have a function containing this is just folly.
    return (DC_STATUS);

    Sure, you may be on a machine with very limited stack space, but that really only affects large arrays. In which case, you can declare them static (as you've done inside the loop() function).
    You declare the array static in the outermost function (of common use), and then pass a pointer to it as a parameter to all the dependent child functions.

    > sendBuffer[0]=' Mod_current{0]' ;
    > sendBuffer[1]= ',';
    1. I'm pretty sure you meant
    sendBuffer[0]=Mod_current[0] ;
    2. I'm pretty sure that your compiler should have given you a warning about using a character constant consisting of 15 characters!

    > int Mod_current[24];
    OK, if you want to convert each one of these to a printable character (assuming each is in the range 0 to 9 inclusive), then you need to do this:
    sendBuffer[0]=Mod_current[0] + '0';

    If any Mod_current contains a value outside the range 0 to 9, then you need to do something else. For sure, it will no longer fit into a single character!

    > Serial.println(sendBuffer[0]);
    Given that println seems to be overloaded in some way (it can take strings, integers and characters), you need to understand the difference between
    Serial.println(sendBuffer[0]); // sends the first character
    vs
    Serial.println(sendBuffer); // sends the string upto the first \0 in sendBuffer
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing input to Character Array
    By MagicWand in forum C++ Programming
    Replies: 6
    Last Post: 04-23-2016, 03:58 PM
  2. Converting character array into an integer array
    By Digisweep in forum C Programming
    Replies: 4
    Last Post: 12-06-2013, 07:03 PM
  3. Storing integers to a character array
    By C0__0D in forum C Programming
    Replies: 4
    Last Post: 01-30-2013, 09:57 PM
  4. Storing user input into integer array
    By samii1017 in forum C Programming
    Replies: 2
    Last Post: 10-28-2012, 03:31 PM
  5. Storing an integer in an array using getchar
    By Interista in forum C Programming
    Replies: 9
    Last Post: 11-04-2011, 12:37 PM

Tags for this Thread