0

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);
}
Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
asked May 1, 2020 at 10:06
4
  • 1
    use digitalWrite and set the pin HIGH for desired duration Commented May 1, 2020 at 10:11
  • AnalogWrite generates a series of pulses with a varying duty cycle. If you only want a single pulse, you should probably write the code manually. What range of pulse widths do you want, and with what precision and accuracy? Commented May 1, 2020 at 11:43
  • As others have said, you could use digitalWrite() and the millis() 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 the digitalWrite() function, so you can make your timing more exact. And if you need sub-millisecond precision you could use the micros() function for your timing. Commented May 1, 2020 at 11:44
  • Thank you for your replies and suggestions. I am trying to synchronize two neural recording systems. Therefore I am trying to generate those pulses with varying width and fixed time so I can align them later on offline, to set a proper off set time in data analysis. I was thinking of pulse widths (5-500)ms. Commented May 1, 2020 at 12:00

1 Answer 1

4

Simple but not fully accurate:

  • Turn on LED
  • Delay for the amount of pulse width (e.g. 20)
  • Turn off LED
  • Delay 5000 ms - pulse width (20) thus 4980 in this example

However, since turn on/off a LED will also take some time, this will take slightly more than 20 + 4980 = 5000 ms.

Thus better is:

  • Set time to a variable (use millis function).
  • Turn on LED
  • Wait until time>= pulse width
  • Turn off LED
  • Wait until time>= 5000
answered May 1, 2020 at 10:11
2
  • I wonder why OP used analogWrite. Commented May 1, 2020 at 10:41
  • @Juraj I assume he wants a 'random' pulse width (PWM) generated. so the pulse width I use in my example is some random time the OP should use. Commented May 1, 2020 at 10:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.