user55998: I have an Adafruit ADS1115 analog-to-digital converter, configured in single ended mode. I want to average over 3 sensor readings to increase the reading stability.
  //initialize variables
  int16_t rawData, ambient;
  int i=0;

  // start loop
  while(i<3) {
      rawData = ads.readADC_SingleEnded(0); //read from ADC
      // divide sensor reading by three
      // (doing so after summation will lead to an overflow)
      rawData=rawData/3;                    
      Serial.println(rawData); // print data to serial for debugging (OK)
      ambient+=rawData;
      i+=1;
      delay(100);
      Serial.println("I LIVVVVEEE"); // print text to serial for debugging (OK)
  }

  // Print signal (NOT OK)
  Serial.print("Ambient is ");
  Serial.println(ambient);

The "rawData" is printed correctly on the serial monitor. However, the value for "ambient" can be crazy: the resulting output is over an order of magnitude higher. Somehow, the summation doesn't seem to be performed correctly.

user55239: If you are trying to average out three sensor ADC reading first you need to store and add them all then divide it by 3.
 int16_t rawData =0;
 int32_t avg=0,ambient=0;
  int i=0;


 while(i<3) {
      rawData = ads.readADC_SingleEnded(0); //read from ADC
      // divide sensor reading by three
      // (doing so after summation will lead to an overflow)
     Serial.println(rawData); // print data to serial for debugging (OK)
     avg += rawData;
      i++;
      delay(100);
      Serial.println("I LIVVVVEEE"); // print text to serial for debugging (OK)
  }
  abmient = avg /3;
  avg = 0;
  // Print signal (NOT OK)
  Serial.print("Ambient is ");
  Serial.println(ambient);


user7508: You forgot to initialize ambient to zero, so it got whatever happened to be in that place of memory when it got instantiated. Some unpredictable, seemingly random data.

55998: <p>I have an <a href="https://learn.adafruit.com/adafruit-4-channel-adc-breakouts/arduino-code" rel="nofollow noreferrer">Adafruit ADS1115 analog-to-digital converter</a>, configured in single ended mode. I want to average over 3 sensor readings to increase the reading stability.</p>

<pre><code>  //initialize variables
  int16_t rawData, ambient;
  int i=0;

  // start loop
  while(i&lt;3) {
      rawData = ads.readADC_SingleEnded(0); //read from ADC
      // divide sensor reading by three
      // (doing so after summation will lead to an overflow)
      rawData=rawData/3;                    
      Serial.println(rawData); // print data to serial for debugging (OK)
      ambient+=rawData;
      i+=1;
      delay(100);
      Serial.println("I LIVVVVEEE"); // print text to serial for debugging (OK)
  }

  // Print signal (NOT OK)
  Serial.print("Ambient is ");
  Serial.println(ambient);
</code></pre>

<p>The "rawData" is printed correctly on the serial monitor. However, the value for "ambient" can be crazy: the resulting output is over an order of magnitude higher. Somehow, the summation doesn't seem to be performed correctly.</p>
 55239: <p>If you are trying to average out three sensor ADC reading first you need to store and add them all then divide it by 3.</p>

<pre><code> int16_t rawData =0;
 int32_t avg=0,ambient=0;
  int i=0;


 while(i&lt;3) {
      rawData = ads.readADC_SingleEnded(0); //read from ADC
      // divide sensor reading by three
      // (doing so after summation will lead to an overflow)
     Serial.println(rawData); // print data to serial for debugging (OK)
     avg += rawData;
      i++;
      delay(100);
      Serial.println("I LIVVVVEEE"); // print text to serial for debugging (OK)
  }
  abmient = avg /3;
  avg = 0;
  // Print signal (NOT OK)
  Serial.print("Ambient is ");
  Serial.println(ambient);
</code></pre>
 7508: <p>You forgot to initialize <code>ambient</code> to zero, so it got whatever happened to be in that place of memory when it got instantiated. Some unpredictable, seemingly random data.</p>
