Author Topic: variable dilemma on Arduino  (Read 8663 times)

0 Members and 1 Guest are viewing this topic.

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
variable dilemma on Arduino
« on: May 24, 2012, 06:18:05 pm »
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?
 

Offline IanJ

  • Supporter
  • ****
  • Posts: 1608
  • Country: scotland
  • Full time EE & Youtuber
    • IanJohnston.com
Re: variable dilemma on Arduino
« Reply #1 on: May 24, 2012, 06:26:17 pm »
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.

Ian.
« Last Edit: May 24, 2012, 06:27:52 pm by IanJ »
Ian Johnston - Original designer of the PDVS2mini || Author of the free WinGPIB app.
Website - www.ianjohnston.com
YT Channel (electronics repairs & projects): www.youtube.com/user/IanScottJohnston, Twitter (X): https://twitter.com/IanSJohnston
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: variable dilemma on Arduino
« Reply #2 on: May 24, 2012, 06:30:21 pm »
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.
 

Offline IanJ

  • Supporter
  • ****
  • Posts: 1608
  • Country: scotland
  • Full time EE & Youtuber
    • IanJohnston.com
Re: variable dilemma on Arduino
« Reply #3 on: May 24, 2012, 07:07:30 pm »
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: [Select]
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

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: [Select]
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

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: [Select]
digitalWrite(DO_Raw[i], 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.
Ian Johnston - Original designer of the PDVS2mini || Author of the free WinGPIB app.
Website - www.ianjohnston.com
YT Channel (electronics repairs & projects): www.youtube.com/user/IanScottJohnston, Twitter (X): https://twitter.com/IanSJohnston
 

Offline caroper

  • Regular Contributor
  • *
  • Posts: 193
  • Country: za
    • Take your PIC
Re: variable dilemma on Arduino
« Reply #4 on: May 24, 2012, 07:14:34 pm »
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.


Cheers
Chris







Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: variable dilemma on Arduino
« Reply #5 on: May 24, 2012, 07:26:56 pm »
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.
 

Offline caroper

  • Regular Contributor
  • *
  • Posts: 193
  • Country: za
    • Take your PIC
Re: variable dilemma on Arduino
« Reply #6 on: May 24, 2012, 07:44:08 pm »
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 helps


Cheers
Chris







« Last Edit: May 24, 2012, 07:51:12 pm by caroper »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: variable dilemma on Arduino
« Reply #7 on: May 24, 2012, 07:45:44 pm »
pinMode(pin, INPUT); // set ‘pin’ to input
digitalWrite(pin, HIGH); // turn on pullup resistors

this is part of the explanation of the  pin configuration code. I assume that "pin" is a variable ?
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: variable dilemma on Arduino
« Reply #8 on: May 24, 2012, 07:47:41 pm »
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.


Cheers
Chris

so really I'm learning C++?
 

Offline caroper

  • Regular Contributor
  • *
  • Posts: 193
  • Country: za
    • Take your PIC
Re: variable dilemma on Arduino
« Reply #9 on: May 24, 2012, 08:01:36 pm »
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 questions
in this statement:

pinMode(pin, INPUT);

pinMode is a Function

Both 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); 

and

pinMode(10, INPUT); 

would be equivalent, so would the constant defined in my first reply (const int LEDPin = 10;)

pinMode(LEDPin, INPUT);

Cheers

Chris







« Last Edit: May 24, 2012, 08:05:31 pm by caroper »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: variable dilemma on Arduino
« Reply #10 on: May 24, 2012, 08:04:36 pm »
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.
 

Offline caroper

  • Regular Contributor
  • *
  • Posts: 193
  • Country: za
    • Take your PIC
Re: variable dilemma on Arduino
« Reply #11 on: May 24, 2012, 08:10:13 pm »
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.

pin 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.

Cheers
Chris
« Last Edit: May 24, 2012, 08:12:51 pm by caroper »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: variable dilemma on Arduino
« Reply #12 on: May 24, 2012, 08:14:36 pm »
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.
 

Offline Bored@Work

  • Super Contributor
  • ***
  • Posts: 3932
  • Country: 00
Re: variable dilemma on Arduino
« Reply #13 on: May 24, 2012, 08:16:38 pm »
so really I'm learning C++?

Yes, 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.
I delete PMs unread. If you have something to say, say it in public.
For all else: Profile->[Modify Profile]Buddies/Ignore List->Edit Ignore List
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: variable dilemma on Arduino
« Reply #14 on: May 24, 2012, 08:22:53 pm »
I.e. dumbing it down so even the arty people can blink a LED and get a hard one over it.

yes 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.
 

Offline caroper

  • Regular Contributor
  • *
  • Posts: 193
  • Country: za
    • Take your PIC
Re: variable dilemma on Arduino
« Reply #15 on: May 24, 2012, 08:34:29 pm »
That is probably an astute observation Simon :)


Arduino 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.


Cheers
Chris


Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: variable dilemma on Arduino
« Reply #16 on: May 24, 2012, 08:39:12 pm »
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.
 

Offline Bored@Work

  • Super Contributor
  • ***
  • Posts: 3932
  • Country: 00
Re: variable dilemma on Arduino
« Reply #17 on: May 24, 2012, 08:47:29 pm »
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.

Yes 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.

so really I should of just got a pic and a C compiler?

No, 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.
« Last Edit: May 24, 2012, 08:49:02 pm by Bored@Work »
I delete PMs unread. If you have something to say, say it in public.
For all else: Profile->[Modify Profile]Buddies/Ignore List->Edit Ignore List
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: variable dilemma on Arduino
« Reply #18 on: May 24, 2012, 08:55:43 pm »
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.
 

Offline caroper

  • Regular Contributor
  • *
  • Posts: 193
  • Country: za
    • Take your PIC
Re: variable dilemma on Arduino
« Reply #19 on: May 24, 2012, 09:14:27 pm »
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: [Select]


//includes
//defines
//prototypes
//global

int main(void)
{
// setup
....
....
    while(1)
    {
         // program
     }
}

The arduino has hidden that structure with one that says:

Code: [Select]
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: [Select]

//includes
//defines
//prototypes
//global


setup()
{
    // 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.cc

Cheers
Chris
« Last Edit: May 24, 2012, 09:34:02 pm by caroper »
 

Offline graynomad

  • Regular Contributor
  • *
  • Posts: 54
  • Country: au
Re: variable dilemma on Arduino
« Reply #20 on: May 27, 2012, 03:21:48 am »
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
Graynomad, AKA Rob Gray www.robgray.com
 

Offline electrode

  • Regular Contributor
  • *
  • Posts: 141
  • Country: au
Re: variable dilemma on Arduino
« Reply #21 on: May 27, 2012, 05:06:28 am »
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.
 

Offline pkrobot

  • Contributor
  • Posts: 12
  • Country: us
Re: variable dilemma on Arduino
« Reply #22 on: May 28, 2012, 02:16:19 am »
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
 

Offline graynomad

  • Regular Contributor
  • *
  • Posts: 54
  • Country: au
Re: variable dilemma on Arduino
« Reply #23 on: May 28, 2012, 02:37:53 am »
Quote
(all 8-bits, though)
Not for much longer, the 32-bit Due has been released to beta testers and should be available soon.

______
Rob
Graynomad, AKA Rob Gray www.robgray.com
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf