pulseIn and led

Hi guys,
I am trying to use a pushbutton and measure its pulse by using pulseIn. The pushbutton is connected to 5v with a pullup resistor. Then, use the measured pulse and convert it to the variable that can control the PWM duty cycle. Then, I can use this variable with analogWrite to control the led.

duration = pulseIn(button, HIGH);

I know I can measure the pulse by this. Then I don't know how to convert it.
note: that variable should between 0 and 255.
PLS help, thank you guys

Maybe you'll find the map function useful:

There's another way to scale a number, two ways actually... '*' and '/'.

If the button makes the pin pulse LOW, shouldn't you be looking for a LOW pulse instead of a HIGH pulse?

What range of microseconds do you want to map to the PWM range (0-255). Maybe one 2.55 seconds? 2550000 microseconds? Note, there is a (default of) 1 second timeout on pulseIn(). Add a third argument if you want to allow more time.

   unsigned long pulse = pulseIn(button, LOW, 100000000UL);  // 100-second timeout
  analogWrite(outputPin, constrain(pulse/10000, 0, 255));

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.