I am trying to generate a square pulse with a fixed delay of 5 second between pulses and varying width of the pulse. The code I wrote so far generates a sequence of pulses every 5 seconds. Is it possible to generate one pulse (with varying width) every 5 seconds instead? Could you please help me with it?
const int pin = 11;
int i = 0;
void setup() {
Serial.begin(9600);
pinMode(pin, OUTPUT);
}
void loop() {
i = random(0 , 255);
analogWrite(pin, i); // turn on LED
delay(5000);
}
digitalWrite()
and themillis()
function to get decent precision and accuracy. If you need higher precision and accuracy though you may want to switch to using port registers to manipulate the pin directly. That is significantly faster to respond than thedigitalWrite()
function, so you can make your timing more exact. And if you need sub-millisecond precision you could use themicros()
function for your timing. – Duncan C May 1 '20 at 11:44