Arduino UNO inconsistent c pin out behavior

Hi!

Hope you guys can help me with this annoying problem that is happening.

Im trying to send a single pulse of 500 us, 5V to pin 13 (or any pin). I've hooked up an oscilloscope to measure how long pin 13 is turned on. To active pin 13, I'm using a pushbutton from a different pin. So everytime I push the button, I get 5V for 500us. I'm using delayMicroseconds(500) for this. The problem is, I don't get it consistently. I Press the button and sometimes it turns on and sometimes it doesn't. The faster I press it, the more times it turns on in a period but never consistently.

I know the button is working since I've added a serial.println("press") right below the on command of pin13 so everytime I press the button I can see it on the serial monitor. I have no idea why. Is it a limitation of my arduino in the microsecond realm? Here's a picture of the oscilloscope reading. I probably pressed the button 20+ times in order to get those 5V spikes.

Any advise would be greatly appreciated!

Thanks,
Tomas

Post your code.

Hey, westfw.

Thanks for replying.
Attached is my code but I don't think its the problem since the "Press" is printing as expected.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  8;      // the number of the LED pin


int buttonState = 0;       
int buttonLog= 1;
void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  
  buttonState = digitalRead(buttonPin);
  if (buttonState == buttonLog) 
  {
    if(buttonState == HIGH) {
    Serial.println("PRESS");
    digitalWrite(ledPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(ledPin,LOW);
    delay(100);
    buttonLog = 0;
    
  }
  
    else {
       digitalWrite(ledPin,LOW);
       buttonLog = 1;
  }
 delay(100);
  }
   
}

I'm no expert but I'm pretty sure you'll need to debounce the switch.

See Nick Gammon's explanation on this page: Switches

I'm also certain that after you've read it you'll see other reasons you're getting the effects you mention.