Help with non functioning if statements

Hello everyone

i am currently working on a project where i am using 3 pots to control the brightness of the individual colours on an RGB strip (its an analog one BTW) and it then sends a serial debug messages when the values change but for some reason it isnt paying any attention to the if statements and is constantly outputting the serial debug messages so fast that i cant read them.

i have included the entire program.

int rval = 0;
int rvalold = 0;
int gval = 0;
int gvalold = 0;
int bval = 0;
int bvalold = 0;
int red = 9;
int green = 10;
int blue = 11;
int rpot = 0;
int gpot = 1;
int bpot = 2;

void setup() 
{
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(blue,OUTPUT);
  pinMode(rpot,INPUT_PULLUP);
  pinMode(gpot,INPUT_PULLUP);
  pinMode(bpot,INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() 
{
 rval = analogRead(rpot);
 gval = analogRead(gpot);
 bval = analogRead(bpot);
 if (rval != rvalold)
 {
   Serial.print(" R = ");
   Serial.print(rval);
   Serial.print("|");
   rvalold = rval;
   Serial.print(rvalold);
 }
 if (gval != gvalold)
 {
   Serial.print(" G = ");
   Serial.print(gval);
   Serial.print("|");
   gvalold = gval;
   Serial.print(gvalold);
 }
 if (bval != bvalold)
 {
   Serial.print(" B = ");
   Serial.print(bval);
   Serial.print("|");
   bvalold = bval;
   Serial.print(bvalold);
 }
 analogWrite(red,rval);
 analogWrite(green,gval);
 analogWrite(blue,bval);
}

but for some reason it isnt paying any attention to the if statements

I don't understand - the serial prints can only occur if the "if" statements evaluate to true, so they're clearly NOT "non-functioning". There'll nearly always be some noise which will cause jitter on an analogue input, so it's not surprising the values differ from iteration to iteration.

int rvalold;
int gvalold;
int bvalold;
const byte red   = 9;
const byte green = 10;
const byte blue  = 11;
const byte rpot = 0;
const byte gpot = 1;
const byte bpot = 2;

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
 int rval = analogRead(rpot);
 int gval = analogRead(gpot);
 int bval = analogRead(bpot);
 if (rval != rvalold)
 {
   Serial.print(" R = ");
   Serial.print(rval);
   Serial.print("|");
   rvalold = rval;
   Serial.print(rvalold);
 }
 if (gval != gvalold)
 {
   Serial.print(" G = ");
   Serial.print(gval);
   Serial.print("|");
   gvalold = gval;
   Serial.print(gvalold);
 }
 if (bval != bvalold)
 {
   Serial.print(" B = ");
   Serial.print(bval);
   Serial.print("|");
   bvalold = bval;
   Serial.print(bvalold);
 }
 analogWrite(red,rval);
 analogWrite(green,gval);
 analogWrite(blue,bval);
}

... so fast that i cant read them.

Nor copy and paste them into a forum message, I gather.

Scarface_the_fox:
constantly outputting the serial debug messages so fast that i cant read them.

Did you try sticking a delay at the very bottom?

}
 analogWrite(red,rval);
 analogWrite(green,gval);
 analogWrite(blue,bval);

delay(5000); // or whatever you like <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}

thanks for fast replys

i totally forgot about jitter from a pot its been a long time since i worked with pots connected to microcontrollers.

well i fixed it by just adding an averaging read instead of constantly reading the pot.

well i fixed it by just adding an averaging read instead of constantly reading the pot.

Pot always gave me the jitters, too. 8)

What's the jitter got to do with the original question about the ifs not working?

AWOL's reply leaping straight to the jitter thing makes me wonder if the OP had a second question that was deleted?

He thought the "if" wasn't working because it kept outputting stuff. In the OP's opinion, if he didn't touch the pot, there should have been no output. However because of the jitter, there was.

Ah ok, thanks Nick, I get it. And if he'd put a delay() in he would have seen the values changing, pot untouched.