Electronics > Microcontrollers

variable dilemma on Arduino

(1/5) > >>

I have found a little more information about the Arduino code other than the mess presented on the website. However I am still confused over variables and pin assignments.

int ledpin = 10

To me this means that the variable LEDpin is to be Assigned the number of 10. But apparently it means that pin 10 on the microcontroller is being selected for the LED output. is it just me or are there to interpretations to this command, int is a variable since when is a pin number a variable.

or is ledpin some sort of Constant or keyword in the programming language?

You are indeed setting a variable that is later used to assign a pin.

int ledpin = 10

Then in void setup you would have,

pinMode(ledpin , OUTPUT);

Then in void loop you might use it as follows,

digitalWrite(ledpin , HIGH);

So you don't need the variable "ledpin" at all, you could just use "10" instead throughout.

Hope that helps.


I see, and this is the problem with people trying to explain how the programming language works. A concept was being explained which was how to use an array, and instead of just explaining how to use an array with a simple piece of code this was also thrown at me. In fact it makes no sense at all to create a variable with a number only to use that variable to determine which pin is going to be the output, it just causes complete confusion when I am trying to understand another concept.


When you have a big project with many, many pins then it's MUCH easier to refer to particular I/O by way of a variable name than numbers. Thats what high level languages are all about.

On the the other hand, you might indeed use an array to assign a bunch of I/O pins. Take this example from one of my own programs:

--- Code: ---int DI_Raw[] = { 14, 15, 16, 17, 18, 19, 22, 23, 24, 25 };  // digital input pins
int DO_Raw[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };            // digital output pins

--- End code ---

So, we are back to numbers right!........but in this case you have 20 pins assigned in just 2 lines (instead of 20).

And since it's an array you can set them up all the easier in void setup, again 2 lines instead of 20:-

--- Code: ---for(int i=14; i<=25; i++) { pinMode(DI_Raw[i], INPUT); }    // set digital inputs, no pullups required
for(int i=0; i<=9; i++) { pinMode(DO_Raw[i], OUTPUT); }     // set digital outputs

--- End code ---

And then you might use one of them as follows, by simply setting the var i be it directly or as part of a for-next loop etc:-

--- Code: ---digitalWrite(DO_Raw[i], HIGH);

--- End code ---

Note: the other flexibility of the array is that you can swap pins around by moving them around in the array definition.

Hope that helps.


The correct way to handle that should be:

#define LEDPin 10

Then at compile time wherever LEDPin occurs in the text, it would be replaced by the number 10.
The Arduino Pre-compiler, however, has some bugs that makes that syntax dangerous.

The correct alternative would be to say:

const int LEDPin = 10;

But people get lazy, omit the const keyword and cause confusion.

You are not alone.



[0] Message Index

[#] Next page

There was an error while thanking
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod