Devices & Components
Arduino Nano
LED strip white 5v or 12v
Slider potentiometer
IRLZ44N
Momentary push button
Software & Tools
Arduino IDE
Project description
Code
Controlling Strip using Push Button and slider potentiometer
c
Button controlling On/Off states ,Pot: brightness
1const int ledPin = 11; // PWM pin connected to MOSFET gate 2const int buttonPin = 2; // Button with internal pull-up 3const int potPin = A3; // Potentiometer pin 4 5volatile bool ledState = false; // LED on/off state 6volatile unsigned long lastDebounceTime = 0; // Debounce timer 7const unsigned long debounceDelay = 50; // 50ms debounce delay 8 9void setup() { 10 pinMode(ledPin, OUTPUT); 11 pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up 12 attachInterrupt(digitalPinToInterrupt(buttonPin), toggleLED, FALLING); 13} 14 15void loop() { 16 int brightness = map(analogRead(potPin), 0, 1023, 0, 255); // Map analog to PWM 17 if (ledState) { 18 analogWrite(ledPin, brightness); // Set brightness 19 } else { 20 analogWrite(ledPin, 0); // Turn off if LED is off 21 } 22} 23 24// Interrupt function to toggle LED state 25void toggleLED() { 26 unsigned long currentTime = millis(); 27 if (currentTime - lastDebounceTime > debounceDelay) { 28 ledState = !ledState; // Toggle LED state 29 lastDebounceTime = currentTime; 30 } 31}
Comments
Only logged in users can leave comments