comparing current values and previous value regarding struct types

Hi all,

i'm quite new to this community and would like to thank all those who given their precious time to view this thread.

I have some problems trying to have the current values and the previous values. i am using punchthrough bean+ as a platform.

I am trying to compare the previous accelerometer values which needs me to use a struct type data, to the current one which the code is displayed below.

void setup()
{
    Serial.begin(57600);
    
}
void loop()
{
    int16_t x_last = abs(reading.xAxis/2);
    AccelerationReading reading = Bean.getAcceleration(); 
    int16_t x = abs(reading.xAxis/2);
    int16_t y = abs(reading.yAxis/2);
    int16_t z = (reading.zAxis/2);
   
    

Serial.print("x_last=");
      Serial.println(x_last);
      Serial.print("x=");
      Serial.println(x);
      delay(250);
      

    if (x >= x_last + 10) {
      Bean.setLed(0, 255, 0); //Green Led
        
    }
}

I got a error stating that my "reading" was not declared in this scope.

I tried declaring "(reading.xAxis/2)" at the void setup section but still to no avail.

I've tried to search the forum and can't find anything related to my problem.

Appreciate any help, and thanks in advance!

Hi,
As I see it, Setup is not terminated with expected "}" and so Loop is inside Setup??

hi, sorry i miss out that part. the setup is terminated with }.

code should be as such.

void setup()
{
    Serial.begin(57600);
    reading.xAxis/2 = 0;
}

void loop()
{
    int16_t x_last = abs(reading.xAxis/2);
    AccelerationReading reading = Bean.getAcceleration(); 
    int16_t x = abs(reading.xAxis/2);
    int16_t y = abs(reading.yAxis/2);
    int16_t z = (reading.zAxis/2);
   
    

Serial.print("x_last=");
      Serial.println(x_last);
      Serial.print("x=");
      Serial.println(x);
      delay(250);
      

    if (x >= x_last + 10) {
      Bean.setLed(0, 255, 0); //Green Led
        
    }
    }

Where did you declare your struct data?

arduino_new:
Where did you declare your struct data?

Hi, the struct data is already self declared in the bean's program so i do not need to declare AccelerationReading as a struct type.

the AccelerationReading at the line "AccelerationReading reading = Bean.getAcceleration();" is the the struct type.

i believe reading is the int declared in the struct type, and the problem i am facing is to declare the "reading" variable to allow my code to work for the old values variable "x_last", to compare the new and old values.

Did you include the correct library where the struct is declared?

arduino_new:
Did you include the correct library where the struct is declared?

the codes can run if i didnt input the "x_new" variable in. so i believed is not the library that is not declared.

Did you declare "reading" before using it in setup()?

What do you mean by "the codes can run if i didnt input the "x_new" variable in"? where does "x_new" come from?

Btw,

C/C++ has no build-in comparison function for struct, so you need to use memcmp() or overload operator= for the struct.

arduino_new:
Did you declare "reading" before using it in setup()?

What do you mean by "the codes can run if i didnt input the "x_new" variable in"? where does "x_new" come from?

Btw,

C/C++ has no build-in comparison function for struct, so you need to use memcmp() or overload operator= for the struct.

Thanks for the help thus far.

i've edited the code to declare the variable at setup but still ran into some errors. hope you would be able to assist as im at my wits.

void setup()
{
    Serial.begin(57600);
    int16_t reading = 0;
}

void loop()
{

    int16_t x_last = abs(reading.xAxis/2);
    AccelerationReading reading = Bean.getAcceleration(); 
    int16_t x = abs(reading.xAxis/2);
    int16_t y = abs(reading.yAxis/2);
    int16_t z = (reading.zAxis/2);
   
    

Serial.print("x_last=");
      Serial.println(x_last);
      Serial.print("x=");
      Serial.println(x);
      delay(250);
      

    if (x >= x_last + 10) {
      Bean.setLed(0, 255, 0); //Green Led
        
    }}

Arduino: 1.8.4 (Windows 10), Board: "LightBlue Bean+ (2.0.3)"

In file included from sketch\accelvalues2.ino.cpp:1:0:

C:\Users\User\Desktop\FYP\Arduino\accelvalues2\accelvalues2.ino: In function 'void loop()':

accelvalues2:10: error: 'reading' was not declared in this scope

int16_t x_last = abs(reading.xAxis/2);

^

C:\Program Files (x86)\Arduino\hardware\LightBlue-Bean\avr\cores\bean/Arduino.h:66:18: note: in definition of macro 'abs'

#define abs(x) ((x)>0?(x):-(x))

^

exit status 1
'reading' was not declared in this scope

Move this line up so it's the first thing in the loop function:

    AccelerationReading reading = Bean.getAcceleration();

KJJ:
i've edited the code to declare the variable at setup but still ran into some errors. hope you would be able to assist as im at my wits.

void setup()

{
    Serial.begin(57600);
    int16_t reading = 0;
}

void loop()
{

int16_t x_last = abs(reading.xAxis/2);
    AccelerationReading reading = Bean.getAcceleration();
    int16_t x = abs(reading.xAxis/2);
    int16_t y = abs(reading.yAxis/2);
    int16_t z = (reading.zAxis/2);

Serial.print("x_last=");
      Serial.println(x_last);
      Serial.print("x=");
      Serial.println(x);
      delay(250);

if (x >= x_last + 10) {
      Bean.setLed(0, 255, 0); //Green Led
       
    }}

is that your ENTIRE code?

BulldogLowell:
is that your ENTIRE code?

Yes it is

KJJ:
Yes it is

well, you are missing the creation of some objects. then...

    int16_t x_last = abs(reading.xAxis/2);
    AccelerationReading reading = Bean.getAcceleration(); 
    int16_t x = abs(reading.xAxis/2);

where is Bean created?
where is a reading created?

BulldogLowell:
well, you are missing the creation of some objects. then...

    int16_t x_last = abs(reading.xAxis/2);

AccelerationReading reading = Bean.getAcceleration();
    int16_t x = abs(reading.xAxis/2);




where is `Bean` created?
where is a `reading` created?

I believe the structure is self declared inside the software where once I entered the following code, I am declaring reading.
AccelerationReading reading = Bean.getAcceleration();
Thus, I’m trying to find a way to do a “previous and current value” for comparison but there’s the error which shows states that “reading” is not declared before my struct is declared. This I have no idea to solve..

KJJ:
I believe the structure is self declared inside the software where once I entered the following code, I am declaring reading.

So what is an AccelerationReading?

It isn't defined in the code you posted!

are you missing a library?

BulldogLowell:
So what is an AccelerationReading?

It isn't defined in the code you posted!

are you missing a library?

Hmm.. to be honest, im not sure. I believed it is the structure that is predefined in the code for me to call the structure out.

KJJ:
Hmm.. to be honest, im not sure. I believed it is the structure that is predefined in the code for me to call the structure out.

You have to define it, or you have to include a library that defines it.

The single exception to that are the "native" functions included in(or by) the Arduino.h header automatically included during compilation... functions like digitalRead() and pinMode() for example.

it is really that simple, that is why your code won't compile.

BulldogLowell:
You have to define it, or you have to include a library that defines it.

The single exception to that are the "native" functions included in(or by) the Arduino.h header automatically included during compilation... functions like digitalRead() and pinMode() for example.

it is really that simple, that is why your code won't compile.

Hi i found out that AccelerationReading is a predefined structure for me to use, it is already declared in the hardware itself. i just have to call the function out and do not need to include the library.

Coming back to the problem, if i were to remove the line that declare last_x, everything would work fine. the problem is the last_x variable which i want it to store the previous value.

Try this:

void loop()
{
static AccelerationReading reading;
static int16_t x_last;
static int16_t x;

x_last = x;
reading = Bean.getAcceleration(); 
x = abs(reading.xAxis/2);
int16_t y = abs(reading.yAxis/2);
int16_t z = (reading.zAxis/2);

wildbill:
Try this:

void loop()

{
static AccelerationReading reading;
static int16_t x_last;
static int16_t x;

x_last = x;
reading = Bean.getAcceleration();
x = abs(reading.xAxis/2);
int16_t y = abs(reading.yAxis/2);
int16_t z = (reading.zAxis/2);

wow it worked! but i dont quite understand the logic why it works!

i actually tried to declare x_last as a global static variable but still it wouldnt work. why would declaring the function as static work?