int ledPin = 13

@OldSteve yes, I made the correction, thanks.

Hi Nick,

I didn't look at any C/C++ tutorials - I've been looking at Arduino tutorials. Are you saying that I need to learn about C/C++ programming before I can hope to understand Arduino sketches? If so it explains a lot of the trouble I'm having.

Regards, Alan

You do know your sketches are written in C/C++
:wink:

Are you saying that I need to learn about C/C++ programming before I can hope to understand Arduino sketches?

It depends on what "depth of understanding" you are looking for. Certainly, to understand ALL arduino sketches, in detail, you will need to learn about C/C++ programming. You'll learn SOME about C/C++ programming from Arduino tutorials. But you'll never (I hope) get exposed to the differences between a globally scoped standard fixed-width integer type, and a locally scoped indeterminate-width type, and stuff like that.

The very first line of programming anyone comes across is: int ledPin = 13;

Really? Exactly what code are you looking at? In 1.0.x, "Blink" has

// Pin 13 has an LED connected on most Arduino boards.
   :
// give it a name:
int led = 13;

That seems to be a relatively clear explanation ("give pin 13 a name"), at one level of detail. You can go a fair distance without understanding it in any more detail than that. And despite what people say, it is usually NOT important to use "const byte" instead of "int." Yes, you might end up using more memory than you would strictly need to, and be slightly slower than you could be. But usually that doesn't matter. (and yes, if you want to write the smallest fastest code, you'll need to learn about C/C++ programming.

In 1.6.0, the blink example has gotten rid of the extra level of definition of "led"; it just sticks "13" in each line where it is needed. Perhaps in response to complaints such as yours. Simpler, I guess. But defining names for your pins ("led", "ledPin", etc) is "good programming practice", and I'm a bit sorry to see it go.

westfw: And despite what people say, it is usually NOT important to use "const byte" instead of "int." Yes, you might end up using more memory than you would strictly need to, and be slightly slower than you could be. But usually that doesn't matter.

There is absolutely no reason to use up RAM by declaring these values as int, but there is a reason to declare them as constants. They are constants, if they're not going to be changed.

You say it's good programming practice to name pins. It's also good programming practice to not waste RAM for no reason whatsoever. It doesn't just apply to pins. People get in the habit of storing every fixed value as an int, instead of as constants. I see it all the time, and you must see it too - bad programming practice.

Let's do away with defines and constants altogether, and store everything in RAM just to make it a bit easier for beginners.

It's also good programming practice to not waste RAM for no reason whatsoever.

There are always reasons. High level "clarity" is one; "int" is supposed to be the generic "don't really care" data type in C. "char" is certainly confusing - it's not a character! "uint8_t" isn't beginner-friendly. "byte" isn't standard. On ARMs, expressions using values smaller than "int" are likely to evaluate more slowly, in contradiction to what many 8-bit programmers are used to.

Blink.ino is not ever going to be the most efficient way to blink an LED connected to an AVR; whether it uses "int ledPin =13;" or "const uint8_t ledPin=13;" or "#define ledPin 13" really doesn't matter.

OldSteve: People get in the habit of storing every fixed value as an int, instead of as constants.

Thats not an alternative but a totally different story.

char vs ints vs long and there unsigned sisters control the range the variable or constant can hold.

So a pin declaration as byte wastes one byte less than a declaration as int.

const hinders you from changing the value and uses no RAM at all.

On an 8-bit processor, 8 bit variables process quicker and take up fewer registers.

Kiwi2: I didn't look at any C/C++ tutorials - I've been looking at Arduino tutorials. Are you saying that I need to learn about C/C++ programming before I can hope to understand Arduino sketches? If so it explains a lot of the trouble I'm having.

There is a "sticky" at the start of the Programming section of the forum that we kind-of hope that people will read. Read this before posting a programming question ...

The first point reads:

  1. Arduino programming language

The Arduino basically uses the industry-standard C++ language, implemented by the GNU g++ compiler.

For information on basic things like loops, strings, functions, data types, and so on try consulting one of the many online C++ references. Hint: Google "C++ tutorial".

I can't say exactly what you need to read before understanding Arduino sketches, but the suggestion of reading a C/C++ tutorial has merit.

process quicker and take up fewer registers.

"Premature optimization is the root of all evil."

Premature optimization

That is a problem with virgins.

.

LOL 8)

Hi Nick,
I’m sorry, I didn’t read the “sticky” at the start of the Programming section - I should have done. I didn’t know that Arduino sketches use C++.
The comments I have made regarding the tutorials weren’t made with the object of having a go at the people who wrote them - rather they were in the nature of a thank you to the forum for clearing up the ins and outs of the line:

int ledPin = 13

I gather that the purpose of this forum is to promote the use of Arduino boards. The point I was trying to make was that I - a reasonably intelligent person with however no programming knowledge - fell at the first line of code I came across because it’s construction wasn’t clearly and simply explained. I suspect that I’m not alone and I would guess that a lot of potential users give up and don’t go on to further explore the Arduino boards.
If it’s not possible to come up with a series of tutorials which are useful to people with no prior programming knowledge then that’s OK but it’s excluding a lot of potential users.
Thanks to the reply by DrAzzy I feel I understand the nature of the line and have had a lot of fun erasing it from the Blink program and getting it to run by replacing ledPin with 13 in the setup and loop parts of the sketch. I got a real kick when it ran.
So thanks to all in the forum who took the trouble to reply.

Regards,
Alan

I gather that the purpose of this forum is to promote the use of Arduino boards.

Really, how sad.

Kiwi2: The point I was trying to make was that I - a reasonably intelligent person with however no programming knowledge - fell at the first line of code I came across because it's construction wasn't clearly and simply explained.

You can lead a horse to water..........

One section of the Foundations Page that explains so much for people who take the time to look.

Programming

Language Reference: Discover the what each term of the Arduino code language means. Variables: Understand how to define and use variables in a Sketch. Functions: Learn how to define and use functions in a Sketch. Library: Using and installing Arduino Libraries. Cores: Need to add a new board to the Arduino Software? Install the relate core and manage it.

Looking for help on how to extend the Arduino Software (IDE), like write your own Library? Check out the Hacking section.

Why have people spend hours typing out what is already done and instantly available?

That section shows you the Reference Page so you can bookmark it.

You do make bookmark folders to make it easy to find your links again? So make a folder named Arduino Site in your bookmarks and put in links to site pages you may need later. You don't have to read more than see what it is about and then bookmark it. Then when you open up the IDE also open up your browser and as many tabs in it as you will need to keep references open, like the Reference Page and the a Library Page The internet has your manuals, keep them open as you look at, change or write code, you can check every bit faster than ask every bit and get answered.

You really need to read the sections Variables, Functions, and Libraries.

In your IDE are just for getting beginners started, a number of Tutorial Examples. Every library is supposed to contain working examples too. But if you don't know how variables work then that's something you need to find out first, learn the alphabet before learning to read words.

Kiwi2: I gather that the purpose of this forum is to promote the use of Arduino boards.

If you scratch some paint off a house, is the house then made of paint?

By the way, code is written in logic so I hope you have no trouble with analogy.

The point I was trying to make was that I - a reasonably intelligent person with however no programming knowledge - fell at the first line of code I came across

Up to here you were doing okay. You should have gone with because I didn't take the time to learn what the terms mean. But instead you fault the language for your failure to read the existing explanations.

I graduated HS years before there was DARPA let alone the WWW. We learned to look things up when it meant going to a building miles away to do it. What is with people today who don't look things up when it's right there at hand? How intelligent is THAT? How real?

because it's construction wasn't clearly and simply explained. I suspect that I'm not alone and I would guess that a lot of potential users give up and don't go on to further explore the Arduino boards.

It takes gumption and work to write code. Those who can't, won't, even when it's can't be bothered.

If it's not possible to come up with a series of tutorials which are useful to people with no prior programming knowledge then that's OK but it's excluding a lot of potential users.

That's why the Learning Section has the Foundations Page you were going to look into. That's why so much of the official site is dedicated to explaining and showing and referencing.

If you are reading code and you don't know for sure about ant command, look it up right then.

Kiwi2: Hi Nick, ...

int ledPin = 13 ... I gather that the purpose of this forum is to promote the use of Arduino boards..

That's certainly not my purpose here. For one thing, none of the contributors here are paid by Arduino (very rarely we get posts by Arduino staff, but they are usually clearing up supply details or something like that).

Most people here are simply posting to try to help others solve problems with their Arduinos, or indeed circuits made from similar devices (like the Atmega328 processor, even if not inside an Arduino).

Your post rubbed me up the wrong way a bit, because it is like going onto a cooking forum and saying:

Hey guys! I am just starting to learn to cook. I stumbled on the first line of this recipe:

3 cups flour

Can you guys break this down for me? What does "3" mean? What is a "cup"? What is "flour"?

I've looked everywhere for an explanation of this with no joy.

You are asking for someone to write a tutorial on the absolute basics. There is no indication you read the "sticky" to find that the language you are trying to learn is C++, nor that you tried to find any tutorials on it.

Imagine yourself on this hypothetical cooking forum as a regular contributor. Would you sit down and write yet another tutorial on how to get started on cooking, or would you suggest to the poster that s/he does a bit of research first?

Succinct.