Adding potentiometer after a toggle push button

Try this:


const byte btnPin  =  4;
const byte ledPin1 = 11;
const byte ledPin2 = 6;
const byte ledPin3 = 5;
const byte potPin  = A0;

bool potOn = false;
int lastBtn = HIGH;

void setup() {
  pinMode(btnPin, INPUT_PULLUP); // reading the button will be HIGH if not pressed
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);

  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);

}

void loop() {

  int btnNow = digitalRead(btnPin);
  if (btnNow == LOW and lastBtn == HIGH) {
    //Button has just been pressed
    potOn = not potOn;
    delay(30); //Wait for any button bouncing to finish
  }
  lastBtn = btnNow; //save button state to compare next time

  if (potOn) {
    int brightness = analogRead(potPin) / 4;  // Results in 0 to 255
    analogWrite(ledPin1, brightness);  
    analogWrite(ledPin2, brightness);
    analogWrite(ledPin3, brightness);
  }
  else { // Jim added this
    // Turn LED off
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
  }
}