
How to control AC power with dimmer. Main functions.
How to control AC power with dimmer. Main functions.
In the previous post, we described how to connect the dimmer to a microcontroller and initialize the library.
The main function dimmer.setPower
(%) is used to control the connected dimmer.
The function sets the dimming value from 0 to 100 (%). For example, if you want to set to half of the input voltage, set the parameter to 50% (no percent sign is used) - dimmer.setPower(50)
.
If you need to smoothly change the brightness of the lamp or speed of a motor, then you can set the dimming value in the cycle.
In this example, the voltage changed from 0 to 100% in 1 second
1 2 3 4 5 |
for (int x = 0; x < 100; x++) { dimmer.setPower(x); delay (10); } |
Additional functions of the RBDdimmer.h library
- Function
getPower()
to display the current dimming value.- Example:
Serial.print(dimmer.getPower());
Result 0~100 int
- Example:
- Function
setState()
sets dimming state ON/OFF. ON = 100%, OFF = 0%- Example:
dimmer.setState(ON); delay(100); dimmer.setState(OFF);
- Example:
- Function
getState()
displays the current state of dimmerSerial.print(dimmer.getState());
Result 0 (OFF) or 1 (ON)
- Function
changeState()
toggle dimmer state to the opposite one.- Example:
dimmer.setState(ON); delay(100); dimmer.changeState; delay(100);
- Example:
- Function
getMode()
displays values of current work mode.- Example:
Serial.print(dimmer.getPower());
Result 0 (NORMAL_MODE) or 1 (TOGGLE_MODE)
- Example:
- Function setMode() sets and changes the work mode (NORMAL_MODE and TOGGLE_MODE) dimmer.setMode(NORMAL_MODE/TOGGLE_MODE)
- Function
toggleSettings(x,y);
smooth change of dimming value up or down in a defined range. Used in mode TOGGLE_MODE. Example SimpleToggleDimmer
Example: The following sketch is meant to define the dimming value through the serial port of the controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
/************** * RobotDyn * Dimmer Library * ************** * * The following sketch is meant to define the dimming value through the serial port of the controller, * using USE_SERIAL.begin * void printSpace() function is used for adding of space after functional data * void loop() serial port evaluator, used to register and define values in dimmer.setPower(outVal); * * #include <RBDdimmer.h>// //#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 //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 outVal = 0; void setup() { USE_SERIAL.begin(9600); dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) USE_SERIAL.println("Dimmer Program is starting..."); USE_SERIAL.println("Set value"); } void printSpace(int val) { if ((val / 100) == 0) USE_SERIAL.print(" "); if ((val / 10) == 0) USE_SERIAL.print(" "); } void loop() { int preVal = outVal; if (USE_SERIAL.available()) { int buf = USE_SERIAL.parseInt(); if (buf != 0) outVal = buf; delay(200); } dimmer.setPower(outVal); // setPower(0-100%); if (preVal != outVal) { USE_SERIAL.print("lampValue -> "); printSpace(dimmer.getPower()); USE_SERIAL.print(dimmer.getPower()); USE_SERIAL.println("%"); } delay(50); } |
Example: The following sketch turns the lamp on/off with use of a button
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/************** * RobotDyn * Dimmer Library * ************** * * The following sketch is meant to turn the lamp on/off with use of a button. * pinMode(14, INPUT); button is connected to 14th pin * void loop() ON/OFF button evaluator of dimmer in dim4.setState(ON/OFF); * * #include <RBDdimmer.h>// //#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 //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 buttonRed = 0; void setup() { dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) dimmer.setPower(50); pinMode(13, INPUT); } void loop() { buttonRed = digitalRead(13); if (buttonRed == 1) { delay(10); dimmer.setState(ON); //name.setState(ON/OFF); } if (buttonRed == 0) { delay(10); dimmer.setState(OFF); //name.setState(ON/OFF); } } |
Example: The following sketch is to define dimming value through potentiometer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/************** * RobotDyn * Dimmer Library * ************** * * The following sketch is meant to to define dimming value through potentiometer, * The potentiometer values are changing in range from 0 to 1023 * potentiometer values are converted through the map function to values from 0 to 100% and saved in dimmer.setPower(outVal); * Serial.begin is used to display dimming values * #include <RBDdimmer.h>// //#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 //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 outVal = 0; void setup() { USE_SERIAL.begin(9600); dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) } void loop() { outVal = map(analogRead(0), 1, 1024, 100, 0); // analogRead(analog_pin), min_analog, max_analog, 100%, 0%); USE_SERIAL.println(outVal); dimmer.setPower(outVal); // name.setPower(0%-100%) } |
Example: 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
/************** * 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 * #include <RBDdimmer.h> //#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); } |
Example: The following sketch used function the smooth changes of dimming values in a range of values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/************** * RobotDyn * Dimmer Library * ************** * The following sketch is meant to define by function the smooth changes of dimming values in a range of values defined by user * values are defined in range from 0 to 100% * #include <RBDdimmer.h>// //#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 //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 void setup() { USE_SERIAL.begin(9600); dimmer.begin(TOGGLE_MODE, OFF); //dimmer initialisation: name.begin(MODE, STATE) USE_SERIAL.println("--- Toggle dimmer example ---"); dimmer.toggleSettings(0, 100); //Name.toggleSettings(MIN, MAX); dimmer.setState(ON); // state: dimmer1.setState(ON/OFF); pinMode(14, INPUT); } void loop() { // put your main code here, to run repeatedly: } |