|
/* |
|
Simple dimmer controlled with buttons |
|
Sarverott @ 2023 |
|
|
|
https://gist.github.com/Sarverott/3b352c6c3fe1c3d13f3faf642cc3b23c |
|
|
|
this project ingredients: |
|
- digispark board { |
|
ATtiny85 :: [ Flash memory = 8kb ; frequency = 16MHz ] |
|
Board operational voltage = 5V |
|
( microUSB B version ) |
|
} |
|
- push buttons *2 (can be replaced with single tristate momentary switch) |
|
- Power MOSFET [ IRFZ44VPbF by InternationalRectifier ] (or similar supplemet) |
|
- 8.2KOhm resistor *2 (both can be replaced with 10KOhm) |
|
- 12v power supply <IN> |
|
- 12v powered LED stripe <OUT> |
|
- some wires and prototype board |
|
|
|
NOTE: |
|
In both cases, whether soldering or using a contact plate, |
|
I recommend using goldpin connectors: |
|
- between the power supply and dimmer |
|
- between dimmer and the led strip |
|
|
|
*/ |
|
|
|
const int pwmPin = 1; // output to controll MOSFET with PWM signal |
|
|
|
const int lightButton = 2; // LEDs brighter button |
|
const int darkButton = 0; // LEDs dimmer button |
|
|
|
byte lightVolume = 255; // current light power (scale 0-255) |
|
|
|
void setup() { // this is preparation |
|
|
|
pinMode(pwmPin, OUTPUT); // set output pin for MOSFET |
|
|
|
pinMode(lightButton, INPUT); // set input pin for button |
|
pinMode(darkButton, INPUT); // set input pin for button |
|
|
|
} // start routine |
|
|
|
void loop() { // this is routine |
|
analogWrite(pwmPin, lightVolume); // update current brightness |
|
|
|
int lightButtonState = digitalRead(lightButton); // read light increse button |
|
int darkButtonState = digitalRead(darkButton); // read light decrese button |
|
|
|
if(lightButtonState == HIGH && lightVolume < 255) // if increse button pressed and light level below maximum |
|
lightVolume++; // light level +1 |
|
|
|
else if(darkButtonState == HIGH && lightVolume > 0) // if decrese button pressed and light level above minimum |
|
lightVolume--; // light level -1 |
|
|
|
delay(10); // wait 0.01 seconds |
|
} // repeat routine |