help with if-then statement

I have a LED light and two buttons - button a toggles the light on and off and button b is a dimmer. What I'm stuck on is the if-then statement to allow the dimmer button to work ONLY when the a button has turned the light on. When the a button has turned the light off the b button should do nothing. Below is my code:

int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;

boolean lastButtonA = HIGH;           //HIGH is the not pressed state
boolean currentButtonA = HIGH;        //HIGH is the not pressed state
boolean ledOn = false;              // Initial LED condition is OFF
int counterA = 0;
boolean lastButtonB = HIGH;           //HIGH is the not pressed state
boolean currentButtonB = HIGH;        //HIGH is the not pressed state
//boolean ledOn = false;              // Initial LED condition is OFF
int ledLevel = 0;
int counterB = 0;

//byte leds = 0;

void setup() 
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);  
  pinMode(buttonBpin, INPUT_PULLUP); 

  Serial.begin(9600);        //starts the serial monitor and sets the baud rate
}

void loop() 
{
  //Get button value or state
  currentButtonA = debounceA(lastButtonA);
  
  //Recall that whenever you release the button, the state goes HIGH
  //When you press the button, you are forcing the state LOW
  if(lastButtonA == LOW && currentButtonA == HIGH)
  {
    //Button press detected
    counterA += 1;                    //increment the counter
    ledOn = !ledOn;                  //shorthand way to switch to the opposite setting
    digitalWrite(ledPin, ledOn);     //Turn ON or OFF the LED
    Serial.print("LED =  ");
    Serial.print(ledOn);
    Serial.print("    Number of Toggles =  ");
    Serial.println(counterA);
  }
  lastButtonA = currentButtonA; 

  //something here                      //checks to see if A is on to run B loop
  {
    //Get button value or state
  currentButtonB = debounceB(lastButtonB);
  
  //Recall that whenever you release the button, the state goes HIGH
  //When you press the button, you are forcing the state LOW
  if(lastButtonB == LOW && currentButtonB == HIGH)
  {
    //Button press detected
    counterB += 1;                    //increment the counter
    //ledOn = !ledOn;                  //shorthand way to switch to the opposite setting
    //digitalWrite(ledPin, ledOn);     //Turn ON or OFF the LED
    ledLevel = ledLevel +51;
    if (ledLevel > 255) ledLevel = 0;
    analogWrite(ledPin, ledLevel);
    Serial.print("LED =  ");
    Serial.print(ledLevel);
    Serial.print("    Number of Toggles =  ");
    Serial.println(counterB);
  }
  lastButtonB = currentButtonB; 
  
  }
}

//Debouncing function
boolean debounceA(boolean lastA)
{
  //Local Variable
  boolean currentA = digitalRead(buttonApin);

  if(lastA != currentA){
    delay(5);                        //5 milliseconds
    currentA = digitalRead(buttonApin);
  }
  return currentA;
}

//Debouncing function
boolean debounceB(boolean lastB)
{
  //Local Variable
  boolean currentB = digitalRead(buttonBpin);

  if(lastB != currentB){
    delay(5);                        //5 milliseconds
    currentB = digitalRead(buttonBpin);
  }
  return currentB;
}

I would use a state variable that informs whether the LED is on or off, and check that when button B action is contemplated.

Create a variable to keep track of the on off state ... bool ButtonStateOn = false;

When the button is on then do the assignment ... ButtonStateOn = true;
When the button is off then do the assignment ... ButtonStateOn = false;

When testing then do ... if (ButtonStateOn==true){do this}

Steve

Nice first post.

You already have a variable you can test, ledOn. if ledOn isn’t true, skip checking switch B.

Now for extra credit, when you get that working see if you can make the dimmer go up to max, then dim down similar fashion non-off minimum… repeat.

a7

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.