Pages: [1]   Go Down
Author Topic: Why am I getting negative numbers?  (Read 606 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
I void warranties.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is my first Arduino project and I've never programmed before so some help would be much appreciated...

I'm reading three AC voltages and trying to average them out. The code below should only return positive numbers as I have used the ABS command, but I'm still getting negative numbers in the mix. What am I doing wrong?

Code:
const int numReadings = 50;
int bluetotal = 0;                  // the running total
int redtotal = 0;                  // the running total
int greentotal = 0;                  // the running total

int red = A0;
int green = A1;
int blue = A2;
void setup()
{
  // initialize serial communication with computer:
  Serial.begin(9600);                   
 
}

void loop() {
  // Take average of red blue and green                   
for (int count = 0; count < numReadings; count++) {
  bluetotal += abs(analogRead(blue));
  greentotal += abs(analogRead(green));
  redtotal += abs(analogRead(red));
}
 
  // send it to the computer as ASCII digits
  Serial.print(bluetotal/numReadings);   
   Serial.print("        "); 
  Serial.print(redtotal/numReadings);
   Serial.print("        "); 
  Serial.println(greentotal/numReadings);
 
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 216
Posts: 12556
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to use the long type for your redtotal/greentotal/bluetotal variables, they are overflowing.

The output of analogRead is in the range 0..1023 inclusive and cannot be negative anyway.
Logged

[ I won't respond to messages, use the forum please ]

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You might want to pay attention to this part of the abs() reference page (http://arduino.cc/en/Reference/Abs):
"Because of the way the abs() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results."

I believe analogRead() would qualify as a function...
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 9
I void warranties.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the quick response, Can you explain what you mean by Long Type?
I still find it unusual that I am getting negative numbers in the output, especially if the readings are all positive number.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
I void warranties.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys, Figured it out. Moved the Long initialiser into the main loop as well and am getting correct readings now!
Logged

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Type int can hold values of up to between -32767 and 32768 you are adding 50 values of up to 1023 which can overrun 32768 and push the value into the negative range of the variable. If you read the reference to the arduino language it explains what 'long' type is.  It can hold a much larger number before it overflows in simple terms.  You could get away with 'unsigned int' which cannot give negative values and doesn't overflow until it gets past 65535.
Logged


Pages: [1]   Go Up
Jump to: