# Need Help with Integers! Hard to explain!

I need to take an integer once, off of an analog read, then stop and remember that number. So I put the integer in the setup of the program. The thing is that I need the number in the loop of the program and when I type in the name of the integer all I am getting in the loop is a zero. Is there a way that I can get that integer in to the loop of the program.

Hard to explain!

So why not just post the code?

Ignore all the other stuff, the problem is in the integers and floating the value to get my final number.

``````#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int initialvalue;
void setup(){
Serial.begin(9600);
lcd.begin(20, 4);
pinMode(5, OUTPUT);    //this is the button, figure out setup for this
pinMode(4, OUTPUT);    //this is the green led
digitalWrite(4, LOW);
pinMode(3, OUTPUT);    //this is the yellow led
digitalWrite(3, LOW);
pinMode(2, OUTPUT);    //this is the red led
digitalWrite(2, LOW);
}

void loop()
{
float value1 = initialvalue - currentvalue ;
float value2 = value1 / initialvalue ;
float value3 = value2 * 100.0 ;
delay(500);
//  Serial.println(value2);

if(value3 >= 225){
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
digitalWrite(2, LOW);

}else if(value3 <= 224 && value3 >= 217){
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
digitalWrite(2, LOW);

}else if(value3 <= 216 && value3 >=101){
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, HIGH);

}else if(value3 <= 100){
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, LOW);
}

delay(5);
lcd.setCursor(0, 0);
lcd.print("ACSavers Air Filter");
lcd.setCursor(0, 1);
}
``````
``````#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int initialvalue;
void setup(){
Serial.begin(9600);
lcd.begin(20, 4);
pinMode(5, OUTPUT);    //this is the button, figure out setup for this
pinMode(4, OUTPUT);    //this is the green led
digitalWrite(4, LOW);
pinMode(3, OUTPUT);    //this is the yellow led
digitalWrite(3, LOW);
pinMode(2, OUTPUT);    //this is the red led
digitalWrite(2, LOW);
}

void loop()
{
float value1 = initialvalue - currentvalue ;
float value2 = value1 / initialvalue ;
float value3 = value2 * 100.0 ;
delay(500);
//  Serial.println(value2);

if(value3 >= 225){
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
digitalWrite(2, LOW);

}else if(value3 <= 224 && value3 >= 217){
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
digitalWrite(2, LOW);

}else if(value3 <= 216 && value3 >=101){
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, HIGH);

}else if(value3 <= 100){
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, LOW);
}

delay(5);
lcd.setCursor(0, 0);
lcd.print("ACSavers Air Filter");
lcd.setCursor(0, 1);
}
``````

The concept is called “scope”

When a variable is declared it will shadow the declarations of other variables of the
same name in outer scopes or the global scope. Which is what you did. The global
variable never not touched.

A declaration is not the same as an assignment.

Arduino uses lots of manipulations in libraries to depart from pure C++ in language;however variable scope stays true to the c++ language:

http://en.wikibooks.org/wiki/C%2B%2B_Programming/Scope/Examples

Once you "get it", you have it forever!

I'm not sure it is obvious from the replies so far. The problem is that you have

``````int initialvalue;
``````

before setup() and again within setup.

When you preface a variable with "int" (or any other variable type) the compiler thinks you want to create a new variable.

A variable created outside of all functions is a global variable and can be seen by all of your code

When you create a variable within a function (in this case inside setup) the variable is a local variable and is only visible inside the function.

When you create a variable inside a function which has the same name as a global variable the compiler will use the local variable inside the function in preference to the local variable.

If you just want to use an existing variable you just use it without putting "int" before it.

...R