void setup() { ledcSetup( 0, 0, 1 ); ledcAttachPin( 4, 0 ); pinMode(5, INPUT); } void loop() { if( digitalRead(5) ) { int v = analogRead( 15 ) / 16; if( v < 4 ) v = 4; int d = 1000/v - v; if( d < 100 ) d = 100; ledcWriteTone( 0, v ); ledcWrite( 0, 1 ); delay( d ); } else { ledcSetup( 0, 4000000, 1 ); ledcWrite( 0, 1 ); delay( 1000 ); } }