# Calculation always zero?

Here is a little Programm i am using for two Analog inputs. Later i want to calculate the difference. But for some reason it's always 0. That drives me crazy

It's a really short and easy code, maybe someone could help me figure out what i did wrong

``````/*
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {
// read the input on analog pin 0/1:
long Dehnung = 1;
long Ua = 0;
long Ub = 0;
// print out the value you read:
Serial.println("Ua: ");
Serial.println(sensorValue1);
Serial.println("Ub: ");
Serial.println(sensorValue2);
delay(1000);        // delay in between reads for stability
Dehnung = Ua - Ub;
Serial.println("Dehnung: ");
Serial.println(Dehnung);
}
``````

Your sensor values aren't used to calculate anything, they are just printed via serial.

``````  long Dehnung = 1;
long Ua = 0;
long Ub = 0;

//...

Dehnung = Ua - Ub;
``````

0 - 0 = 0

I’m a newbie too, so I hope I’ve got this right

See how you go with this code…your version was subtracting 0 from 0 every time (the variables Ua and Ub are never set after they are declared as 0), so the answer was always going to be zero.

``````/*
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.
*/

long Ua = 0;
long Ub = 0;
long result=0;

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {
// read the input on analog pin 0/1:

delay(10);
delay(10);
result=Ua-Ub;

// print out the value you read:
Serial.print("Ua: ");
Serial.println(Ua);
Serial.print("Ub: ");
Serial.println(Ub);
Serial.print("Dehnung: ");
Serial.println(result);
}
``````

While true for uninitialized GLOBAL variables it's a bad assumption that uninitialized LOCAL variables will be zero. Uninitialized LOCAL variables will contain whatever garbage is on the stack.

Something a little more easy to follow -

``````const unsigned long ONE_SECOND  = 1000UL;

void loop()
{
Serial.print("Ua: ");       Serial.println(Ua);

Serial.print("Ub: ");       Serial.println(Ub);

delay(ONE_SECOND);

int Dehnung = Ua - Ub;
Serial.print("Dehnung: ");  Serial.println(Dehnung);
}

void setup()
{
Serial.begin(9600);
}
``````

You better not code when its more than 35 degrees :cold_sweat:

This is somewhat embarrassing. No i clearly see the mistake and cannot understand how i could made it.

Thanks guys :)