/************** * RobotDyn * Dimmer Library * ************** * The following sketch is meant to smoothly turn the dimmer ON after pressing the first button and turn it OFF after pressing a second one * * ---------------------- OUTPUT & INPUT Pin table --------------------- * +---------------+-------------------------+-------------------------+ * | Board | INPUT Pin | OUTPUT Pin | * | | Zero-Cross | | * +---------------+-------------------------+-------------------------+ * | Lenardo | D7 (NOT CHANGABLE) | D0-D6, D8-D13 | * +---------------+-------------------------+-------------------------+ * | Mega | D2 (NOT CHANGABLE) | D0-D1, D3-D70 | * +---------------+-------------------------+-------------------------+ * | Uno | D2 (NOT CHANGABLE) | D0-D1, D3-D20 | * +---------------+-------------------------+-------------------------+ * | ESP8266 | D1(IO5), D2(IO4), | D0(IO16), D1(IO5), | * | | D5(IO14), D6(IO12), | D2(IO4), D5(IO14), | * | | D7(IO13), D8(IO15), | D6(IO12), D7(IO13), | * | | | D8(IO15) | * +---------------+-------------------------+-------------------------+ * | ESP32 | 4(GPI36), 6(GPI34), | 8(GPO32), 9(GP033), | * | | 5(GPI39), 7(GPI35), | 10(GPIO25), 11(GPIO26), | * | | 8(GPO32), 9(GP033), | 12(GPIO27), 13(GPIO14), | * | | 10(GPI025), 11(GPIO26), | 14(GPIO12), 16(GPIO13), | * | | 12(GPIO27), 13(GPIO14), | 23(GPIO15), 24(GPIO2), | * | | 14(GPIO12), 16(GPIO13), | 25(GPIO0), 26(GPIO4), | * | | 21(GPIO7), 23(GPIO15), | 27(GPIO16), 28(GPIO17), | * | | 24(GPIO2), 25(GPIO0), | 29(GPIO5), 30(GPIO18), | * | | 26(GPIO4), 27(GPIO16), | 31(GPIO19), 33(GPIO21), | * | | 28(GPIO17), 29(GPIO5), | 34(GPIO3), 35(GPIO1), | * | | 30(GPIO18), 31(GPIO19), | 36(GPIO22), 37(GPIO23), | * | | 33(GPIO21), 35(GPIO1), | | * | | 36(GPIO22), 37(GPIO23), | | * +---------------+-------------------------+-------------------------+ * | Arduino M0 | D7 (NOT CHANGABLE) | D0-D6, D8-D13 | * | Arduino Zero | | | * +---------------+-------------------------+-------------------------+ * | Arduino Due | D0-D53 | D0-D53 | * +---------------+-------------------------+-------------------------+ * | STM32 | PA0-PA15,PB0-PB15 | PA0-PA15,PB0-PB15 | * | Black Pill | PC13-PC15 | PC13-PC15 | * | BluePill | | | * | Etc... | | | * +---------------+-------------------------+-------------------------+ */ #include //#define USE_SERIAL SerialUSB //Serial for boards whith USB serial port #define USE_SERIAL Serial #define outputPin 12 #define zerocross 5 // for boards with CHANGEBLE input pins #define LAMPMAXVALUE 100 //dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero int stateL = 0, valLamp; int DIM4 = 0; int mainLamp = 0; int buttonRed = 0; int buttonBlue = 0; bool setLamp = true; void setup() { USE_SERIAL.begin(9600); dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) } void RiseFallLamp(bool RiseFallInt) { if ((RiseFallInt == true) && (mainLamp < LAMPMAXVALUE)) mainLamp++; else if ((RiseFallInt != true) && (mainLamp > 0)) mainLamp--; } bool setLampState(int val) { bool ret; if (val >= 1) ret = true; else ret = false; return ret; } void readButtonState() { buttonRed = digitalRead(13); buttonBlue = digitalRead(15); if (buttonRed < 1) stateL++; if (buttonBlue < 1) stateL--; if (stateL < 0) stateL = 0; if (stateL > 1) stateL = 1; } void loop() { readButtonState(); dimmer.setPower(mainLamp); // setPower(0-100%); RiseFallLamp(setLampState(stateL)); delay(25); }