const int Lonboardchannel = 6; // use channel 6 const int Lonboardpin = 2; //use pin 2, that's the onboard LED const int pwmfrequency = 15000; //15 kHz frequency. change to sub-400Hz for visible artifacts const int pwmresolution = 10; //Resolution bits - 8, 10, 12, 15 int i = 0; volatile uint32_t timenow; const int fadespeed = 5; //5 ms tick interval volatile int fade1 = 0; volatile int fade2 = 1023; void setup() { Serial.begin(9600); ledcAttachPin(Lonboardpin, Lonboardchannel); //attach pin to channel ledcSetup(Lonboardchannel, pwmfrequency, pwmresolution); // setup channel } void loop() { Serial.println("Fading from " + String(fade1) + " to " + String(fade2)); if(fade1>fade2){ for (i = fade1; i > fade2; i--) { ledcWrite(Lonboardchannel, i); timenow = millis(); while (millis() < timenow + fadespeed) { yield(); } } }else{ for (i = fade1; i < fade2; i++) { ledcWrite(Lonboardchannel, i); timenow = millis(); while (millis() < timenow + fadespeed) { yield(); } } } fade1 = fade2; fade2 = exp((float)random(0, 69)/10); // min to max-1 }