Getting new value of variable throught each loop

Hello guys,

I am new to advanced category of arduino (did LED blink only in past). I am programming something and I am kinda stuck in between about the logic.

I want to use this formula, W0 = (Wa + Wb)/(1-(Wf/W0)-(We/W0);

now variable W0 on left and right hand side of the equation is same. The way this function works is that user enters initial value of W0 and putting It in equation, it gives new value and loop continues, by keep updating with new value it comes to convergence after some some 30-40 iteration.

My question is, while putting (initial guess) float W0=1; and then running it through equation gives new value in void loop(), but when it comes to 1st line again W0 will be converted to 1. I don't want that!

please guide me guys!

Having made LEDs blink does not mean you are ready for advanced Arduino projects. Far from it.

Things to do once go in setup(), so just put the initial value into the variable there.

Maybe to reset for another run, a button would be checked in the loop() and a new initial value solicited from the user.

a7

1 Like

For help, post your code, using code tags.

1 Like

This is an error in your code which you have not shared. We can't help you fix code we can't see.

1 Like

yes I did that, I put float W0=1; in void setup, and eqution in void loop, but then it gives error "W0 was not declared in this scope"

Stop wasting time and post your code.

Ensure that you use code tags!

1 Like

actually I am using different variables and equations, let me refine and send it then. Thanks!

okay

void setup() {

float B0=1;
float B1=0;
float B2=0;
float B3=0;


}

void loop() {

  float X0[4]={B0,B1,B2,B3}

float w1= 10;
float w2= 3;
float w3= 5;

float dt = 0.002;   

float A[4][4]= {{1,(-w1*dt)/2,(-w2*dt)/2,(-w3*dt)/2},
                 {(w1*dt)/2,1,(w3*dt)/2,(-w2*dt)/2},
                 {(w2*dt)/2,(-w3/2*dt),1,(w1*dt)/2},
                 {(w3*dt)/2,(w2*dt)/2,(-w1*dt)/2,1}};


 float c= X0[1]*A[0][0];

}

Basically code is far from final version, basically I am learning kalman filter, here equation for 'c' is just one of many equations which eventually give new values for X0[4] variable, along with some new data

1 Like
In file included from C:\Users\chinm\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino/Arduino.h:32:0,
                 from C:\Users\chinm\AppData\Local\Temp\arduino\sketches\E511F3C1C59241FE36B5CB662F6076FA\sketch\sketch_nov3d.ino.cpp:1:
C:\Users\chinm\AppData\Local\Temp\.arduinoIDE-unsaved2024103-26608-io1en.b01gi5\sketch_nov3d\sketch_nov3d.ino: In function 'void setup()':
C:\Users\chinm\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino/binary.h:23:12: error: expected unqualified-id before numeric constant
 #define B0 0
            ^
C:\Users\chinm\AppData\Local\Temp\.arduinoIDE-unsaved2024103-26608-io1en.b01gi5\sketch_nov3d\sketch_nov3d.ino:5:7: note: in expansion of macro 'B0'
 float B0=1;
       ^~
C:\Users\chinm\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino/binary.h:31:12: error: expected unqualified-id before numeric constant
 #define B1 1
            ^
C:\Users\chinm\AppData\Local\Temp\.arduinoIDE-unsaved2024103-26608-io1en.b01gi5\sketch_nov3d\sketch_nov3d.ino:6:7: note: in expansion of macro 'B1'
 float B1=0;
       ^~
C:\Users\chinm\AppData\Local\Temp\.arduinoIDE-unsaved2024103-26608-io1en.b01gi5\sketch_nov3d\sketch_nov3d.ino: In function 'void loop()':
C:\Users\chinm\AppData\Local\Temp\.arduinoIDE-unsaved2024103-26608-io1en.b01gi5\sketch_nov3d\sketch_nov3d.ino:15:22: error: 'B2' was not declared in this scope
   float X0[4]={B0,B1,B2,B3}
                      ^~
C:\Users\chinm\AppData\Local\Temp\.arduinoIDE-unsaved2024103-26608-io1en.b01gi5\sketch_nov3d\sketch_nov3d.ino:15:22: note: suggested alternative: 'A2'
   float X0[4]={B0,B1,B2,B3}
                      ^~
                      A2
C:\Users\chinm\AppData\Local\Temp\.arduinoIDE-unsaved2024103-26608-io1en.b01gi5\sketch_nov3d\sketch_nov3d.ino:15:25: error: 'B3' was not declared in this scope
   float X0[4]={B0,B1,B2,B3}
                         ^~
C:\Users\chinm\AppData\Local\Temp\.arduinoIDE-unsaved2024103-26608-io1en.b01gi5\sketch_nov3d\sketch_nov3d.ino:15:25: note: suggested alternative: 'A3'
   float X0[4]={B0,B1,B2,B3}
                         ^~
                         A3
C:\Users\chinm\AppData\Local\Temp\.arduinoIDE-unsaved2024103-26608-io1en.b01gi5\sketch_nov3d\sketch_nov3d.ino:23:22: error: 'w1' was not declared in this scope
 float A[4][4]= {{1,(-w1*dt)/2,(-w2*dt)/2,(-w3*dt)/2},
                      ^~
C:\Users\chinm\AppData\Local\Temp\.arduinoIDE-unsaved2024103-26608-io1en.b01gi5\sketch_nov3d\sketch_nov3d.ino:23:22: note: suggested alternative: 'w3'
 float A[4][4]= {{1,(-w1*dt)/2,(-w2*dt)/2,(-w3*dt)/2},
                      ^~
                      w3

exit status 1

Compilation error: 'B2' was not declared in this scope

the error I am getting

1 Like

The variables you are trying to declare and define (B0, B1, etc.) conflict with predefined symbols in the Arduino IDE. Use other names.

To avoid "starting over" every time the loop function is executed, declare variables as static.

void loop() {
static float myB1 = 0.5;
...
2 Likes

Besides the predefined Bxxx variables that @jremington identified, If you intend to share variables between setup() and loop(), they need to have a wider scope. Otherwise the will be discarded when setup() completes.

Declare them as globals before you define setup().

1 Like

As @jremington mentioned, unfortunately the identifiers B0 and B1 are already defined for another purpose in Arduino language, so you can't use them as variable names.

B2 and B3 are available to use, which is why you may notice that the error messages about them are different to the error messages about B0 and B1.

However, in this code, B2 and B3 are local to setup(). As soon as setup finishes executing, B2 & B3 cease to exist. That's why they cannot be seen or used in loop().

If you want your variables to be used in more than one function, you need to make them global variables by defining them outside of any function.

1 Like

yes thanks guys. @jremington @DaveX . your solution works. I have one final question though, if I set static global variable, will it be called once (1st time) only? and not the second time onwards loop continues?

I have tried your suggestions and error got vanished.

static float i=1;
static float j=0;
static float k=0;
static float l=0;

void setup() {




}

void loop() {

  float X0[4]={i,j,k,l};

float w1= 10;
float w2= 3;
float w3= 5;

float dt = 0.002;   

float A[4][4]= {{1,(-w1*dt)/2,(-w2*dt)/2,(-w3*dt)/2},
                 {(w1*dt)/2,1,(w3*dt)/2,(-w2*dt)/2},
                 {(w2*dt)/2,(-w3/2*dt),1,(w1*dt)/2},
                 {(w3*dt)/2,(w2*dt)/2,(-w1*dt)/2,1}};


 float c= X0[1]*A[0][0];

}

The static keyword is not necessary with global variables, they are always static.

Variables are not called. Only functions are called. So I'm not clear what you are asking here.

2 Likes

by calling here I mean I need new values of the variable each time loop runs ( I will evantully get new values of i,j,k and l by some equations later in code) and not the initial values I set as global variables (i.e, i,j,k,l as 1,0,0,0) at the beginning.

I am sorry for being so naive.

Yes, global variables will retain their updated values, so next time loop() runs, it will see the updated values.

Their values will only return to the original values when the Arduino is reset, or you upload new code, or when the Arduino is powered up.

1 Like

Perfact, Thank you so much I got answers to all my questions. I didn't expected to get solution this fast. you guys are gem. Keep up the good work! and again thank you so much guys!

No. In a program with multiple source files, goofball global variables with the static qualifier are visible only in the file in which they appear.

So here, static is harmless and unnecessary.

a7

1 Like