setting a port High. Is it realy that Cumbersome?

Hi Guys,

Maybe it is because I haven't looked further than the very basic ways to set a port High, but is it really that cumbersome to set a port high, in the sketch??

       digitalWrite (BuzzPin, HIGH);      //buzzer on
       delay (200);
       digitalWrite (BuzzPin, LOW);        //buzzer off
       delay (800);
       if (i > 4){           //after 5 seconds
        i = 0;             //reset i
        digitalWrite (BuzzPin, HIGH);      //buzzer 1 second on
        delay (1000);
        digitalWrite (BuzzPin, LOW);

What are you going on about?

-jim lee

I don't understand what you mean by cumbersome.

If your fingers are tired by all that typing, how about

#define dwH(x) digitalWrite((x), HIGH)
define dwL(x) digitalWrite((x), LOW)

?

TheMemberFormerlyKnownAsAWOL:
If your fingers are tired by all that typing, how about

#define dwH(x) digitalWrite((x), HIGH)

define dwL(x) digitalWrite((x), LOW)


?

HAhaha! I find myself adding this to a lot of my code

#define out Serial.print
#define outln Serial.println

-jim lee

PerryBebbington:
I don't understand what you mean by cumbersome.

Google translate would be my friend, but it seems it has forsaken me... :slight_smile:

I wonder why putting a port High is so complex. So much effort for just setting 1 port high...

TheMemberFormerlyKnownAsAWOL:
If your fingers are tired by all that typing, how about

#define dwH(x) digitalWrite((x), HIGH)

define dwL(x) digitalWrite((x), LOW)



?

This looks promising...
Could you explain what it does?

Well… You are actually giving examples of setting a pin high. Not a port.

digitalWrite(x, HIGH);

Ok lets see…

digitalWrite <<-- The kind of thing we want to do.
x <<-- The pin we want to do it to.
HIGH <<-- What we want to do to that pin.

How would you like to lay this out in a better way?

Err… Google translate? What language are you translating?

-jim lee

FTMZ:
I wonder why putting a port High is so complex. So much effort for just setting 1 port high.

I know what the words mean! I don't have the slightest clue why you think it's cumbersome.

So much effort for just setting 1 port high...

Pfffft. You should have seen how things were done in the old days.

This sets PORTD, pin 3 HIGH. Naturally, the pin must be configured as OUTPUT.

PORTD |= 1<<3;

How’s that? Still too cumbersome?

Well... In slightly lesser old days, I was used to use:

TRIS.A 00000011 to get the A port with the first 2 ports as input and the other ones output.

Then by using (I recall) Symbol PortA.1 = Bell to name the second bit of the A port "Bell"

So when I wanted something to happen when Port A1 was high, I'd say:
If Bell = 1 then ...

Extremely simpel and handy.

Of course for some reasons the Arduino does that differently and I was wondering why that was.

IG:
If I have a short loop to hold the program until a certain port is set high, I need to:

*digitalRead (buttonPin) *

While (buttonpin = 0){

digitalRead (buttonPin)

//Do something while waiting for the button to be pushed

}
(I might be slightly off here in the correct use of the codes, but I hope you catch my drift.) :slight_smile:

Not to mention al the tricks and settings I need to set before I can even do something related to that port beeing an input.
For every equation I first need to get he arduino to check the port using a quite "cumbersome" rule of code.

There is no aliassing for ports. :confused:

@JimmyLee
Google translate was because I could not find the right translation for the dutch word "Omslachtig".
When PerryBebbington mentioned he did not get the "Cumbersome" thing, I thought Google might had given me the wrong translation. :wink:

And last but not least,
I indeed was mistaken in calling a port while mentioning a specific pin on that port.
Sorry for that... :frowning:

Perhaps changing the Arduino Uno core to meet with your idea of ease of use would be in order? That way you can contribute to improving the Arduino Platform.

And whiles your at it could you work on a way to make it easier to set up and write to an ESP32 GPIO port using the API?

void setup()
{
  gpio_config_t io_cfg = {}; // initialize the gpio configuration structure
  io_cfg.mode = GPIO_MODE_OUTPUT; // set gpio mode
  io_cfg.pin_bit_mask = ( (1ULL << GPIO_NUM_4) ); // assign gpio number to be configured
  gpio_config(&io_cfg); // configure the gpio based upon the parameters as set in the configuration structure
  gpio_set_level( GPIO_NUM_4, LOW); // set air particle sensor trigger pin to LOW
}

And that’s just to set one pin as output.

I’d say setting a pin up by using pinmode it quite a simplification over the direct method but then again there is speed and code size to consider.


If you got a better way show us.

Of course for some reasons the Arduino does that differently and I was wondering why that was.

It's very simple actually. You were using the PIC which has native single bit manipulation instructions, most processors including the AVR do not. So it is not possible to code the constructs you presented.

FTMZ:
Well… In slightly lesser old days, I was used to use:

TRIS.A 00000011 to get the A port with the first 2 ports as input and the other ones output.

Ah! Yes, and all the hoops you had to jump through because PIC assembler doesn’t have a proper computed goto.

I’ll stick with AVR’s C++, thanks.

There is no aliassing for ports.

I have no idea what that means.

aarg:
It's very simple actually. You were using the PIC which has native single bit manipulation instructions, most processors including the AVR do not. So it is not possible to code the constructs you presented.

Well, there's a good reason, indeed. :slight_smile:

I'll find some tricks to get the hang of it. But stepping up from PIC to Arduino is somewhat more complex, I realize.

But just to solve the reason for this post in the first place...
If I was to wait for a button to be pushed before continuing the sequence of the code, I'd normally use something like:

While ([button is not pushed]) {
//Wait patiently...
}

What other way would you recommend?

Don't wait for the button to be pressed. Check the status of the button, if it's pressed do something, if not move on and do something else.

PerryBebbington:
Don't wait for the button to be pressed. Check the status of the button, if it's pressed do something, if not move on and do something else.

I was just going to type something similar. +1

FTMZ:
If I was to wait for a button to be pushed before continuing the sequence of the code, I'd normally use something like:

While ([button is not pushed]) {
//Wait patiently...
}

What other way would you recommend?

File->Examples->02.Digital->StateChangeDetection
File->Examples->02.Digital->Debounce

If you WANT your sketch to stop everything until the button is pressed:

const byte ButtonPin = 4;
void setup()
{
  pinMode(ButtonPin, INPUT_PULLUP); // Active LOW (button switch between pin and ground
}
void loop()
{
  // Wait for button to be pressed
  while (digitalRead(ButtonPin) == HIGH) {}
}

Running an ESP32 and using freeRTOS, I'd set up button press task, that periodically checks the register state of the GPIO port/pin. Checking the GPIO_NUM_X pin once every 3mS or 7mS or so would allow other tasking code to run and still be very responsive to button presses.

Idahowalker:
... Checking the GPIO_NUM_X pin once every 3mS or 7mS or so would allow other tasking code to run and still be very responsive to button presses.

I had something simulair in mind.
But in my imagination it would be a little script which will start at the change of status of an input pin.
So when the pin changes in value, the script then will adjust the required settings, and therefore giving a variable the most recent value for me to work with.

If this sounds stupid, let me know.
I'm quite a layman in all of this and keep in mind that I don't use very fast operating projects. 8)
I have the luxury of keeping the arduino on pauze, waiting for me to release (or in other cases, push) a button.

 while (digitalRead(ButtonPin) == HIGH) {}

For now this is something very usefull!!
It keeps me from checking the (digitalRead(buttonpin)) before and in the loop.