Search code examples
arduino-c++

Problem calling a function with a pointer Arduino


I am fairly new to C++ and anything but the basics of Arduino, so I am stuck with this. I was handed a function to include in my code to do some calculations on a sensor value I am reading. I searched and read a lot of similar topics on here and on the web in general, but failed to find anything that helps me solve my lack of understanding.

I setup a basic Arduino sketch with a loop to read the sensor value all good. I then #included the header file and added the function to the bottom of my code outside the loop just to see if it will compile,which it did without any errors. So now a need to call the function and pass 2 values to it, millis() and the sensor value I just read.

Part of the header file...

struct TCS1000v {
    unsigned short int  u16RawVal;      
    unsigned short int  u16RawValPrev;  
    unsigned short int  u16CycleTime;
    unsigned short int  u16Iso4um;      
    unsigned short int  u16Iso6um;      
    unsigned short int  u16Iso14um; 
        .....
};

typedef struct TCS1000v;

extern void vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime);

The loop part of the sketch

void loop() {
    // reading the sensor...
    unsigned short int u16CycleTime = millis();
    unsigned short int u16RawVal = adc.readsensor(channel);
    // the function to call - not sure about this?
    vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
}

The function as supplied...

    void vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
{
    ptCS1000->u16RawVal   = u16RawVal;
    ptCS1000->u16CycleTime  = u16CycleTime;
    //.... and the rest of the code in the function
}

Compile errors...

test.ino: In function 'void loop()':
cs1000:30:25: error: expected primary-expression before '*' token
   vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
                         ^
cs1000:30:27: error: 'ptCS1000' was not declared in this scope
   vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
                           ^
cs1000:30:37: error: expected primary-expression before 'unsigned'
   vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
                                     ^
cs1000:30:67: error: expected primary-expression before 'unsigned'
   vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
                                                                   ^
exit status 1
expected primary-expression before '*' token

So the struct is setup in the header file, but the problem seems to be with the pointer? Or am I just not calling the function correctly?


Solution

  • In the end I had to set a global variable and it's all working now. Thanks for looking :)

    TCS1000v g_tCS1000v;  
    vCalcCS1000v2(&(g_tCS1000v), RawVal, CycleTime);