Negative Reading On Analog Input

Hello, I’m currently working on a project that involves the an Arduino Mega2560 gathering a voltage from six (6) 10KOhm potentiometers. I’ve encountered a rather strange issue that I just can’t seem to find a reason for.

The issue is… after the analog readings reach above a certain value (in this case around 655 out of the 0-1023 range), the value is read as negative, which is throwing off my operation. I understand that the arduino analog inputs can only read from 0 to +5V. So I double checked that the input voltages were positive (they indeed are and approximately the expected value).

So the arduino is receiving a positive voltage. So, I started checking my code. I incorporated a smoothing function into my project to average the incoming voltage to in a way account for noise within the system. Commenting out this function produces the right positive voltage, so it appears to be the smoothing code that is changing the sign of this value and I can’t figure out why.

The relevant code is below and I can’t determine why it would respond this way. Any tips or suggestions would be greatly appreciated. Thanks.

const int numReadings = 50;

int Pot1[numReadings];
int Pot2[numReadings];
int Pot3[numReadings];
int Pot4[numReadings];
int Pot5[numReadings];
int Pot6[numReadings];

int total_1 = 0;
int total_2 = 0;
int total_3 = 0;
int total_4 = 0;
int total_5 = 0;
int total_6 = 0;

void setup()
{
for (int i = 0; i < numReadings; i++)
  {
    Pot1[i] = 0;
    Pot2[i] = 0;
    Pot3[i] = 0;
    Pot4[i] = 0;
    Pot5[i] = 0;
    Pot6[i] = 0;
  }
}

void loop()
{
  SMOOTH();
}

void SMOOTH()
{
  for (int index = 0; index < numReadings; index++)
  {
    total_1 = total_1 - Pot1[index];
    Pot1[index] = analogRead(0);
    total_1 = total_1 + Pot1[index];      
        
    total_2 = total_2 - Pot2[index];
    Pot2[index] = analogRead(1);
    total_2 = total_2 + Pot2[index];      
    
    total_3 = total_3 - Pot3[index];
    Pot3[index] = analogRead(2);
    total_3 = total_3 + Pot3[index];      
    
    total_4 = total_4 - Pot4[index];
    Pot4[index] = analogRead(3);
    total_4 = total_4 + Pot4[index];      
    
    total_5 = total_5 - Pot5[index];
    Pot5[index] = analogRead(4);
    total_5 = total_5 + Pot5[index];      
    
    total_6 = total_6 - Pot6[index];
    Pot6[index] = analogRead(5);
    total_6 = total_6 + Pot6[index];      
  }
  POTVAL[0] = total_1/numReadings;
  POTVAL[1] = total_2/numReadings;
  POTVAL[2] = total_3/numReadings;
  POTVAL[3] = total_4/numReadings;
  POTVAL[4] = total_5/numReadings;
  POTVAL[5] = total_6/numReadings;
}

Your total variable is a signed integer. How big of a positive number can a signed int hold? Total probably should be an unsigned long. See rollover.

groundfungus:
Your total variable is a signed integer. How big of a positive number can a signed int hold? Total probably should be an unsigned long. See rollover.

Well that solved it! Dang, I should have realized that. Haha. Thanks for the advice. I appreciate it.

Easy mistake. Guess why I spotted it so quickly?