int led = 9; //Set LED in Pin 9
int cycleDir, dutyCycle; //cycleDir for increment/decrement, dutyCycle as integer input
// Function to set PWM duty cycle
void setPWMDutyCycle(int pin, int dutyValue) {
// Map the duty cycle percentage (0-100) to a value (0-255) for analogWrite
int pwmValue = map(dutyValue, 0, 100, 0, 255);
// Set the PWM signal on the specified pin
analogWrite(pin, pwmValue);
}
void setup() {
// Set the pin mode for PWM output
pinMode(led, OUTPUT);
}
void loop() {
/*****For dummy only, set value 0 to 100 then back to 0******/
if(dutyCycle == 0)
{
cycleDir = 1;
}
else if(dutyCycle >= 100)
{
dutyCycle = 100; // To make sure that the duty cycle is between 0 and 100
cycleDir = 0;
}
if(cycleDir == 1)
dutyCycle += 5;
else
dutyCycle-= 5;
/************************************************/
setPWMDutyCycle(led, dutyCycle); // Set int duty cycle for LED pin
delay(100); // Wait for 0.1 second
}