Function is returning large and weird values!!

hey arduino noob here working on my first non-tutorial project!
its an eight button RGB LED controller. Each button has 3- 8 bit (0 thru 255) values associates with it that sets 3 different variables to be the "direction/destination" the colors will "go to." Every time it goes through the loop it checks to see if the values the RGB LED are currently at are the same as the "destination" values, and if one or more of the values are different, add or subtract to get closer to the destination by one(++ or --).

At the end of this there is a delayMicroSeconds(elTiempo)
elTiempo is equal to 100000 divided by "steppage." steppage is the largestDiff (largest difference) function set at the time the button is pressed. This is to ensure that no matter how far the LED RGB values has to go from one button to another, the fading is always 100000 microseconds/ 1/10th of a second.

Here is the code for largestDiff:

int largestDiff(int prRed, int prGreen, int prBlue, int nwRed, int nwGreen, int nwBlue){
  int rr = prRed-nwRed;
  Serial.println(rr);
  int rojo;
  rojo = abs(rr);
  Serial.println(rojo);
  int gg = prGreen-nwGreen;
  int verde = abs(gg);
  int bb = prBlue-nwBlue;
  int azul = abs(bb);
  if (rojo >= verde && rojo >= azul){
    Serial.println("*******************************************");
    Serial.print("LargestDiff is: ");
    Serial.print(rojo);
    return rojo;
  }
  if (verde >= rojo && verde >= azul){
    Serial.println("*******************************************");
    Serial.print("LargestDiff is: ");
    Serial.print(verde);
    return verde;
  }
  if (azul >= rojo && azul >= verde){
    Serial.println("*******************************************");
    Serial.print("LargestDiff is: ");
    Serial.print(azul);
    return rojo;
  }
}

The prColor arguments are the color values the LED starts at (the destination values from the last time it was pressed), and the nwColor are the new "destination" values. It subtracts each value from its corresponding value, does an abs for each and returns the largest one.

as you can see there are Serial.prints which i was using to troubleshoot. I print the rr, which is the negative or positive difference which prints correctly. I then print rojo, which is abs(rr) which also prints correctly (both rr and rojo is a number between 0 and 255)
But once inside the if statements that actually return values, I print rojo again and get weird values this time. (a large number, like 255039 or something.)

Any ideas??

Please post your entire sketch, not just part of it.

Please post your entire sketch,not just part of it, because an integer can not be 255039. You print two numbers after each other.

here it is:

const int one = 12;
const int two = 2;
const int three = 3;
const int four = 4;
const int five = 5;
const int six = 6;
const int seven = 7;
const int eight = 8;

const int kRed = 9;
const int kBlue = 10;
const int kGreen = 11;
const int kPot = A0;

int btnStateOne = 0;
int btnStateTwo = 0;
int btnStateThree = 0;
int btnStateFour = 0;
int btnStateFive = 0;
int btnStateSix = 0;
int btnStateSeven = 0;
int btnStateEight = 0;

int prvStateOne = 0;
int prvStateTwo = 0;
int prvStateThree = 0;
int prvStateFour = 0;
int prvStateFive = 0;
int prvStateSix = 0;
int prvStateSeven = 0;
int prvStateEight = 0;


int spf = analogRead(kPot);
int sped;
int stepto = 0;

int ogRed = 0;
int ogGreen = 0;
int ogBlue = 0;

int prevRed = 0;
int prevGreen = 0;
int prevBlue = 0;

int newRed = 0;
int newGreen = 0;
int newBlue = 0;

void setColor(int red, int green, int blue){
  int rojo = 255 - red;
  analogWrite(kRed, rojo);
  int verde = 255 - green;
  analogWrite(kGreen, verde);
  int azul = 255 - blue;
  analogWrite(kBlue, azul);
}

int largestDiff(int prRed, int prGreen, int prBlue, int nwRed, int nwGreen, int nwBlue){
  int rr = prRed-nwRed;
  Serial.println(rr);
  int rojo;
  rojo = abs(rr);
  Serial.println(rojo);
  int gg = prGreen-nwGreen;
  int verde = abs(gg);
  int bb = prBlue-nwBlue;
  int azul = abs(bb);
  if (rojo >= verde && rojo >= azul){
    Serial.println("*******************************************");
    Serial.print("LargestDiff is: ");
    Serial.print(rojo);
    return rojo;
  }
  if (verde >= rojo && verde >= azul){
    Serial.println("*******************************************");
    Serial.print("LargestDiff is: ");
    Serial.print(verde);
    return verde;
  }
  if (azul >= rojo && azul >= verde){
    Serial.println("*******************************************");
    Serial.print("LargestDiff is: ");
    Serial.print(azul);
    return rojo;
  }
}

void goToColor(int red, int green, int blue, int steppage, int spd){// send this function prevRed/Green/Blue and steppage is the 100,000/largestdiff
  setColor(red, green, blue);
  int neoRed = red;
  int neoGreen = green;
  int neoBlue = blue;
  int elTiempo = steppage;
  if (red != newRed){
    if (red - newRed < 0){
      neoRed = red + 1;
    }
    if (red - newRed > 0){
      neoRed = red - 1;
    }
    prevRed = neoRed;
  }
  //asdfjalksdfjalksdf
  if (green != newGreen){
    if (green - newGreen < 0){
      neoGreen = green + 1;
    }
    if (green - newGreen > 0){
      neoGreen = green - 1;
    }
    prevGreen = neoGreen;
  }
  if (blue != newBlue){
    if (blue - newBlue < 0){
      neoBlue = blue + 1;
    }
    if (blue - newBlue > 0){
      neoBlue = blue - 1;
    }
    prevBlue = neoBlue;
  }
  delayMicroseconds(elTiempo);
}


void setup()
{
  Serial.begin(9600);
  pinMode(one, INPUT);
  digitalWrite(one, HIGH);
  pinMode(two, INPUT);
  digitalWrite(two, HIGH);
  pinMode(three, INPUT);
  digitalWrite(three, HIGH);
  pinMode(four, INPUT);
  digitalWrite(four, HIGH);
  pinMode(five, INPUT);
  digitalWrite(five, HIGH);
  pinMode(six, INPUT);
  digitalWrite(six, HIGH);
  pinMode(seven, INPUT);
  digitalWrite(seven, HIGH);
  pinMode(eight, INPUT);
  digitalWrite(eight, HIGH);
  pinMode(kRed, OUTPUT);
  pinMode(kGreen, OUTPUT);
  pinMode(kBlue, OUTPUT);
}

void loop()
{
  if (digitalRead(one) == HIGH){
    btnStateOne = 0;
  }
  if (digitalRead(two) == HIGH){
    btnStateTwo = 0;
  }
  if (digitalRead(three) == HIGH){
    btnStateThree = 0;
  }  
  if (digitalRead(four) == HIGH){
    btnStateFour = 0;
  }
  if (digitalRead(five) == HIGH){
    btnStateFive = 0;
  }
  if (digitalRead(six) == HIGH){
    btnStateSix = 0;
  }
  if (digitalRead(seven) == HIGH){
    btnStateSeven = 0;
  }
  if (digitalRead(eight) == HIGH){
    btnStateEight = 0;
  }
  
  
  spf = analogRead(kPot);
  sped = map(spf, 0, 1023, 0, 40);
  goToColor(prevRed, prevGreen, prevBlue, stepto, sped);
//  stepto = 100000/(largestDiff(prevRed, prevGreen, prevBlue, newRed, newGreen, newBlue));
  if (digitalRead(eight) == LOW){
    prvStateEight = btnStateEight;
    btnStateEight = 1;
    newRed = 0;
    newGreen = 100;
    newBlue = 170;
    if (btnStateEight != prvStateEight && btnStateEight == 1){
      stepto = 100000/(largestDiff(prevRed, prevGreen, prevBlue, newRed, newGreen, newBlue));
    }
    Serial.println(stepto);
  }
  if (digitalRead(seven) == LOW){
    prvStateSeven = btnStateSeven;
    btnStateSeven = 1;
    newRed = 0;
    newGreen = 220;
    newBlue = 50;
    if (btnStateSeven != prvStateSeven && btnStateSeven == 1){
      stepto = 100000/(largestDiff(prevRed, prevGreen, prevBlue, newRed, newGreen, newBlue));
    }
    Serial.println(stepto);  
}
  if (digitalRead(six) == LOW){
    prvStateSix = btnStateSix;
    btnStateSix = 1;
    newRed = 0;
    newGreen = 0;
    newBlue = 255;
    if (btnStateSix != prvStateSix && btnStateSix == 1){
      stepto = 100000/(largestDiff(prevRed, prevGreen, prevBlue, newRed, newGreen, newBlue));
    }
    Serial.println(stepto);  
}
  if (digitalRead(five) == LOW){
    prvStateFive = btnStateFive;
    btnStateFive = 1;
    newRed = 0;
    newGreen = 255;
    newBlue = 0;
    if (btnStateFive != prvStateFive &&btnStateFive == 1){
      stepto = 100000/(largestDiff(prevRed, prevGreen, prevBlue, newRed, newGreen, newBlue));
    }
   Serial.println(stepto);  
 }
  if (digitalRead(four) == LOW){
    prvStateFour = btnStateFour;
    btnStateFour = 1;
    newRed = 70;
    newGreen = 0;
    newBlue = 220;
    if (btnStateFour != prvStateFour && btnStateFour == 1){
      stepto = 100000/(largestDiff(prevRed, prevGreen, prevBlue, newRed, newGreen, newBlue));
    }
    Serial.println(stepto);  
}
  if (digitalRead(three) == LOW){
    prvStateThree = btnStateThree;
    btnStateThree = 1;
    newRed = 100;
    newGreen = 147;
    newBlue = 0;
    if (btnStateThree != prvStateThree && btnStateThree == 1){
      stepto = 100000/(largestDiff(prevRed, prevGreen, prevBlue, newRed, newGreen, newBlue));
    }
    Serial.println(stepto);
 }
  if (digitalRead(two) == LOW){
    prvStateTwo = btnStateTwo;
    btnStateTwo = 1;
    newRed = 145;
    newGreen = 0;
    newBlue = 170;
    if (btnStateTwo != prvStateTwo && btnStateTwo == 1){
      stepto = 100000/(largestDiff(prevRed, prevGreen, prevBlue, newRed, newGreen, newBlue)); 
    }
    Serial.println(stepto);

  }
  if (digitalRead(one) == LOW){
    prvStateOne = btnStateOne;
    btnStateOne = 1;
    newRed = 255;
    newGreen = 0;
    newBlue = 0;
    if (btnStateOne != prvStateOne && btnStateOne == 1){
      stepto = 100000/(largestDiff(prevRed, prevGreen, prevBlue, newRed, newGreen, newBlue));
    }
   Serial.println(stepto);
 }
 delay(1);
}

You print a value within largestDiff() and then print "stepto" right after that on the same line.

It is best to print variables in the same way.
For example print them all like this:

  Serial.print(F("stepto = "));
  Serial.println(stepto);

ahh

Oops...

 if ([color=blue][b]azul[/b][/color] >= rojo && [color=blue][b]azul[/b][/color] >= verde){
    Serial.println("*******************************************");
    Serial.print("LargestDiff is: ");
    Serial.print([color=blue][b]azul[/b][/color]);
    return [color=red][b]rojo[/b][/color];
  }

That function has far more statements than it needs.

  int rr = prRed-nwRed;
  Serial.println(rr);
  int rojo;
  rojo = abs(rr);
  Serial.println(rojo);

should be:

  int rojo = abs(prRed-nwRed);
  Serial.println(rojo);

Cut out half the code for blue and green, too.

Peter_n:
Please post your entire sketch,not just part of it, because an integer can not be 255039. You print two numbers after each other.

Err, 255039 is an integer. :wink:
It cannot be a sixteen bit "int"

AWOL:
Err, 255039 is an integer. :wink:
It cannot be a sixteen bit "int"

Wise *ss :smiling_imp:

chuck.