Since two days I'm trying and I'm lost.
I'm trying do replicate a bought dimmer i have.
- You can set the time how long it takes to dim up or down (potentiometer on the device.
- You can set the minimum and maximum brightness (again potentiometer).
But i want to hardcode this two functions.
The dimmer works with just one button.
- If light is off and short press > light goes on (fades from 0 to last state)
- If light is on and short press > light goes off (fades down to 0)
- If light is off and button held > light goes on (fades from 0 up until button released)
- If light is on and button held > fades up or down until button released or minimum or maximum brightness is reached. If up or down depends on the last function. If the last thing was dimming up then a button holding dims down and vice versa.
Here is something i have tried:
#include <Bounce2.h>
const byte STARS_BUTTON = 9; // D9
const byte STARS_RELAY_ON = 11; // D11
const byte STARS_RELAY_OFF = 12; // D12
const byte STARS_STATE = 14; // A0
const byte SHOWER_LIGHT_BUTTON = 10; // D10
const byte SHOWER_RELAY_ON = 7; // D7
const byte SHOWER_RELAY_OFF = 8; // D8
const byte SHOWER_LIGHT_STATE = 19; // A5
const byte SHOWER_LIGHT_PWM = 6; // D6
Bounce showerLightButton = Bounce(); // Instantiate a Bounce object
Bounce starsButton = Bounce(); // Instantiate a Bounce object
int valueBrightness = 0; // variable to store the brightness
int minBrightness = 4; // minimum brightness for dimming down
int maxBrightness = 200; // maximum brightness for dimming up
int lastBrightness = 0; // variable to store the last brightness
int dimSteps = 5; // how many points to fade the LED by
unsigned long showerLightCurrentTime = 0;
unsigned long showerLightLoopTime = 0;
void showerLight() {
if (digitalRead(SHOWER_LIGHT_STATE)==LOW) {
showerLightCurrentTime = millis();
if(showerLightCurrentTime >= (showerLightLoopTime + 5000)){
analogWrite(SHOWER_LIGHT_PWM, valueBrightness);
Serial.print("Brightness:");
Serial.println(valueBrightness);
if (valueBrightness == 0 || valueBrightness == 255) {
dimSteps = -dimSteps;
}
valueBrightness = valueBrightness + dimSteps;
}
} else {
analogWrite(SHOWER_LIGHT_PWM, 0);
digitalWrite(SHOWER_RELAY_OFF, HIGH);
relay();
}
}
void relay() {
do {
digitalWrite(SHOWER_RELAY_OFF, HIGH);
} while (digitalRead(SHOWER_LIGHT_STATE)==LOW);
}
void setup() {
Serial.begin(9600); // Open serial communications and wait for port to open
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(SHOWER_LIGHT_BUTTON, INPUT); // Setup the button
// After setting up the button, setup the Bounce instance :
showerLightButton.attach(SHOWER_LIGHT_BUTTON);
showerLightButton.interval(10); // interval in ms
pinMode(SHOWER_LIGHT_PWM, OUTPUT);
pinMode(SHOWER_RELAY_ON, OUTPUT);
pinMode(SHOWER_RELAY_OFF, OUTPUT);
pinMode(SHOWER_LIGHT_STATE, INPUT);
}
void loop() {
// Update the Bounce instance :
showerLightButton.update();
// Get the updated value :
int value = showerLightButton.read();
if ( value == LOW ) {
showerLight();
}
}
The Bounce2 library does not have something like "if button pressed 2 seconds".
I tried this one: GitHub - t3db0t/Button: A big update of Tom Igoe's fork of Alexander Brevig's Button library for Arduino. Check out the Readme for all the details!
but there the "showerLight loop" does not run.
The relay is bistable and i get a feedback into pin 19 (A5) from it to see if its on or off.
So the first thing to do is to check the relay state (pin 19 high or low).
But the "relay loop" does not work.
RELAY_ON and RELAY_OFF should only be HIGH until the relay state changed.
At the moment if i push the button my LED is at full brightness immediately.
But in the console i can see counting like:
Brightness:0
Brightness:-5
Brightness:-10
Brightness:-15
Brightness:-20
Brightness:-25
Brightness:-30
Brightness:-35
Brightness:-40
Brightness:-45
At some point the LED dims down to then go back to full brightness without fading.
The numbers in the console go up infinite as long as i press the button.
I'm lost. I guess some of you coding gods to it in 15 minutes ![]()