Pulsating a diode for microseconds- code not working

I wrote this code to pulsate a diode for an interval of microseconds after clicking a button. The problem is that the diode pulsates longer than the value in the interval variable because it turns on the whole time that my finger pushes the button (which is longer than some microseconds). What should I correct from this code to get it to just pulsate the microseconds in the interval variable?

const int pinAnalogIn = A0;
const int pinAnalogOut = 9;
const int button = 2;
const long interval = 500;

int valueInPotentio = 0;
int valueOutDiode = 0;
int state = 0;

unsigned long duration = 0;

void setup()
{
Serial.begin(9600);
pinMode(button, INPUT);
}

void loop()
{
unsigned long currentmicros = micros();

valueInPotentio = analogRead(pinAnalogIn);
valueOutDiode = map(valueInPotentio, 0, 1023, 0, 255);

if (digitalRead(button) == HIGH && state == 0){
state = 1;
duration = currentmicros + interval;
}

if (state == 1){
analogWrite(pinAnalogOut, valueOutDiode);
} else {
analogWrite(pinAnalogOut, 0);
}

if (currentmicros >= duration){
state = 0;
}

delay(2);
}

Hello
Well,I think you shall delete the delay(2); ?

I would use my DebounceSwitch class from Debouncing Switches in Arduino

Here is a simple sketch that toggles the LED when the normally open momentary push button is released.

#include <DebouncedSwitch.h>
int led = 13;
int D4 = 4; // give the pin a name

DebouncedSwitch sw(D4); // monitor a switch connected between input D4 and GND

void setup() {
  pinMode(led, OUTPUT); // initially low (OFF)
}

void loop() {
  sw.update(); // call this every loop to update switch state
  if (sw.isChanged()) { // debounced switch changed state Up or Down
    // isChanged() is only true for one loop(), cleared when update() called again
    if (!sw.isDown()) { // switch was just released
      unsigned long current_uS = micros();
      digitalWrite(led,HIGH);   // 
      while((micros() - current_uS) < 1) {
       }
       digitalWrite(led,LOW);
    }
  }
}

The actual timing will vary depending on the board.
Using one of the onboard timers might be better but less portable.

Arduino analogWrite is PWM and runs 490Hz with 8-bit resolution on pin 9.
And the value is updated only once every 2 milliseconds.
I think this is not suitable for the output you want.

1 Like

Unless you have something else in your loop() that can't stand a 1/2-millisecond delay, this should do the trick. Debounces the button and looks for a state change. When the state changes to HIGH, send the 500 microsecond pulse.

const int pinAnalogIn = A0;
const int pinAnalogOut = 9;
const int button = 2;
const unsigned long interval = 500;

int valueInPotentio = 0;
int valueOutDiode = 0;

int PreviousState = LOW;
unsigned long LastStateChangeTime = 0;  // Debounce timer

void setup()
{
  Serial.begin(9600);
  pinMode(button, INPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  
  valueInPotentio = analogRead(pinAnalogIn);
  valueOutDiode = map(valueInPotentio, 0, 1023, 0, 255);

  int buttonState = digitalRead(button);
  
  // Detect state change
  if (buttonState != PreviousState && currentMillis - LastStateChangeTime > 10)
  {
    PreviousState = buttonState;
    LastStateChangeTime = currentMillis;
    
    if (buttonState == HIGH)
    {
      analogWrite(pinAnalogOut, valueOutDiode);
      delayMicroseconds(interval);
      analogWrite(pinAnalogOut, 0);
    }
  }
}

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