Introduction: Arduino Is Slow - and How to Fix It!
(If you predict you will like this instructable, feel free to vote for the Arduino contest!)
Step 1: The Truth About Pins
Step 2: A and B Watch Out, C Is Here
PORT{letter} &= ~_BV(P{letter}{number});
So, basically, you can replace the entire digitalWrite() command with the above to get a faster response!
Step 3: Exspearimintation
Try out this experiment for yourself, all you need is an arduino and a computer.
Step 4: Overview
I have attached a little cheat sheet below, print it out so it is handy whenever you need it.
Step 5: Going Further
Are you the type of person who wants to know how everything works?
Basically, what happens when you tell a pin to go high or low is that you modify a 8-bit register. (Remember how the pins go from A0-A7, B0-B7? 8 pins per letter, so those 8 pins are toggled by that one register). A register holds 8 bits (Each bit can be 0 or 1).
When you execute the command to put a pin high, the appropriate bit in the register is set to 0. (1 would be low).
Having 8 pins toggled by one register can also have its advantages, mainly that you can toggle any of the 8 pins nearly simultaneously.
For example, if I wanted to turn pins C0 through C6 high and C7 low, the command would be:
PORTC = 0b10000000;
Note how the first pin number coming after the "b" is pin 7, and it goes down from there until pin 0.
0b10000000 is an 8-bit binary number, you can convert it to hex for a cleaner look. Doing it manually is a pain (but useful knowledge), an easier method would be to google "0b10000000 to hex", which results in "0x80".
PORTC = 0x80;
For further reading, see here:
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=37871
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230286016
(thanks gmoon and westfw for the links)
Participated in the
Arduino Contest