Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Arduino code

Not open for further replies.


Mar 5, 2022
Reaction score
Trophy points
Activity points
Hi everyone, I am new in this forum, nice to meet you. I'd like to ask if this code of arduino it's right. I don't want to use the default functions in arduino as digitalWrite, delay...etc. instead I'd like to use the micros(); function and the port register control.

This is the code that I want to change:

      digitalWrite(3, LOW);

and I believe that it should be something like that:

       unsigned long currentMillis = micros();

      if (currentMillis - previousMillis >= 7000UL -valor) {
          PORTD |= B00001000;

        unsigned long currentMillis2 = micros();

        if (currentMillis2 - previousMillis2 >= 10UL) {
          PORTD &= !B00001000;
          previousMillis2 = currentMillis2;
        previousMillis = currentMillis;


Also this code:


And I believe that it should be:

DDRD &= B11111011;
if (!(PIND >> 2 & B00000100 >> 2)){

I'm new programing in this way. I'd really appreciate if someone can corroborate if it is right,

Thanks and regards.


I´m quite new to Arduino, too.
But I think you misunderstood "Arduino". Arduino is an IDE and maybe you can call it a "philosopy".

When I read your post you think that "arduino" = "AVR microcontoller". But this is not the case.
With the arduino IDE you may be able to program many different microcontrollers: AVR, ESPxx, ARM,....

So different microcontrollers have different access to their pins, for example. And to avoid that you need to learn all the access modes for each microcontroller individually they built high level functions.

This code: "digitalWrite(3,HIGH);" you can use and it will work with every supported microcontroller.
But this code "PORTD |= B00001000;" just works on one type of microcontroller.

For sure this all has benefits and drawbacks.
Arduino is simple to use, but also a bit awkward and not that code effective and processing time effective than it could be.

Good thing is you can use an AVR C compiler and don´t need arduino IDE at all.
You may use other assmbler.
You may use Ardunio, just do what you prefer.

Using Arduino while using very hardware oriented programming style is counter productive in my eyes.

Example 1 is not quite equivalent for timing. It needs to be reorganized so that the test to take pin 3 back low is not within the if statement to take it high. It assumes that the time between taking pin 3 high is longer than 10 microseconds. The example code below assumes all the variables have been declared and pin 3 set to output.

void loop()
      currentMicros = micros();                     // check the current time

      if((currentMicros - previousMicros) >= 7000UL -valor)   //  if time (7000UL - valor) has passed
        previousMicros = currentMicros;     // start timing the the period to the next high pulses here
        previousMicros2 = currentMicros;   // start timing the length of the high pulse here
        PORTD |= B00001000;                         // bring pin 3 high
      currentMicros2 = micros();                  // check the current  time . 
                                    //  During the time waiting for pin 3
                                    // to go high again, the if statement below will continually be setting 
                                    //  pin 3 low each time through to loop. if this is not OK then test to see                                                                             // if it is high before setting it low again.

      if((currentMicros2 - previousMicros2) >= 10UL)  // if 10 microseconds has elapsed since pin 3 high
        PORTD &= !B00001000;                     // set pin 3 low

Example 2 looks to be OK. It may not need the shifts.
Not open for further replies.

Part and Inventory Search

Welcome to