Arduino - general tutorials/lessons on coding? (C/C++?)

Howdy!

I've been loving the Arduino, and have been learning how to do some things with it. I have been able to get some interesting projects set up, but they (to me) are still relatively simple. I am now trying things that are using more arrays, libraries, and have started to come across pointers. I would like to learn more about these, and get a better handle on the fundamentals (ints, strings, using #define as opposed to int =, etc.). Most of my learning has been from the Project book from the Arduino Starter Kit, and looking at other Arduino examples. I have also been using the Arduino language reference page, but am looking for something a little more guided.

I was just wondering if anybody has any good tutorial websites, YouTube videos, books, etc. they could recommend to learn the code/theory better - as opposed to piecemealing it together from Arduino projects and examples as those only get me so far before I run into a wall. I don't know C/C++, so I am assuming that I should look in to that. But, I'm so new, I don't know if I should learn C first, then C++, or if they're "the same" only one more capable than the other...

Anyways, any general ideas/advice would be appreciated! Thanks!

I don't know C/C++, so I am assuming that I should look in to that.

Yes, you should.

But, I'm so new, I don't know if I should learn C first, then C++, or if they're "the same" only one more capable than the other...

C++ (C = C + 1 in other languages) builds upon C. A solid foundation in C, and an understanding of what object oriented programming means and why it is useful, will make learning C++ easy.

Forget tutorials and you tube videos. Go to a bookstore or library and get a book. The bigger, the better, usually. Get more than one. Each will have a slightly different approach/viewpoint. One of them will, eventually, be a better match to your learning style/background.

While it is not intended to teach C/C++ this Thread provides guidance about planning and implementing an Arduino program which is also a necessary part of the overall learning experience.

It may also provide a framework in which you can exercise your language skills.

Keep in mind that many (most) computer language textbooks are written for PC programming where there is virtually unlimited RAM and hard disk space so some of the stuff won't be relevant for an Arduino. The use of Strings (with a capital S) is a good example. On the Arduino you are better using strings with a small s.

...R

PaulS:
The bigger, the better, usually.

Agreed. OP - here are two rare exceptions worth looking at:
The C programming language.
C++: the core language.

I think most beginners find it easier to learn C first and then take on C++. Paul's statement:

The bigger, the better, usually.

has the right caveat on it: usually. One of the brightest coders I ever knew could take over an hour telling you how to add two numbers together. By the time he finished making his point, the student's eyes would be totally glazed over. Lesson: Great coders don't necessarily make good teachers.

As far as books, my suggestion is to go to Amazon and search for a topic like "C, Arduino" and read the reviews. The search will produce quite a few books for you to consider and read the reviews for those that have at least four or five reviews. In an unabashed plug, my book, Beginning C for Arduino is written for the reader who has zero prior programming experience. It's distilled from teaching university-level programming courses since my first C book back in 1982 (The C Programming Guide). Once you are comfortable with C, use the same process to tackle C++ and OOP.

Lesson: Great coders don't necessarily make good teachers.

My thought was that bigger books have more examples. Showing the syntax for a function, and a brief explanation is one thing. Showing two or three examples of how to use, and not use, the function is orders of magnitude more helpful.

How many times do we get people here that say they can read and understand code, but can't write it?

Big books usually have more examples than little books.

Big books usually have more examples than little books.

I was hoping bigger books have bigger print. :wink:

Paul:

Big books usually have more examples than little books.

And that's a good thing when it's true. However, all of us have also seen "big books" that are little more than a repeat of the documentation that comes with the software (e.g., an online tutorial supplied by the vendor). In other cases, a lot of examples is a "shotgun approach" where the writer hopes that one of the examples "sticks". However, there are other cases where one or two well-chosen examples can get you from point A to point D by carefully planning of the steps along the way. If you have 150 pairs of eyes staring at you like deer in the headlights, you've probably picked a bad example. Using different examples in the classroom can help to refine the examples list to a point where the book is of manageable size for a one semester class. True, there will still be some who "don't get it" for any given example, but usually they can Google the topic and find plenty of other examples that might suit their learning needs.

The books I consider good books are ones like Visual C# 2005 How to Program, by Deitel and Deitel. Lots of examples on how to do things like read from, or write to files. The examples build on things from previous chapters, with the new stuff highlighted. If you've been doing the exercises from previous chapters, it's easy to see how this new set of features integrates with what you already know. If you haven't, or are just looking for an example of reading a text file, there is enough information in the text and examples to get you going.

The book is almost 1600 pages, so it feels overwhelming at first. But, once you see how many examples are provided, and can be skipped if you "get it" (or don't think you'll ever use the feature), the book shrinks to manageable size.

However, there are other cases where one or two well-chosen examples can get you from point A to point D by carefully planning of the steps along the way.

That's great, if that's the route you want. But, if you want to start from point B... But, I understand your point.

The hard part, when writing a book, is knowing just what the audience is going to bring to the party. Assume too much, and the book is worthless. Assume too little, and the book teaches nothing (new).

If you have 150 pairs of eyes staring at you like deer in the headlights, you've probably picked a bad example.

Tough to see all them Bambis when you are writing a book.

Using different examples in the classroom can help to refine the examples list to a point where the book is of manageable size for a one semester class.

Yep. Especially if the example is from a somewhat different point of view or is related to homework or is more familiar to the students.

The hard part, when writing a book, is knowing just what the audience is going to bring to the party. Assume too much, and the book is worthless. Assume too little, and the book teaches nothing (new).

Absolutely true, which is why the author must state clearly on page 1 who the intended audience is. Stores like Amazon allow you to read a few pages from the book, and page 1 should be one that a prospective buyer should read to see if it matches their expectations.

Tough to see all them Bambis when you are writing a book.

Tough to see them, but not so tough remembering them. Also, classroom pre-testing of different examples as you write the book also helps.

Especially if the example is from a somewhat different point of view or is related to homework or is more familiar to the students.

Also very true. Each chapter should have a set of exercises that allow the reader to apply the concepts of the chapter to the exercises, but in a way that is a little different than what they read in the chapter. Also, the answers should not immediately follow the exercise...the answer should be somewhere else. Otherwise, it's too easy to create a Yeah-that's-how-I-would-have-coded-it-too attitude in the reader's mind.

the author must state clearly on page 1 who the intended audience is.

One can't fault a buyer for failing to meet all the author's expectations. Person the person bought the book to expand their knowledge. Or, there may not be any intermediate books available on a topic. Basic beginner and Advanced expert are often the two levels books are written for. How do you get from beginner to expert? Experience helps. Stepping out of your comfort zone helps. Reading books that are above your current level helps.

Tough to see them, but not so tough remembering them. Also, classroom pre-testing of different examples as you write the book also helps.

Great idea, if you are writing a text book. Not all authors are. And the deer? Well, the author knows what he/she is writing about. Why can't they get it?

One can't fault a buyer for failing to meet all the author's expectations.

Very true. One can, however, at least make some statement about the assumed level of the reader. Stating something like: "This book assumes no prior programming experience" at least sets the tone for the book. I've seen other books that state: "This book assumes one has a least one or two years of programming experience and is already familiar with the syntax rules of the language." This at least gives the potential reader some clue as to what is expected going into the book.

Great idea, if you are writing a text book. Not all authors are. And the deer? Well, the author knows what he/she is writing about. Why can't they get it?

Almost every book I've written was designed as a text book, so pre-testing examples does help and was very doable for me. As to the second part of the quote, that's a little surprising to me, especially coming from you. It would be great if all people understood at exactly the same level and pace. But you know that doesn't happen and to assume so is a mistake. I remember hearing Bjarne Stroustrup, the father of C++, present a paper at the Software Development conference back in the 80's. The title of his presentation was "C With Classes" and was the keynote to an audience of about 750. An hour later, 749 people exited the auditorium saying: What the hell was that all about? Knowing what you are writing about doesn't automatically make you a great teacher, especially when you have limited time or pages available to you. If it's so easy, why do you ever need to make more than one post per topic? Shouldn't the poster understand your answer from the outset? Alas, such expectations rarely work and not everyone "gets it" at the same pace. As a writer, the best you can hope for is to have large percentage "get it" with the few examples you can allocate to the limited number of trees available to you.

As to the second part of the quote, that's a little surprising to me, especially coming from you.

Oh, damn. I forgot the tongue-in-cheek emoticon. 8)

I presume you own a computer, so you can always get a standard C (or C++) book and work your way through the tutorials. Even if they involve reading/writing files. After all you can practice on your PC, which saves the time it takes uploading code to your Arduino all the time. Plus you can more easily debug by writing to standard output (eg. printf).

With a C++ book you will probably find it suggests you write to output like this:

cout << "Hello, world!" << endl;

These examples can be expressed in a similar way on the Arduino if you include the "Streaming" library:

#include <Streaming.h>

void setup ()
  {
  Serial.begin (115200);
  Serial << "Hello, world!" << endl;
  }  // end of setup

void loop () { }

http://arduiniana.org/libraries/streaming/

Stroustrup is a horrible writer. His books are just about unreadable.

econjack:

One can't fault a buyer for failing to meet all the author's expectations.

Very true. One can, however, at least make some statement about the assumed level of the reader. Stating something like: "This book assumes no prior programming experience" at least sets the tone for the book. I've seen other books that state: "This book assumes one has a least one or two years of programming experience and is already familiar with the syntax rules of the language." This at least gives the potential reader some clue as to what is expected going into the book.

Great idea, if you are writing a text book. Not all authors are. And the deer? Well, the author knows what he/she is writing about. Why can't they get it?

Almost every book I've written was designed as a text book, so pre-testing examples does help and was very doable for me. As to the second part of the quote, that's a little surprising to me, especially coming from you. It would be great if all people understood at exactly the same level and pace. But you know that doesn't happen and to assume so is a mistake. I remember hearing Bjarne Stroustrup, the father of C++, present a paper at the Software Development conference back in the 80's. The title of his presentation was "C With Classes" and was the keynote to an audience of about 750. An hour later, 749 people exited the auditorium saying: What the hell was that all about? Knowing what you are writing about doesn't automatically make you a great teacher, especially when you have limited time or pages available to you. If it's so easy, why do you ever need to make more than one post per topic? Shouldn't the poster understand your answer from the outset? Alas, such expectations rarely work and not everyone "gets it" at the same pace. As a writer, the best you can hope for is to have large percentage "get it" with the few examples you can allocate to the limited number of trees available to you.

Arduinos are great for learning about microcontrollers and electronics, but the IDE is a bit primitive. Plus developing for an external device is harder in lots of ways than developing and running your code on the same machine.

Modern IDEs include source-level debuggers with breakpoints, the ability to examine (and change) the variables in the running program, etc. These tools are incredibly valuable in learning programming.

I would suggest getting a C/C++ compiler for your computer (I use a Mac, and for the Mac I would suggest Xcode, which is excellent, and free) and a book on C.

You can write command line tools and skip all the OS stuff, since that doesn't apply. Command line C code is pretty much identical across all platforms. Writing command line tools is quite a bit like writing code using the Serial library on the Arduino, but with faster turn-around and much better debugging.

For learning vanilla C the original Kerrigan and Richie C book is a good choice. It isn't that long, is very well written, and includes lots of exercises. If you read that book and do all the exercises you'll have a solid knowledge of C, including a strong understanding of pointers.

Once you have a strong basis in C, you can move on to C++, or just stick with C. C++ is pretty heavy-weight for a small system like an Arduino with very little RAM. There is a lot to be said for the economy of C.

It would help to learn the basics of C++ so you understand the C++ libraries that are widely used on the Arduino platform, and also learn the basics of OOD (Object-Oriented Development).

Piethon:
Howdy!

I've been loving the Arduino, and have been learning how to do some things with it. I have been able to get some interesting projects set up, but they (to me) are still relatively simple. I am now trying things that are using more arrays, libraries, and have started to come across pointers. I would like to learn more about these, and get a better handle on the fundamentals (ints, strings, using #define as opposed to int =, etc.). Most of my learning has been from the Project book from the Arduino Starter Kit, and looking at other Arduino examples. I have also been using the Arduino language reference page, but am looking for something a little more guided.

I was just wondering if anybody has any good tutorial websites, YouTube videos, books, etc. they could recommend to learn the code/theory better - as opposed to piecemealing it together from Arduino projects and examples as those only get me so far before I run into a wall. I don't know C/C++, so I am assuming that I should look in to that. But, I'm so new, I don't know if I should learn C first, then C++, or if they're "the same" only one more capable than the other...

Anyways, any general ideas/advice would be appreciated! Thanks!

Oh woah, I just checked back in to this thread - thanks for all of the replies! :grin:

I will go through this and start learning. Y'all are awesome, thanks so much for all of the advice/thoughts/direction!

Edit: For what it's worth, I'm not brand new to programming. I have been teaching myself VBA (I do a lot of work with Excel), and just finished a Coursera class on Python, so I'm a little familiar with programming, but am still pretty green.

Piethon:
Edit: For what it's worth, I'm not brand new to programming. I have been teaching myself VBA (I do a lot of work with Excel), and just finished a Coursera class on Python, so I'm a little familiar with programming, but am still pretty green.

Obviously different people have different approaches to learning.

Personally I like learning by doing and. I reckon if you already know how to program then an in-depth study of C/C++ would not be necessary to figure out most Arduino stuff.

I am currently figuring out how to convert some Ruby code to Python. I know the sort of capabilities that should exist even if I don't know exactly what they are called. That's very different from the position a complete beginner would be in when they try to start programming in any language.

...R

Piethon:
I am now trying things that are using more arrays, libraries, and have started to come across pointers. I would like to learn more about these, and get a better handle on the fundamentals (ints, strings, using #define as opposed to int =, etc.).

I have absolutely no hesitation recommending, Beginning C for Arduino, by Dr Jack Purdham.
By far the clearest, most succinct explanation of pointers and data storage, I have ever came across.

I actually read the book as a refresher, after not touching C for over 15 years. The book is thin enough to not be intimidating and easily read cover to cover. The explanations focus on teaching one thing at once, going directly to the salient points; without blinding with science, waffling on like you are some over sensitive 5 year old, or hiding the key facts within myriad, repetitive and overly complex, irrelevant, examples. I particularly liked that the book sticks to teaching C by reading and writing code, rather than building projects.

I don't know C/C++, so I am assuming that I should look in to that. But, I'm so new, I don't know if I should learn C first, then C++, or if they're "the same" only one more capable than the other...

Not so much better or worse but different.

In t' old days, the sort of applications you found inside things like AT&T's telephone switches, often used OOP-esque C code. A bunch of static structures to store state information, operated on by a bunch of functions written specifically to manipulate those structures. Much casting to and from void was in evidence, much reinitialising of static variables and many bugs resulted. Stroustrup created C++ to marry the code and data together, as both a defined type and a natural progression of the C language. As my first introduction to C was in a company making telephone equipment, I familiarised myself by working through the 2nd edition of Stroustrup's 'The C++ Programming Language' A difficult book to read but still the best explanation of the language I have found. I guess I got 2/3 of the way through it before moving on. I didn't really get to grips with OOP techniques properly until I started programming with Delphi.

You can write OOP code for an Arduino using C++ but if you want to learn Object Oriented Programming, there are more approachable languages to do it with.