Problem with RGB controller with multiple switches.

Hi, im new to the world of programing and I have some problems with the code for a program.
The project at total is a wake-up lamp with RGB that will dim up the light over a period of time until it is at full brightness when the Arduino Uno gets power.
And I am gonna switch between colors with the help of a switch with 8 on/off.
I use a "for" loop that count up to 255 steps and the brightness depends on how many steps it have made.
So the problem is that when the "for" loop is active I cant interupt it with my switches and I cant turn it off if I turn all switches off.
I would be glad if someone smart person could help me and hopefully I will learn something on the way aswell. :smiley:

Some spec:
I am gonna use a Arduino UNO with ATMega328P

And the code. Since it is big I will only post the part I think is essential for the problem.

//-----------------------------------Button 2------------------------------------------------------ 


if (lightMode == 2)
{
  
if (varRed,varGrn,varBlu <= 255);
{
  for (int x=0; x<255; x++)    //loop that goes throuh 255 steps.   
  {  
    varRed++,varGrn++,varBlu++;
    if (varRed,varGrn,varBlu >= 0)
    {
    
    delay(dimDelay); //Should be 7000 later for 7sec delay between each fade step at a total of 30 min for full light.

analogWrite(redPin, varRed);    
analogWrite(grnPin, varGrn);
analogWrite(bluPin, varBlu);
Serial.print(varRed);
Serial.println(" of 255 fade steps, Red---Button 2"); 
Serial.print(varGrn);
Serial.println(" of 255 fade steps, Green---Button 2");
Serial.print(varBlu);
Serial.println(" of 255 fade steps, Blue---Button 2");
 
Serial.print(lightMode);
Serial.println(" lightMode");
    
 if (varRed,varGrn,varBlu >= 255) 
 {
  exit(varRed); //Stops "for" loop so it dosen´t keep counting.
  exit(varGrn); //Stops "for" loop so it dosen´t keep counting.
  exit(varBlu); //Stops "for" loop so it dosen´t keep counting.
 }
 
    
}
}
}
}

//------------------------------------Button 2 End------------------------------------------------

Ctrl - A, Ctrl - X, and start over. You can not use delay() in a sketch that needs to be reactive.

Read, understand, and embrace the blink without delay methodology.

Ahh I think I understand what you mean. I will take a look at the exemple and start over. Thanx for the quick answer :smiley: cheers!

You don't need to totally scrap your sketch, but you need to substantially change the control structure. The innermost part of your code which deals with a particular set of RGB values would remain essentially the same. Instead of calling it in a loop, you would use the 'blink without delay' example PaulS showed you to increment the RGB values on a timed basis, and after each change you would then execute your code to do something with the RGB values.

However, you should note that your code contains several fundamental mistakes that will stop it from doing anything useful, and you would need to correct these anyway. For example, the statements below do not do anything useful or desirable:

if (varRed,varGrn,varBlu >= 0)

 if (varRed,varGrn,varBlu >= 255) 
 {
  exit(varRed); //Stops "for" loop so it dosen´t keep counting.
  exit(varGrn); //Stops "for" loop so it dosen´t keep counting.
  exit(varBlu); //Stops "for" loop so it dosen´t keep counting.
 }