int led1 = D0;
int led2 = D6;
int led3 = D7;
int potenciometer = A2;
int brightness; // variable for brightness of LED1
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(potenciometer, INPUT);
}
void loop() {
brightness = analogRead(potenciometer) / 4; //analogRead for values from 0-1024 and analogWrite from 0-255
// Because the ADC converter (analogRead) is a ten-bit device, and the PWM timer (analogWrite) is eight-bit. So, 1024/4 = 256
// Set the brightness for each LED based on the scaled potentiometer value
if (brightness >= 0 && brightness <= 85) {
analogWrite(led1, brightness);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}
else if (brightness > 85 && brightness <= 170) {
digitalWrite(led1, HIGH); //Remember digitalWrite for HIGH and LOW, and analogWrite for values from 0-255
analogWrite(led2, brightness - 85); //It´s important to return to the 0 value to gradually turn on the LED
digitalWrite(led3, LOW);
}
else if (brightness > 170 && brightness <= 255) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
analogWrite(led3, brightness - 170); //It´s important to return to the 0 value to gradually turn on the LED
}
}