variable dilemma on Arduino - Page 1
Forum section: electronics / general

author: Simon
author_rank: Unknown
timestamp: May 24, 2012, 06:18:05 pm
content: 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 = 10To 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?

author: IanJ
author_rank: Unknown
timestamp: May 24, 2012, 06:26:17 pm
content: You are indeed setting a variable that is later used to assign a pin.int ledpin = 10Then 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.Ian.

author: Simon
author_rank: Unknown
timestamp: May 24, 2012, 06:30:21 pm
content: 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.

author: IanJ
author_rank: Unknown
timestamp: May 24, 2012, 07:07:30 pm
content: Hi,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 pinsint DO_Raw[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // digital output pinsSo, 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, INPUT); } // set digital inputs, no pullups requiredfor(int i=0; i<=9; i++) { pinMode(DO_Raw, OUTPUT); } // set digital outputsAnd 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, HIGH);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.Ian.

author: caroper
author_rank: Regular Contributor
timestamp: May 24, 2012, 07:14:34 pm
content: The correct way to handle that should be:#define LEDPin 10Then 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.CheersChris

author: Simon
author_rank: Unknown
timestamp: May 24, 2012, 07:26:56 pm
content: how close is the Arduino programming language to the C programming language? I am reading the Arduino notebook PDF which was linked on Arduino books.com, there is a short paragraph about logical operators, it seems that the standard terms like and, or and not are not used in the code but instead some other symbols like to and symbols together. Surely this is unintuitive?I need to know what those strange symbols mean first but the standard logical operator terms of pretty obvious even to a non-programmer.

author: caroper
author_rank: Regular Contributor
timestamp: May 24, 2012, 07:44:08 pm
content: Arduino is a super-set of C called C++.The actual compiler is perfectly happy compiling C code, so if you want to learn C using an Arduino there is no problem.The advantage of working in C is portability, the advantage of C++ is the Libraries.I am actually a bit torn as to which I prefer, but as the bulk of my coding is for PIC32 and as of this moment Microchip do not provide a C++ compiler, I tend to restrict my code to C even when working on Arduino or ChipKit boards.The symbols you are referring to (Though I have not read that book) are probably the Bitwise logical operators.1 & 1 = 1 (1 AND 1 = 1)1 | 0 = 1 (1 OR 0 = 1)1^1 = 0 (1 Exclusive OR 1 = 0)~1 = 0 (NOT 1 = 0)Hope that helpsCheersChris

author: Simon
author_rank: Unknown
timestamp: May 24, 2012, 07:45:44 pm
content: pinMode(pin, INPUT); // set ‘pin’ to inputdigitalWrite(pin, HIGH); // turn on pullup resistorsthis is part of the explanation of the pin configuration code. I assume that "pin" is a variable ?

author: Simon
author_rank: Unknown
timestamp: May 24, 2012, 07:47:41 pm
content: Quote from: caroper on May 24, 2012, 07:44:08 pmso really I'm learning C++?

author: caroper
author_rank: Regular Contributor
timestamp: May 24, 2012, 08:01:36 pm
content: In a nutshell yes you are learning C++I updated the earlier post to cover your question about the logic symbols.on to the more resent questionsin this statement:pinMode(pin, INPUT);pinMode is a FunctionBoth pin and INPUT are variables that are passed to the function as arguments.In the Arduino INPUT is a Predefined Constant, pin is a variable that you can assign a Pin Number too or just replace in the function call so:int pin = 10;pinMode(pin, INPUT);andpinMode(10, INPUT);would be equivalent, so would the constant defined in my first reply (const int LEDPin = 10;)pinMode(LEDPin, INPUT);CheersChris

author: Simon
author_rank: Unknown
timestamp: May 24, 2012, 08:04:36 pm
content: using pin as a generic variable to replace the pin number is not a great idea, and takes away from the fact that the Arduino has quite a high number of input and output pins. The way the code is taught you would think there is only one input or output pin.

author: caroper
author_rank: Regular Contributor
timestamp: May 24, 2012, 08:10:13 pm
content: Quote from: Simon on May 24, 2012, 08:04:36 pmpin is not a generic variable, it is a place holder for a variable.if you look at the definition of the function pinMode() it probably looks something like this:void pinMode(int pinnumber, int direction){...}pinnumber and direction are the names that the function body will use to address the actual variables or constants that you pass.The use of pin in the documentation is not meant as a variable but as a descriptive place Holder for a variable or constant of your choosing.CheersChris

author: Simon
author_rank: Unknown
timestamp: May 24, 2012, 08:14:36 pm
content: yes I'm starting to see that now. Because the code is so minimalistic there is not a lot to go on. I found basic code quite easy because writing a program in BASIC was not like writing War and peace but there was also enough there for you to understand clearly what was intended.

author: Bored@Work
author_rank: Super Contributor
timestamp: May 24, 2012, 08:16:38 pm
content: Quote from: Simon on May 24, 2012, 07:47:41 pmYes, but in a bad way. The Arduino environment is a wrapper around C++, trying to shield the user from all what the Arduino fashion designer considers evil in the world. I.e. dumbing it down so even the arty people can blink a LED and get a hard one over it.

author: Simon
author_rank: Unknown
timestamp: May 24, 2012, 08:22:53 pm
content: Quote from: Bored@Work on May 24, 2012, 08:16:38 pmyes except that the explanation of the code is so confusing even to one that has some coding experience that to be honest the mission statement is a load of drivel. I really would like to challenge an artist to write some code from scratch after reading some people's idea of an explanation. My guess is that most artists are using the many example sketches or copying code from all over the Internet developed by novice users that have a bit more experience and have been able to hack the instructions and understand them.

author: caroper
author_rank: Regular Contributor
timestamp: May 24, 2012, 08:34:29 pm
content: That is probably an astute observation SimonArduino is a wrapper around C++ which is an extension of C.The Idea behind Arduino is not to give programmers a toy but rather to give non programmers a device that they can pretend to program, or perhaps a better way of saying that is Create Scripts for.The arduino actual refers to them as Sketches.That is not to say you can't use good programming structure on the arduino, just that the documentation will not emphasis it. It tries hard to be as non technical as possible. That means that people such as yourself who expect a more formal approach tend to get lost.Ignore the Ardino documentation, grab a C tutorial and try working from that.CheersChris

author: Simon
author_rank: Unknown
timestamp: May 24, 2012, 08:39:12 pm
content: so really I should of just got a pic and a C compiler? I'm not too fussed which processor I use, as I've not invested that much into pics which is all I know at the moment. I have only programmed with basic, so that is pretty microcontroller independent. Although I found that with the crappy compiler I had before I was having to get more involved with the pic hardware to work out why things were not working as expected.but then I'd need to find a decent C compiler. So perhaps the Arduino is a good bridge, I can hack stuff together with Arduino code and gradually learn C on it.If I am just writing C on it what considerations do I need to make for the hardware? I assume that you learn C within the context of the hardware you are working on.

author: Bored@Work
author_rank: Super Contributor
timestamp: May 24, 2012, 08:47:29 pm
content: Quote from: Simon on May 24, 2012, 08:22:53 pmYes the Arduino world is about copying. More hype than substance. They sell the promise of "you can be an embedded engineer, too". While I don't mind the artist blinking a LED, I really fear those who believe they are an embedded engineer after they managed to blink a LED. In a few years these people might end up on the job market, and some stupid managers might give them jobs in embedded, where they can do some real damage.Quote from: Simon on May 24, 2012, 08:39:12 pmNo, you don't need to. What the Arduino people don't tell you is that the Arduino hardware can be programmed like a normal embedded board with an Atmel AVR microcontroller - because that is what is on the board.They don't tell you this, because then you would step outside of the Arduino universe, where they can sell you overpriced shields and overpriced preprogrammed AVRs.Even the Arduino bootloader in the preprogrammed Arduino Atmel AVRs can be used to load normal C programs into the AVR, and to get them running. So, if you already have an Arduino board, all you need is an AVR C compiler, programming software like avrdude (which knows how to talk to the Arduino bootloader), the schematic of your board (to translate the stupid Arduino pin names into the ones really used by the AVR), and a clue.

author: Simon
author_rank: Unknown
timestamp: May 24, 2012, 08:55:43 pm
content: well people who talk bollocks are pretty easy to spot, then again at my workplace they still employ them. Fortunately I will shortly be moving up the ladder in the workplace due to the merits I have displayed.I might have a look at AVR Dude, perhaps the basic Arduino code system will help me get my foot in the door, and then I can progress straight to C. Once I know C I will become more platform independent.

author: caroper
author_rank: Regular Contributor
timestamp: May 24, 2012, 09:14:27 pm
content: As Bored mentioned, you can still call the arduino library's from C code and it is best to get to know C before you start to worry about the underlying chip and architecture.No need to learn AVRDude etc. at this point, you have all the tools you need already.This should get you started with C on an Arduino:All C code starts executing in the function main(), so the basic structure of all C programs is:Code://includes//defines//prototypes//globalint main(void){// setup........while(1){// program}}The arduino has hidden that structure with one that says:Code:int main(void){setup();while(1) loop();}you can write normal C code and call arduino functions and use arduino constants, just do it within the setup() and loop() functions and remember that main() has been predefined for you, so your program structure would be:Code://includes//defines//prototypes//globalsetup(){// setup code}loop(){// program body}There is a section on the Arduino Playground that talks about native C.It may be a bit advanced for you at this stage but it has some good links.You will probably get more out of Arduino Playground than you will from Arduino.ccCheersChris

author: graynomad
author_rank: Regular Contributor
timestamp: May 27, 2012, 03:21:48 am
content: The Arduino "code" is simply a library like any other, the environment is C/C++ with some help to include that library. I think they should make that clearer as I almost rejected Arduino because I though the language was brain dead until I realised that.So an Arduino is the same as any other embedded processor board, except there's some abstraction in the IDE environment for newbies, you can get down and dirty with a makefile, Emacs, an ISP programmer or some assembly language if you want.All this means that you can start without knowing too much about the details and slowly do more as you learn.______Rob

author: electrode
author_rank: Regular Contributor
timestamp: May 27, 2012, 05:06:28 am
content: Notes:- Bored@Work's family was brutally murdered (allegedly) by the creators of Arduino, so there's a lot of opinion and hate there. No need for facts to get in the way.- C++ is NOT a (strict) superset of C.- Arduino's environment is not exactly C++, in that you can only create classes by creating them as libraries, including those libraries, and instantiating objects that way.- No, you're not "technically learning C++", really. Not yet. Everything you've asked about so far is pretty well just C.- Constant variables (or preprocessor #defines) are much better for readability than magic numbers throughout your code.

author: pkrobot
author_rank: Contributor
timestamp: May 28, 2012, 02:16:19 am
content: Arudino is a wonderful learning environment. I normally use Arduino to introduce people to embedded software development. Within the Arduino environment, you can introduce people to basic programming constructs in C/C++, embedded concepts like IDE, target/host, bootloader, interrupts, timers etc. It will be quite a while before you outgrow the Arduino environment for your projects. If Atmega328 is not enough for you, you can consider other AVR microcontrollers to which Arduino environment/ bootloader has been ported (all 8-bits, though), like Atmega1284P etc.Also, Arduino is not a money-making conspiracy, as suggested by someone here. It is based on the concept of open source hardware. Info is available for all the shields, and if you want, you can make your own.Arduino provides a library. pinMode(), for example, is an API call from this library, to which you can either pass the value by variable, or as a constant. When you are learning about any higher level embedded platform, you'll have to learn the API also that is provided by the vendor. Otherwise, if you try to do everything from scratch, you won't go far.-pk

author: graynomad
author_rank: Regular Contributor
timestamp: May 28, 2012, 02:37:53 am
content: QuoteNot for much longer, the 32-bit Due has been released to beta testers and should be available soon.______Rob