5 button RGB control still doesn't work?

I got some help, fixed for loops to define my pins. I’ve tested my pinModes and definitions by using my setup section and just digitally writing the different colors on. I’ve even tested all the way through the analogWrite parts by just defining the fadeLevel values as 61. That works, but the if statements for the button functioning are doing literally nothing.

void setup() {
  //set the function of my button and led pins
  for(int ledPins = 3;ledPins < 7;ledPins++){
    pinMode(ledPins, OUTPUT);
  }
  for(int buttonPins = 8;buttonPins < 13; buttonPins++){
  pinMode(buttonPins, INPUT_PULLUP);
}
}
// the loop function runs over and over again forever
void loop() {
//define my buttons by color
int greenButton = digitalRead(8);
int blueButton = digitalRead(9);
int redButton = digitalRead(10);
int upButton = digitalRead(11);
int downButton = digitalRead(12);
//define the RGB components by color
int redLed = 5;
int blueLed = 3;
int greenLed = 6;
//create an array of values for analogWrite functions
int fadeLevel1 = 61;
int fadeLevel2 = 61;
int fadeLevel3 = 61;
//write the fade level to each color of the RGB

analogWrite(redLed, fadeLevel1);
analogWrite(blueLed, fadeLevel2);
analogWrite(greenLed, fadeLevel3);

//set a series of statements to use the buttons
//to raise or lower the duty cycle of the led by color
//using if statements to look for simultaneous presses
//of a color button and the "up" or "down" buttons
//respectively
if(greenButton == LOW && upButton == LOW){
  fadeLevel1 == fadeLevel1 + 5;
  delay(1);
  return;
}
else if(blueButton == LOW && upButton == LOW){
  fadeLevel2 == fadeLevel2 + 5;
  delay(1);
  return;
}
else if(redButton == LOW && upButton == LOW){
  fadeLevel3 == fadeLevel3 + 5;
  delay(1);
  return;
}
else if(greenButton == LOW && downButton == LOW){
  fadeLevel1 == fadeLevel1 - 5;
  delay(1);
  return;
}
else if(blueButton == LOW && downButton == LOW){
  fadeLevel2 == fadeLevel2 - 5;
  delay(1);
  return;
}
else if(redButton == LOW && downButton == LOW){
  fadeLevel3 == fadeLevel3 - 5;
  delay(1);
  return;
}
}

I want a smooth, linear fade. I put in a 1ms delay and only added 5 to the value over a cycle. In any case, I’d appreciate whatever help I can get.

Your fade levels are being reset to 61 at the beginning of every single loop.

And don't make new threads for the same thing.

Hmmm... if I try declaring the fadeLevel integers within the if statements and put the analogWrite functions after the if statements, the program says that my fadeLevel integers aren't declared...

and I'll keep that in mind for the future.

INTP:
Your fade levels are being reset to 61 at the beginning of every single loop.

= don’t put them in loop().

TexArduinoAs:
I got some help, fixed for loops to define my pins. I’ve tested my pinModes and definitions by using my setup section and just digitally writing the different colors on. I’ve even tested all the way through the analogWrite parts by just defining the fadeLevel values as 61. That works, but the if statements for the button functioning are doing literally nothing.

This should be under void setup or elsewhere not in the loop for 1 thing

//define my buttons by color

int greenButton = digitalRead(8);
int blueButton = digitalRead(9);
int redButton = digitalRead(10);
int upButton = digitalRead(11);
int downButton = digitalRead(12);
//define the RGB components by color
int redLed = 5;
int blueLed = 3;
int greenLed = 6;
//create an array of values for analogWrite functions
int fadeLevel1 = 61;
int fadeLevel2 = 61;
int fadeLevel3 = 61;
//write the fade level to each color of the RGB




I want a smooth, linear fade. I put in a 1ms delay and only added 5 to the value over a cycle. In any case, I'd appreciate whatever help I can get.

Well, my buttons work, my lighting works, and the fade function works. I still have a problem though, I want to constrain my values. I added “constrain” functions to my program, but they don’t work and I don’t know why. I have tried putting them in the loop and in the setup, but they don’t constrain the values properly… I can’t find a good tutorial on “constrain,” only “map,” which doesn’t work here.

int fadeLevel1 = 0;
int fadeLevel2 = 0;
int fadeLevel3 = 0;

void setup() {
  //set the function of my button and led pins
  for(int ledPins = 3;ledPins < 7;ledPins++){
    pinMode(ledPins, OUTPUT);
  }
  for(int buttonPins = 8;buttonPins < 13; buttonPins++){
  pinMode(buttonPins, INPUT_PULLUP);
}
//I am fading leds using PWM, so I only want values from 0-255.
constrain(fadeLevel1, 0, 255);
constrain(fadeLevel2, 0, 255);
constrain(fadeLevel3, 0, 255);
//I put in some serial prints so I could see what was happening
//to my values. Serves no purpose in the program except to
//allow me to visualize the numbers being generated.
Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {

//define my buttons by color
int greenButton = digitalRead(8);
int blueButton = digitalRead(9);
int redButton = digitalRead(10);
int upButton = digitalRead(11);
int downButton = digitalRead(12);
//define the RGB components by color
int redLed = 5;
int blueLed = 3;
int greenLed = 6;
//create an array of values for analogWrite functions
//write the fade level to each color of the RGB
analogWrite(redLed, fadeLevel1);
analogWrite(blueLed, fadeLevel2);
analogWrite(greenLed, fadeLevel3);

//set a series of statements to use the buttons
//to raise or lower the duty cycle of the led by color
//using if statements to look for simultaneous presses
//of a color button and the "up" or "down" buttons
//respectively.  The buttons are being held and there are
//delays built in, so debouncing is a non-factor.

if(greenButton == LOW && upButton == LOW){
  fadeLevel1 = fadeLevel1 + 1;
  delay(10);
  }
if(blueButton == LOW && upButton == LOW){
  fadeLevel2 = fadeLevel2 + 1;
  delay(10);
}
if(redButton == LOW && upButton == LOW){
  fadeLevel3 = fadeLevel3 + 1;
  delay(10);
}
if(greenButton == LOW && downButton == LOW){
  fadeLevel1 = fadeLevel1 - 1;
  delay(10);
}
if(blueButton == LOW && downButton == LOW){
  fadeLevel2 = fadeLevel2 - 1;
  delay(10);
}
if(redButton == LOW && downButton == LOW){
  fadeLevel3 = fadeLevel3 - 1;
  delay(10);
}
//And here we print the values so I can see if my constrain
//is working or not. It doesn't.
Serial.print(fadeLevel1);
Serial.print(fadeLevel2);
Serial.print(fadeLevel3);
//I'm not so good with serial comms yet, and that isn't
//the point of this project, so this delay is just to slow it
//so I can read it. I'll remove it and the whole
//serial comm functionality when my constraints work.
delay(200);
}

never seen constrain used in setup.

normally constrain is used like this

fadeLevel1 = constrain(fadeLevel1, 0, 255);

but there may be ways to use it that I haven't seen before

Wow. I feel stupid. I've been staring at the "constrain" explanation page on this site and never figured that out. It works perfectly now, thank you very much!

So, it works great. I got it to do what I want it to do. I’m trying to get a little better with serial comms, so I added a function to basically add 1 to an integer every time it goes through the loop, that way I can set a condition where it will print only after a set number of iterations when that integer meets a certain value. Here’s what I have

int fadeLevel1 = 0;
int fadeLevel2 = 0;
int fadeLevel3 = 0;
int iteration = 0;

void setup() {
  //set the function of my button and led pins
  for(int ledPins = 3;ledPins < 7;ledPins++){
    pinMode(ledPins, OUTPUT);
  }
  for(int buttonPins = 8;buttonPins < 13; buttonPins++){
  pinMode(buttonPins, INPUT_PULLUP);
}
Serial.begin(9600);

}

// the loop function runs over and over again forever
void loop() {

//define my buttons by color
int greenButton = digitalRead(8);
int blueButton = digitalRead(9);
int redButton = digitalRead(10);
int upButton = digitalRead(11);
int downButton = digitalRead(12);
//define the RGB components by color
int redLed = 5;
int blueLed = 3;
int greenLed = 6;
//create an array of values for analogWrite functions
//write the fade level to each color of the RGB
analogWrite(redLed, fadeLevel1);
analogWrite(blueLed, fadeLevel2);
analogWrite(greenLed, fadeLevel3);

//set a series of statements to use the buttons
//to raise or lower the duty cycle of the led by color
//using if statements to look for simultaneous presses
//of a color button and the "up" or "down" buttons
//respectively.  The buttons are being held and there are
//delays built in, so debouncing is a non-factor.

if(greenButton == LOW && upButton == LOW){
  fadeLevel1 = fadeLevel1 + 1;
  delay(10);
  }
if(blueButton == LOW && upButton == LOW){
  fadeLevel2 = fadeLevel2 + 1;
  delay(10);
}
if(redButton == LOW && upButton == LOW){
  fadeLevel3 = fadeLevel3 + 1;
  delay(10);
}
if(greenButton == LOW && downButton == LOW){
  fadeLevel1 = fadeLevel1 - 1;
  delay(10);
}
if(blueButton == LOW && downButton == LOW){
  fadeLevel2 = fadeLevel2 - 1;
  delay(10);
}
if(redButton == LOW && downButton == LOW){
  fadeLevel3 = fadeLevel3 - 1;
  delay(10);
}
//Now we constrain our values so that only the fade range of PWM
//for an LED is a legitimate value that goes back to the start
//of the loop.
fadeLevel1 = constrain(fadeLevel1, 0, 255);
fadeLevel2 = constrain(fadeLevel2, 0, 255);
fadeLevel3 = constrain(fadeLevel3, 0, 255);

//Add one to our integer, essentially counting passes through the loop
iteration = iteration + 1;

//So, this should only print when iteration = 100
if(iteration == 100){
Serial.print("Green level: ");
Serial.println(fadeLevel1);
Serial.print("Blue level: ");
Serial.println(fadeLevel2);
Serial.print("Red Level: ");
Serial.println(fadeLevel3);

I eventually wanted to set it in a way that it would print after a set number of passes, but it is actually pretty much doing that now. It prints a set of data every couple of seconds or so. I thought maybe it was just counting to some ceiling and resetting, but I added " Serial.println(iteration); " right after the adding bit and now it is giving me an ever growing string of numbers and not spitting my fadelevel values at me, so I don’t get it.

some of your program didn't get copyed so its hard to tell but im guessing if it is printing every few seconds that you are over flowing a int.

I should know this but to be honest ive forgot......

I think a max signed int which you call using int iteration = 0; is the number 32767 which will then become negative until it maxes again then count positive so

if(iteration == 100){

may only be true every 65,000 ish cycles of the program (I might be wrong)

What you are try to do can be done with a millis timer. (lots of good write ups on how to make and use a millis timer)