Why am I getting negative numbers?


this first arduino project , i've never programmed before appreciated...

i'm reading 3 ac voltages , trying average them out. code below should return positive numbers have used abs command, i'm still getting negative numbers in mix. doing wrong?

code: [select]

const int numreadings = 50;
int bluetotal = 0;                  // running total
int redtotal = 0;                  // running total
int greentotal = 0;                  // running total

int red = a0;
int green = a1;
int blue = a2;
void setup()
{
  // initialize serial communication computer:
  serial.begin(9600);                   

}

void loop() {
  // take average of red blue , green                   
for (int count = 0; count < numreadings; count++) {
  bluetotal += abs(analogread(blue));
  greentotal += abs(analogread(green));
  redtotal += abs(analogread(red));
}
 
  // send computer ascii digits
  serial.print(bluetotal/numreadings);   
   serial.print("        "); 
  serial.print(redtotal/numreadings);
   serial.print("        "); 
  serial.println(greentotal/numreadings);
 
}

you need use long type redtotal/greentotal/bluetotal variables, overflowing.

the output of analogread in range 0..1023 inclusive , cannot negative anyway.


Arduino Forum > Using Arduino > Programming Questions > Why am I getting negative numbers?


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial