Not able to detect a 1 second width pulse

I do not know if my problem is coming from Arduino or the oscilloscope itself.

I want to generate pulses with a fixed width, without using wait() because I do not want to stop the execution of the code.

Everything works perfectly fine for long pulses, however when I try less than 1 second in the oscilloscope I am not able to visualize it. It appears a much narrow pulse, around 5 ms.

I have only connected Resistors and LEDs to the output pins of PORTD.
I am probing directly the output of ARDUINO Pins

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>



int data;

unsigned long PulseEndMillis;
unsigned long currentMillis;
unsigned long PulseWidth =1000;


bool Triggering;

void setup() {
 
  Serial.begin(115200);   
  DDRD = DDRD | B11111100;  // this is safer as it sets pins 2 to 7 as outputs
                    // without changing the value of pins 0 & 1, which are RX & TX 
  PORTD=0;
  data=0;
  Triggering=false; 
}



void loop() {
  currentMillis = millis();
  // put your main code here, to run repeatedly:
  if (Serial.available()>0) {
        data = Serial.parseInt();     
        PORTD= data<<2;
        Triggering=true; 
        PulseEndMillis=currentMillis+PulseWidth;
        Serial.flush();
    }

  if(Triggering)
  {
    Serial.print(" currentMillis  ");
    Serial.print(currentMillis);
    Serial.print("  PulseEndMillis ");
    Serial.println(PulseEndMillis);

        if(currentMillis>=PulseEndMillis)
        {
            PORTD=0;
            PulseEndMillis=0;
            Triggering=false; 
        }
  }
    
}

If I run it with PulseWidth =2000;

14:52:24.012 → currentMillis 16393 PulseEndMillis 18393
14:52:24.012 → currentMillis 17393 PulseEndMillis 18393
14:52:24.012 → currentMillis 17395 PulseEndMillis 18393
14:52:24.012 → currentMillis 17398 PulseEndMillis 18393
14:52:24.012 → currentMillis 17402 PulseEndMillis 18393

14:52:24.996 → currentMillis 18381 PulseEndMillis 18393
14:52:24.996 → currentMillis 18385 PulseEndMillis 18393
14:52:24.996 → currentMillis 18390 PulseEndMillis 18393
14:52:24.996 → currentMillis 18394 PulseEndMillis 18393

However for a PulseWidth =1000 (See attached picture)

14:54:20.718 → currentMillis 5593 PulseEndMillis 6593
14:54:20.718 → currentMillis 6593 PulseEndMillis 6593

Why my code is not working? one second seems to be a considerable amount of time.

Capture.JPG

Have you tried only receiving values and printing them, so you can see what the actual value of ‘data’ is? Bit positions in portD will have different values because you’re sending a serial parameter. Are you sure you logic handles the transitions properly? For example, if last time you wrote a ‘7’, and this time you also wrote a ‘7’, there would be no change at the pins where you’re scoping it. Or, if you receive a different value, some pins will change and some won’t. So it matters what you send, and what pins you scope. Also you should never use future time stamps, because they will fail when millis() rolls over.

Also, you don’t need these, they are already included by the IDE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

I’m not sure about unistd.h. What features are you using from that?

What do you think

Serial.flush();

is doing? Did you read the documentation?

        Serial.flush();

WARNING: This only flushes OUTPUT, not INPUT.

To flush input:

 while (Serial.available()) Serial.read();