1 LED gradually change brightness by control on Potentiometer
1 LED gradually change brightness by control on Potentiometer
const int sensor = A5; // Constantly make potentiometer connected to analog pin A5, the value
should be an integer
const int RLED = 3; // Constantly assign integer variable RLED connected to PWM pin 3
int value = 0; // Assign the integer variable to store the potentiometer value to 0 in the
beginning, will change based on the code in the loop
int brightness = 0; // Assign the integer variable "brightness" as a newValue of "value", the
identity is in the loop, but this is still set to 0 first, which ensures the brightness starts from 0
const int DLay = 25; // Constantly assign the regular delay time to an integer of 25ms
void setup() {
pinMode(RLED, OUTPUT); // Set red LED as output
pinMode(sensor, INPUT); // Set the sensor (potentiometer) as input
Serial.begin(9600); // Set baud rate for Serial Monitor to 9600
}
void loop() {
value = analogRead(sensor); // Let Arduino check the current voltage level from sensor (pin
A5), turn it into digital value, and store it in the value of variable "value"
int brightness = map(value, 0, 1023, 0, 255); // Map a new value that is different than 'value'
(0~1023), which is set to 0~255, because 0~255 is the range that analogWrite could scan, and
we need to use analogWrite in this assignment
// Display the analog value and mapped brightness value in the Serial Monitor
Serial.print("Brightness: "); // Display message "Brightness:" in serial monitor
Serial.println(brightness); // Display mapped brightness value after