Lighting project - need help

I have 2 buttons controlling an LED strip. Button 1 will ramp up to full bright, hit it again and it fades to off. Button 2 will do the same, but only half brightness.

I’ve done this with 2 separate functions, scene1 and scene2. I can’t figure out how to pass the current light level(PWM value) between the 2 functions. I’d also like to add dimmer switches to increase and decrease brightness, as well as an all off button. So I guess I’d need to pass the current PWM value to/from those functions as well.

Seems like, and I suspect, it’s a pretty simple procedure. But it’s got my newbie brain stumped. Any help is greatly appreciated! Here’s my code:

/* FauxLutron
   Each press of a scene button should raise or lower the light level
   to that specified b the variables "scene1Level" and "scene2Level"
*/

#define button1 2      //"Scene 1" button pin
#define button2 3      //"Scene 2" button pin
#define outPin 13      //"Pilot Light" pin
#define fixture1 6          //lighting fixture1

int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
int currLevel;         // the current PWM light level

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  //  pinMode(dimDown, INPUT);
  //  pinMode(dimUp, INPUT);
  //  pinMode(offButton, INPUT);
  pinMode(outPin, OUTPUT);
  pinMode(fixture1, OUTPUT);
  Serial.begin(9600);
  digitalWrite(fixture1, HIGH);
  delay(300);
  digitalWrite(fixture1, LOW);
}

void loop()
{
  scene1();
  scene2();
}
/////////////////////////////// Scene1 //////////
int scene1()
{
  int scene1Level = 255;
  static int state = LOW;
  static int lightLevel;
  reading = digitalRead(button1); //read state of button1
  //        *******debounce section********
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
    //Serial.print("Button 1 State = ");
    //Serial.println(state);
    time = millis();
  }//       **********change state variable for button**********
  digitalWrite(outPin, state); //write the output state variable

  //       ********Change LED state based on state variable********
  if (state == 1)
  {
    while (lightLevel < scene1Level)
    {
      (lightLevel = lightLevel + 5);
      delay(30);
      analogWrite(fixture1, lightLevel);
    }
  }
  //if the button is turned off(HIGH to LOW):
  else {
    while (lightLevel > 0)
    {
      (lightLevel = lightLevel - 5);
      delay(30);
      analogWrite(fixture1, lightLevel);
    }
  }
currLevel = lightLevel;
}//end scene1
//////////////////////////////// scene2 ///////////
int scene2()
{
  int scene2Level = 95; //light level for scene 2
  static int state = LOW;
  static int lightLevel;
  int reading2;
  reading2 = digitalRead(button2); //read state of button2
  //        *******debounce section********
  if (reading2 == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
    //Serial.print("Button 2 State = ");
    //Serial.println(state);
    time = millis();
  }//       *********change state variable for button2********
  digitalWrite(outPin, state); //write the output state variable

  //       ********Change LED state based on state variable********
  if (state == 1)
  {
    while (lightLevel < scene2Level)
    {
      (lightLevel = lightLevel + 5);
      delay(30);
      analogWrite(fixture1, lightLevel);
    }
  }
  //if the button is turned off(HIGH to LOW):
  else {
    while (lightLevel > 0)
    {
      (lightLevel = lightLevel - 5);
      delay(30);
      analogWrite(fixture1, lightLevel);
    }
  }
currLevel = lightLevel;
} //end scene2

Hi and welcome.

I would abandon the two function approach and start over. Have a global variable representing the target fade level. Adjust that to 0, 128 or 255 when one of the buttons is pressed. Have a second global variable for the current fade level. When the current fade level is above target, decrease it and when below target, increase it.

Paul

But I thought all the cool programmers used separate functions! :wink:

Thanks for the input, I'll try that approach...

Yes, they do. But, as with any programming technique, they should be used to make code simpler, easier to understand, shorter and less repetitive. Your two functions don’t seem to be doing any of those things, almost the opposite in fact. My advice would be to keep things as simple as possible at first. Then look for opportunities in your code to remove repeated sections using functions, and then break up long sections of code by splitting it into functions, but only where it makes it easier to understand overall.