// Program: 1A Ex6 Q3 Chan Tai Ming.ino
// Program: Ex6C_Analog_Output-E.ino
// Wokwi: https://wokwi.com/projects/341138337709425236
/*
Exercise 6 Q3
Change the program of Ex6C_Analog_Output-E to:
1. Fully turn on the LED at 3.3V from VR
2. Dim the LED to zero intensity at 0V from VR
*/
#define LED_Out_Pin 25
#define SW_In_Pin 15
#define VR_In_Pin 36
int VRvalue;
int fadevalue;
int duty_cycle;
void setup()
{
// Initialize serial port
Serial.begin(115200);
// Set pins
pinMode(SW_In_Pin, INPUT_PULLUP);
// Assign pin to PWM channel
ledcAttachPin(LED_Out_Pin, 1);
// Initialize PWM channel
// channel 0-15, resolution 1-16 bits, freq limits depend on resolution
ledcSetup(1, 12000, 12); // 12 kHz PWM, 12-bit resolution
}
void loop()
{
// Reading
VRvalue = analogRead(VR_In_Pin);
// Display
Serial.print("Raw Sensor reading: ");
Serial.println(VRvalue);
duty_cycle = map(VRvalue, ??, ??, ??, ??);
ledcWrite(1, duty_cycle);
// fade in from min to max in increments of 5 points
// for (fadevalue = 0; fadevalue <= 4095; fadevalue += 5)
// {
// ledcWrite(1, fadevalue);
// // wait for 5 ms to see the dimming effect
// delay(5);
// }
// delay(1000);
// fade out from max to min in increments of 5 points
// for (fadevalue = 4095; fadevalue >= 0; fadevalue -= 5)
// {
// ledcWrite(1, fadevalue);
// // wait for 5 ms to see the dimming effect
// delay(5);
// }
delay(1000);
}