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

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  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:
  int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);
  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 :slight_smile:

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.

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  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:

  Ua = analogRead(A0);
  delay(10);
  Ub = analogRead(A1);
  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()
{
    int Ua      = analogRead(A0);
    Serial.print("Ua: ");       Serial.println(Ua);

    int Ub      = analogRead(A1);
    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 :)