Not using digitalWrite and so on for faster execution...but why doesn't work?

Hi,

I am modifying original ps2.h library for faster execution. I have noticed the original library uses a lot of digitalWrite(), pinMode() and so on which I believe slows the Arduino. So I will want to go for port manipulation. Anyway, here's two questions I would like to ask:

  1. For example, in the .cpp file, I am changing these two lines :

pinMode(34, INPUT);
digitalWrite(34, HIGH);

to :

DDRC &= ~_BV (3);
PORTC |= _BV (3);

I know digital pin 34 in Arduino Mega belongs to PC3. And the new codes come from the posts by nickgammon.

For me, it seems nothing wrong, but why it doesn't work?

  1. There are plenty of delayMicroseconds() functions in the original .cpp file. Since it is in .cpp file, will those delay functions causes delay to void loop() too?

Please help. Thank you

It may come as a surprise to you, but we can't see your code

For me, it seems nothing wrong, but why it doesn't work?

What DOES the code actually do?

Post the ACTUAL code that illustrates the problem.

There is a digitalWriteFast library which is almost as good as port manipulation and a great deal easier to use. (The zeal of the recently converted :slight_smile: )

However it can only be used if you know the pins that are to be used at compile time - and I suspect you do.

delayMicroseconds() will delay everything. In the final program all the files become merged into one by the compiler and linker so it does not matter where the delay() is in the written code.

Replacing delay() or delayMicroseconds() with non-blocking millis() or micros() (as illustrated in Several Things at a Time) could be a major task in a large program that you did not write yourself. It would probably require a big re-design,

...R

You could make this shorter:

pinMode(34, INPUT);
digitalWrite(34, HIGH);

by changing it to:

pinMode(34, INPUT_PULLUP);

If this is used only in an initialisation then it's hardly worth optimising with direct port manipulation commands.