Hello everyone,
In my project, I am taking the analog readings of photosensitive sensors. I want to run a starting function once in the void setup scope, then I want to loop through conditional statements in the void loop scope. Therefore, I have defined the variables, their types, and the analogRead statements to them all in the global scope of the code. My question is: will this guarantee that I will get sensor data continuously or just once?
Here's some of the code below:
int LightSensor1 = analogRead(A1);
int LightSensor2 = analogRead(A2);
void setup(){
StartPosition();
}
void loop(){
if (LightSensor1 >= 100 && LightSensor1 >= 300){
//some code }
else if {
//some code}
}
void StartPosition(){
//some code }
Thanks a lot for your answer.
Will this also work for using the data for the function called in the setup scope (StartPosition())?
Also, another question since you mentioned it, how can I calibrate these sensors?
will this guarantee that I will get sensor data continuously or just once?
This question hints at the fact that you think it is possible that the global variables will be updated automatically throughout the program without the need to do the analogRead() again but that, of course, is not the case. If you want the global variables to be updated then analogRead() has to be used at that time.
Since you have to call analogRead() each time you want the latest value it is generally more appropriate to store the value in a local variable than a global variable.