Week 9 assignment

Concept

So basically, I made this little setup where I can control two LEDs using a knob and a button. One fades in and out smoothly when you twist the knob, and the other just pops on and off when you hit the button. It’s honestly pretty straightforward, but it was actually kind of fun to put together.

Demo
How It Works

The Knob: There’s a potentiometer, when you turn it, one of the LEDs gets brighter or dimmer. Twist one way and the light fades down, twist the other way and it gets brighter.

The Button: The other LED is even simpler. Press the button, light’s on. Let go, light’s off. That’s it.

Code

int pushButton = 2;
int led1 = 4;
int potentiometer = A5;
int led2 = 3;
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(pushButton, INPUT);
pinMode(potentiometer, INPUT);
}
void loop()
{
// digital sensor (switch) controlling ON/OFF state of LED
int buttonState = digitalRead(pushButton);
if (buttonState == HIGH){
digitalWrite(led1, HIGH);
}
else if (buttonState == LOW) {
digitalWrite(led1, LOW);
}
// analog sensor (potentiometer) controlling the brightness of LED
int potValue = analogRead(potentiometer);
int brightness = map(potValue, 0, 1023, 0, 255);
analogWrite(led2, brightness);
}

Future Improvements

Making It More Interactive: I could add more sensors to make it do cooler stuff. Like maybe a light sensor so the LEDs automatically adjust based on how bright the room is, or a temperature sensor that changes the colors based on how hot or cold it is.

Adding Colors: Right now it’s just basic LEDs, but I could swap them out for RGB LEDs. Then the potentiometer could control not just brightness but also what color shows up. Turn the knob and watch it cycle through the rainbow. That’d be way more visually interesting.

Leave a Reply