This program controls the brightness of an AC lamp using interrupt and pulse width modulation (PWM) techniques. A potentiometer is used to adjust the lamp brightness.
The code is written in C/C++ and can be uploaded to an Arduino or similar microcontroller. It consists of three major functions: setup()
, zero_cross()
, and loop()
.
setup()
: This function is executed once when the program starts. It performs the following tasks:
LAMP
) as an output.attachInterrupt()
.zero_cross()
: This function is called whenever there is a change on digital pin 2. It performs the following tasks:
dim_val
value.delayMicroseconds()
.digitalWrite(LAMP, HIGH)
and delayMicroseconds()
.digitalWrite(LAMP, LOW)
.loop()
: This function runs continuously after the setup()
function. It performs the following tasks:
analogRead(A0)
.map()
.dim_val
variable with the mapped value.The flowchart below illustrates the flow of control and the sequence of actions performed in the code.
graph LR A[Start] --> B[Setup] B --> C[Attach Interrupt] C --> D[Read Potentiometer] D --> E[Map Potentiometer Value] E --> F[Update dim_val] F --> G[Loop] G --> H[Calculate dimming_time] H --> I[Delay] I --> J[Turn on Lamp] J --> K[Delay] K --> L[Turn off Lamp] L --> G
The flowchart demonstrates how the program initializes, reads the potentiometer value, calculates the dimming time, and controls the lamp's brightness using interrupt and PWM techniques.
To use this code, follow these steps:
LAMP
).A0
).Contributions to this project are welcome. You can fork the repository, make improvements, and submit a pull request.
This code is released under the MIT License.
Feel free to use, modify, and distribute this code for personal and commercial purposes.
Special thanks to the Arduino community for their support and resources.
If you find this code helpful, please give it a star!