Back to Parent

//exercise :combined a switch and potentiometer
int led1 = D2;
int buttonPin = D3;
int buttonState;

int potPin = A5;
int potRead = 0;

void setup() {

    pinMode( led1, OUTPUT);
    pinMode( buttonPin, INPUT_PULLUP);
}

void loop() {
    
    buttonState = digitalRead(buttonPin);
    
    potRead = analogRead(potPin); //0-4095
    
    if(buttonState == LOW){
        //digitalWrite( led1, HIGH );
        int ledBrighteness = map( potRead, 0, 4095, 0, 255);
        analogWrite(led1, ledBrighteness); //0-255
    }else{
        digitalWrite( led1, LOW );
    }

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0