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:- Sets the lamp pin (
LAMP
) as an output. - Attaches an interrupt to digital pin 2 using
attachInterrupt()
.
- Sets the lamp pin (
-
zero_cross()
: This function is called whenever there is a change on digital pin 2. It performs the following tasks:- Calculates the dimming time for the lamp based on the current
dim_val
value. - Delays for the calculated dimming time using
delayMicroseconds()
. - Turns on the lamp for a brief period using
digitalWrite(LAMP, HIGH)
anddelayMicroseconds()
. - Turns off the lamp using
digitalWrite(LAMP, LOW)
.
- Calculates the dimming time for the lamp based on the current
-
loop()
: This function runs continuously after thesetup()
function. It performs the following tasks:- Reads the value from a potentiometer using
analogRead(A0)
. - Maps the potentiometer value to a range between 10 and 49 using
map()
. - Updates the
dim_val
variable with the mapped value.
- Reads the value from a potentiometer using
- AC lamp
- Microcontroller board (e.g., Arduino)
- Potentiometer
- Connecting wires
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:
- Connect the AC lamp to the microcontroller's output pin (
LAMP
). - Connect a potentiometer to the microcontroller's analog input pin (
A0
). - Upload the code to the microcontroller.
- Observe the changes in the lamp's brightness as you adjust the potentiometer.
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!
You can support me by buy me a coffee if u like to.