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 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];
total_1 = total_1 + Pot1[index];

total_2 = total_2 - Pot2[index];
total_2 = total_2 + Pot2[index];

total_3 = total_3 - Pot3[index];
total_3 = total_3 + Pot3[index];

total_4 = total_4 - Pot4[index];
total_4 = total_4 + Pot4[index];

total_5 = total_5 - Pot5[index];
total_5 = total_5 + Pot5[index];

total_6 = total_6 - Pot6[index];
total_6 = total_6 + Pot6[index];
}