Thanks Paul, thanks ofransen.
So, if we could just automatically replace a word like ikRedLed with a value, then why would we use const int ikRedLed = 4?
#define is shorter to write.
And when you say const int is the modern way, then i suppose there must be something more beneficial using const int?
int is a single object, hence in some circs better to use than storing the same value allover the place in your program.
So given that int is sometimes better than #define, why use const? Because we are fallible programmers.
// at the start of the program you may have..
int RedLed =4 ; // This should never change
int RedLedLevel = 22 ; // Brightess of the led which can change
...while programming late Friday night after too much cider I do this:
// Increase brightness...
RedLed = RedLed + 10 ;
Spot the drunken error? Now if you do this:
// at the start of the program you may have..
const int RedLed =4 ; // This should never change
int RedLedLevel = 22 ; // Brightess of the led which can change
...while programming late Friday night after too much cider I do this:
// Increase brightness...
RedLed = RedLed + 10 ;
(note the const) the compiler will complain and fail when you try to change the value of RedLed. If you know something should remain constant, and you know that you make mistakes sometimes, then make it a const object. It is a bit more typing, but it has saved my bacon in the past!
The error message you'll get will be something like:
Serial_Test:18: error: assignment of read-only variable 'RedLed'