Hi,
Im having some problems trying to get the output of my formula to print onto the screen.
The value that comes up is 0.
The formula is the bottom line of the code.
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
String msg1 = "Enter Red Value (0-255): ";
String msg2 = "Enter Green Value (0-255): ";
String msg3 = "Enter Blue Value(0-255): ";
int tR,tRa;
int tG,tGa;
int tB,tBa;
int formulaR;
int formulaG;
int forulaB;
void setup()
{
Serial.begin(9600);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,HIGH);
digitalWrite(bluePin,HIGH);
}
void loop()
{
Serial.print(msg1);
while(Serial.available()==0)
{
}
tR = Serial.parseInt();
Serial.println(tR);
Serial.print(msg2);
while(Serial.available()==0)
{
}
tG = Serial.parseInt();
Serial.println(tG);
Serial.print(msg3);
while(Serial.available()==0)
{
}
tB = Serial.parseInt();
Serial.println(tB);
tRa = 255 - tR;
tGa = 255 - tG;
tBa = 255 - tB;
formulaR = (tRa / 255) * 8000;
Serial.println(formulaR);
}
Your variables are all int data type so it is doing integer math. tRa/255 is going to be less than 0, so the result is 0 because integer math rounds down to the nearest integer. 0 * 8000 is 0.
Integer math is preferred over floating point math on 8 bit Arduinos when speed is a concern. 8 bit Arduinos do not have floating point math units so do floating point math slower than integer math.