void setup()
{
pinMode(11, OUTPUT);
}
void loop()
{
digitalWrite(11, HIGH);
delay(100); // Approximately 10% duty cycle @ 1KHz
digitalWrite(11, LOW);
delay(900);
}
Will work - in the sense you have a constant semi-bright LED - if you replace the delay() with delayMicroseconds().
So next step is to use two int variables, lowRatio and highRatio, giving them the values 100 and 900.
Then you put in the button code that - say - puts the values 200 and 800 respectivly. This should change your brightness.
Having put these delays into your main loop code, your loop() is limited to run once a millisecond. That is OK for such a short demo program. The challenge is to replace the two delays with one construct using two "timervalues" based on the "blink without delay" example sketch. That way your loop can run unhindred at near Mhz speed - which may be important for other tasks your bigger programs will need.
NB: You do not need to do ALL example sketches
only the ones that contain relevant peripherals for your projects. But then if you want to learn EVERYTHING about C++ then you're going to need to read all examples and the source of the libraries and core. ![]()