Pointers doesn't seem to work when integrating two different sensors

Hi all,

I am trying to integrate wind sensor (LCJ) and a solar radiation sensor together on Arduino uno. If I just use any one sensor with uno, code works perfectly fine. But as i try to integrate the two sensors together, I see that the pointer in my code doesn’t quite seem to work.
A part of my code is as follows:

pString = strstr(inputString, “$IIMWV”);
pTemp = strstr(inputString, “$WIXDR”);

if((pString != NULL) && (pTemp != NULL)){
//Serial.println(“testing here…”); //with SP 2015, it does not enter this loop.FAIL!!
for(wCount=0; wCount<6; wCount++){
wSpeed[wCount] = inputString[(int)(pString-inputString+((char)15))+wCount];
}
for(wCount=0; wCount<6; wCount++){
wAngle[wCount] = inputString[(int)(pString-inputString+((char)7))+wCount];
}
for(wCount=0; wCount<6; wCount++){
wTemp[wCount] = inputString[(int)(pTemp-inputString+((char)9))+wCount];
}

I have attached the sketch to this thread as well. Please have a look and let me know what i am doing wrong.

Your help is much appreciated!

Board: Arduino Uno
IDE : Arduino IDE 1.6.5

Thanks!

IDA_demo.ino (3.27 KB)

/*
 * Title              : IDA demo
 * Author             : Samarth Kapoor
 * Date created       : 03/07/2015
 * Date last modified : 14/08/2015
 * Modified by        : Samarth Kapoor 
*/


#include<SoftwareSerial.h>

char inputString[128] = "";         // a char array to hold incoming data
SoftwareSerial mySerial(9,8);
//char *pString;
//char *pTemp;

void setup() {
  Serial.begin(57600); 
  //Serial.begin(4800); //RX2 of Due
  mySerial.begin(4800);
}

void loop() {
  serialEvent();
  //delay(533);
  solar_par();
}
  void solar_par(){
    //variables for SP 2015
    float solar_val;
    char charVal_solar[6];            
    String stringVal_solar = "";    
    //Solar radiation SP 2015 data extraction     
    int solar_sens_Value = analogRead(A1);
    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3.3V), multiplication factor of 1.52 to convert to a voltage (0 - 5V)
    //float solar_voltage = solar_sens_Value * (3.3 / 1023.0)*1.52;
    //Data sheet of SP215
    //solar_val = solar_voltage * 0.25 * 1000;
    solar_val = solar_sens_Value * 0.25 * 1000;
    dtostrf(solar_val, 2, 2, charVal_solar);  //4 is mininum width, 4 is precision; float value is copied onto buff
    for(int i=0;i<sizeof(charVal_solar);i++){
      //Serial.print(charVal_temp[i]);
    }
    //convert chararray to string
    for(int i=0;i<sizeof(charVal_solar);i++){
      stringVal_solar+=charVal_solar[i];
    } 
    //Serial.println(stringVal_solar);
  }

void serialEvent() {
  uint8_t wCount;
  uint8_t wLoopCount = 6;
  uint8_t countOne;
  char *pString;
  char *pTemp;
  char wSpeed[6] = "";
  char wAngle[6] = "";
  char wTemp[6]  = "";
  float wData;
  float wAngleData;
  float wTempData;

  while(!mySerial.available()){
    //loop until mySerial is available
  }
  while(mySerial.available()) mySerial.read(); //dump serial

  while(wLoopCount-- && wCount < 127 ){
    delay(100);
    while(mySerial.available() && wCount < 127) {
      inputString[wCount++] = (char) mySerial.read();  
    }
  }
  inputString[127] = '\0';
  delay(1);
//output it on the serial monitor  
/*    for(uint8_t countOne=0; countOne<wCount; countOne++){
      Serial.print(inputString[countOne]);
    }
*/  
  pString = strstr(inputString, "$IIMWV");   
  pTemp   = strstr(inputString, "$WIXDR");
  //Serial.println(*(pString+1));
  if((pString != NULL) && (pTemp != NULL)){
    //Serial.println("testing here.."); //with SP 2015, it does not enter this loop.FAIL!!
    for(wCount=0; wCount<6; wCount++){
      wSpeed[wCount] = inputString[(int)(pString-inputString+((char)15))+wCount]; 
    }
    for(wCount=0; wCount<6; wCount++){
      wAngle[wCount] = inputString[(int)(pString-inputString+((char)7))+wCount];
    }
    for(wCount=0; wCount<6; wCount++){
      wTemp[wCount] = inputString[(int)(pTemp-inputString+((char)9))+wCount];
    }
/*
    for(wCount=0; wCount<5; wCount++){
      Serial.print(wTemp[wCount]);    //to print the values from the buffer
    }
*/
  }else{
    Serial.println("Token not found");
  }
//convert char array to double

  wData = atof(wSpeed);
  wAngleData = atof(wAngle);
  wTempData = atof(wTemp);
    
//output to serial monitor
/*  Serial.println(wData);
  Serial.println(wAngleData);
  Serial.println(wTempData);
*/    
  memset(inputString,0,sizeof(inputString)); //clear the character array 
}

Wat?

  wSpeed[wCount] = inputString[(int)(pString-inputString+((char)15))+wCount];
  wSpeed[wCount] = inputString[(int)(pString-inputString+15)+wCount];
  wSpeed[wCount] = inputString[pString-inputString+15+wCount];
  wSpeed[wCount] = * (inputString + pString-inputString+15+wCount);
  wSpeed[wCount] = * (pString + 15 + wCount);
  wSpeed[wCount] = pString[15 + wCount];

Isn't that what you are actually trying to do here? Copy 5 characters from pString starting at position 15?

PaulMurrayCbr: Isn't that what you are actually trying to do here? Copy 5 characters from pString starting at position 15?

Also - this could be done without the loops using strncpy() and before using atof() you need to null terminate the copy.

Thank you guys for your responses. Much appreciated.

PaulMurrayCbr: Wat?

  wSpeed[wCount] = inputString[(int)(pString-inputString+((char)15))+wCount];
  wSpeed[wCount] = inputString[(int)(pString-inputString+15)+wCount];
  wSpeed[wCount] = inputString[pString-inputString+15+wCount];
  wSpeed[wCount] = * (inputString + pString-inputString+15+wCount);
  wSpeed[wCount] = * (pString + 15 + wCount);
  wSpeed[wCount] = pString[15 + wCount];

Isn't that what you are actually trying to do here? Copy 5 characters from pString starting at position 15?

Thank you for simplifying it.

Maybe I din't make my question clear. When I comment out only solar_par() in loop(), the program runs fine and I can see the output on the monitor. Also, if i comment out only serialEvent() in loop(), program runs perfectly fine. Problem comes when i am putting these two seperate entities in the same sketch. This time, sketch compiles and is uploaded on the board, but it is not able to enter this statement below:

if ((pString != NULL) && (pTemp !=NULL)){
 //code
}

Also, testing it more, inputString[] does have value inside. Its just that pString and pTemp does not point to anything which is startling!

So my question is - how is it that pString and pTemp have nothing to poiunt to even though inputString has the whole string inside it?

Thanks!

Either pString or pTemp MUST be NULL for the 'if' to fail. This implies that your input string does not contain both "$IIMWV" and "$WIXDR". We cannot see the content of your input string. Examine again your input string.

I really don't understand the code for reading the input. It seems to me you should be reading a variable length line terminated by '\r' or '\n' or both and processing it checking if for either "$IIMWV" OR "$WIXDR" (not both together) and then extracting from the line the appropriate field.

If you take a look at the documentation for atof() (google will find it) you will see that you do not actually need to copy the field since it just stops converting to a double once it reaches the end of the number.

samarth003: Maybe I din't make my question clear.

I didn't see a question, exactly:

I see that the pointer in my code doesn't quite seem to work.

"doesn't quite seem to work" is like saying my potatoes don't quite taste right.

Can you be more specific? Like, a lot more specific?

How to use this forum

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

Read this before posting a programming question