Go Down

Topic: using integer type to define pins, why? (Read 2 times) previous topic - next topic

Nick Gammon

why do all the examples use variables to define pins? Is this a wast of RAM? isn't it slower than a constant?

No and no. Provided it is a const variable.

Some programmers may not put const there, not seeing the point. Without it, what you say is correct.

James C4S

you sure ? I pulled that from the Knob example under the servo menu. Im not 100% sure but I think A0 is the first pin on my mega

When using analog pins is it better to use the Ax pin numbers instead of just "0".  This makes the code portable and makes it very clear when reading it what pin you intended to use.

const int potpin = A0;

This is the best method.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com


Just awesome! you guys are very helpful


The only additional question I have about pin numbers is why they are int instead of byte type?

Seems a wasted type size and there can be no such thing as a negative pin number, so maybe unsigned int rather then int, but still byte would seem to be more space saving?


Nick Gammon

Personally I use byte, because pin numbers are indeed unsigned, and don't go over 255.

Go Up