I am using a gas sensor and want to get a value during the setup function and use it throughout the loop function as a constant, without having to repeatedly get the same value. So, it would be something like the below :
void setup() {
pinMode(1, INPUT);
int constant = analogRead(1);
}
void loop() {
int changing = analogRead(1); //This will be a different value than the constant
int ratio = changing / constant; //I should be able to access the constant variable here
}
Currently, this does not compile and i understand the scope of the variables, but is there a way I can use the value of the "constant" variable in loop()? Would it be something like putting const int constant = analogRead(1); in loop()? Any help would be much appreciated!
int constant;
void setup() {
pinMode(1, INPUT);
constant = analogRead(1);
}
void loop() {
int changing = analogRead(1); //This will be a different value than the constant
int ratio = changing / constant; //I should be able to access the constant variable here
}
void loop()
{
// Constant will be initialized to the value of analogRead(1) the first time loop
// runs and will hold that value forever
static int constant = analogRead(1);
}
I've removed the unhelpful and possibly misleading posts about 'the float command'. Also removed the associated replies. I hope that what remains is useful to @xxrw