A good programing tutorial

i would like some suggestions, i would like to watch / buy a tutorial explaining everything about programming (arduino). I have already searched on yt, skilshare and udemi but not really found something that really covers everything ... I already have 3 years of experience but I would really like to know everything, does anyone know a good tutorial? paid or free!

Isaak

Some here will disagree, but I found this to be very useful:
http://www.cplusplus.com/doc/tutorial/
However, you need to be aware that this is a general C++ tutorial (the Arduino programming language is a superset of C++, so any valid C++ is valid Arduino code). It doesn't document any of the functions of the standard Arduino API (though you're probably pretty familiar with those already after 3 years). It also teaches about things that are relevant when programming a computer, but don't apply well to embedded systems like Arduino. So you sort of have to follow your intuition about what information is useful and what you should skip over. I didn't have any trouble with that. I didn't read straight through it. Instead, I read the sections that provided the information I thought would be immediately useful, and took note of the other information that was available so I'd know I could come back for it later when I needed it.

how deep does "everything" mean?

deep down to the manufacturing process of the ATmega dies?
Verilog-files that define the logic of each single transistor inside the die?

for beginners

you wrote 3 years of experience with what? Arduino programming? So what was the most complex thing you did.
overloaded functions? multi-dimensional array of pointers to pointers?

Find a C tutorial. Arduino programs are generally small and simple and unless you are writing a library, your C++ needs can come later. Classes can wait, as long as you know how to use the existing ones like Serial.

Once you have C thoroughly nailed, pert's C++ tutorial would be good too.

It depends what your needs are though, a need to know "everything" is rather nebulous. What do you want to do with the skills you acquire?

I will list several sources of good information. The books are fairly dated and some may be out of print. They are good to buy here and there if you like to read. I would get one for Christmas, or check it out from the library, and see if you get much from it before laying down a lot of money for the whole library. Most of the websites are very worth the money, as they are free.

websites
Stroustrup: C++ - useful in particular for the style guide
http://www.cplusplus.com
https://www.cprogramming.com
https://www.avrfreaks.net - hosted by Microchip (formerly Atmel) forum for all things AVR
http://www.microchip.com - formerly Atmel, semiconductor manufacturer, spec sheets for AVR chips
ArduinoCore-avr/cores/arduino at master · arduino/ArduinoCore-avr · GitHub - the "Open Source" of Arduino
Arduino Reference - Arduino Reference - Arduino's documentation "very lacking"
https://electronics.stackexchange.com/
https://www.edn.com/
https://www.eevblog.com/

Books
"C Programming Language" by Brian Kernighan and Dennis Ritchie
"The C++ Programming Language" by Bjarne Stroustrup
"The Elements of Programming Style" by Brian Kernighan and P. J. Plauger
"Essentials of Flowcharting" by Michel H Boillot, Gary M Gleason and Lister Wayne Horn
"Code Complete" by Steve McConnell
"Clean Code" by Robert C. Martin
"Arduino Cookbook" By Michael Margolis

wildbill:
Find a C tutorial. Arduino programs are generally small and simple and unless you are writing a library, your C++ needs can come later. Classes can wait, as long as you know how to use the existing ones like Serial.

Once you have C thoroughly nailed, pert's C++ tutorial would be good too.

I would disagree. C and C++ are different languages.
Arduino uses C++, so if you want to learn programming for Arduino, follow a C++ tutorial.

Should I learn C before learning C++?

Many (especially older) people learned C before C++, because the languages became popular in that order, but that doesn't mean everyone should do it in the same order.
You are not missing anything if you start with a C++ tutorial, good tutorials cover the basics, there's absolutely no need to learn C first.
Besides, C programmers generally write terrible C++ code.

Pieter

Here is your home work:

Arduino links of interest.

How to use this forum:
https://forum.arduino.cc/index.php?topic=149014.0

Listing of downloadable 'Arduino PDFs' :
Either Google >>>- - - - > arduino filetype: pdf
Or
https://www.google.ca/search?q=arduino+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=arduino+filetype%3A+pdf&aqs=chrome..69i57j69i65.1385j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8

Listing of downloadable 'C++ PDFs' :
Either Google >>>- - - - > C++ filetype: pdf
Or
https://www.google.ca/search?q=c%2B%2B+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=c%2B%2B+filetype%3A+pdf&aqs=chrome..69i57.22790j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8

Arduino cheat sheet:

Watch these:
Arduino programming syntax:

Arduino arithmetic operators:

Arduino control flow:

Arduino data types:

Understanding Destructive LC Voltage Spikes:

OR

Why MOSFET gate resistors:

Some things to read

LCD information:

OR

Reading a schematic:
https://learn.sparkfun.com/tutorials/how-to-read-a-schematic

Language Reference:

Foundations:

How and Why to avoid delay():
http://playground.arduino.cc/Code/AvoidDelay

Demonstration code for several things at the same time.
http://forum.arduino.cc/index.php?topic=223286.0

Multitasking:
Part 1:

Part 2:

Part 3:

Sparkfun Tutorials:
https://learn.sparkfun.com/tutorials?page=all

Micro Controllers:

Useful links:
https://forum.arduino.cc/index.php?topic=384198.0

Arduino programming traps, tips and style guide:

Call for useful programming discussions
https://forum.arduino.cc/index.php?topic=383980.0

Jeremy Blume:

Arduino products:

Motors/MOSFETs

Switches:

Share tips you have come across, 500+ posts:
https://forum.arduino.cc/index.php?topic=445951.0

Images from above:
https://www.google.com/search?q=“Share+tips+you+have”+larryD+site:https://forum.arduino.cc&prmd=nmvi&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiJw-zu68ncAhXPGTQIHWCDCNwQ_AUIFCgE&biw=1024&bih=653

Debug discussion:
https://forum.arduino.cc/index.php?topic=215334.msg1575801#msg1575801

Frequently Asked Questions:
https://www.arduino.cc/en/main/FAQ#toc10

Number 'type's.

  • boolean (8 bit) - simple logical true/false, Arduino does not use single bits for bool
  • byte (8 bit) - unsigned number from 0-255
  • char (8 bit) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results
  • unsigned char (8 bit) - same as 'byte'; if this is what you're after, you should use 'byte' instead, for reasons of clarity
  • word (16 bit) - unsigned number from 0-65535
  • unsigned int (16 bit)- the same as 'word'. Use 'word' instead for clarity and brevity
  • int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE
  • unsigned long (32 bit) - unsigned number from 0-4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running
  • long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
    float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should. We'll touch on this later.

You select the 'type' best suited for your variables.

ex:

  • your variable does not change and it defines a pin on the Arduino. const byte limitSwitchPin = 34;
  • since an analog variable can be 0 to 1023, a byte will not do, you can select 'int'. int temperature;
  • if your variable needs to be within -64 to +64 a 'char' will do nicely. char joystick;
  • if your variable is used for ASCII then you need type 'char', char myText[] = {"Raspberry Pie Smells"};
  • if your variable enables some code then boolean can be used. boolean enableFlag = false;
  • millis() returns the time in ms since rebooting, unsigned long currentTime = millis();
    etc.

Oh, and have fun too :slight_smile: !

PieterP:
I would disagree. C and C++ are different languages.
Arduino uses C++, so if you want to learn programming for Arduino, follow a C++ tutorial.

I agree with your first point. However, Arduino programs are tiny for the most part. Repeating myself, the C++ side isn't a necessary skillset to acquire to be effective. Almost all you'll see in Arduino code is basically C with use of classes that exist in libraries.

If you want to be an Arduino guru creating things for other folks to use, good C++ knowledge is necessary. If you just want to make things, it really isn't. Of course it's good to have, but it feels to me that it can come later YMMV.

@PieterP: My guess is that you've never taught a C/C++ programming course. I've taught university-level courses in both C and C++. For a while, there were no prerequisites for either course. However, my experience was that those who had C first and a much easier time learning C++. I distinctly remember a beginning lecture on what a class was and one of the students who had taken the C course commented: "Oh! A class is like a structure, but it can have functions (methods) in it!". Those who had not had the C experience looked at him like he was from a different planet. Later, the university required you to have C or Java before taking C++. Even with Java, I think those students are at a disadvantage because Java is C with trailing wheels (i.e., no pointers). Finally, most of the C++ code found in the Arduino environment is in the library code, not in the examples that so many beginning Arduino students use while learning to program in the IDE.

Also, the statement:

Besides, C programmers generally write terrible C++ code.

seems a bit harsh, and Bjarne Stroustrup and several others might take offense.

C++ Programming Style by Cargill explains how not to write bad C++ programs and why

Bjarne Stroustup said he thinks it take 10 years to become proficient in C++

"A class is like a structure, but it can have functions (methods) in it!".

I simulate classes as structs all the time. not sure if a struct support construction.

IIRC, structs and classes are almost equivalent in C++. The only difference is that structs default to their components being public, classes private. So a struct should be able to have a constructor.

I don't claim that people with a background in C don't pick up C++ more easily, that's obviously the case, both because they have some general programming knowledge and because the syntax is largely the same.
But if your goal is to become a C++ programmer, you follow a C++ course, not a C course and a C++ course.

There's no reason why a C++ course can't explain what a struct is, you don't need a C course for that.

econjack:
Also, the statement:

Besides, C programmers generally write terrible C++ code.

seems a bit harsh, and Bjarne Stroustrup and several others might take offense.

I'm sure Bjarne doesn't fall in the "generally" category when it comes to C++ :slight_smile:

Don't get me wrong, I'm not at all saying that C programmers are worse programmers!
However, more often than not, C programmers are used to using unnecessary mallocs, raw pointers, macros all over the place, etc. in places where C++ offers better or more appropriate tools. There's a lot of good practices in C that are bad practices in C++, and it can take a lot of time for someone to re-learn old habits.

So not only does taking a C course before a C++ course mean you have to follow two courses, it also means you have to re-learn some parts of the C course to follow the C++ best practices.
If your goal is to master both languages, there's nothing wrong with that, but if you just want to learn C++, I don't think learning C first is an efficient way to reach that goal.

PieterP:
If your goal is to master both languages, there's nothing wrong with that, but if you just want to learn C++, I don't think learning C first is an efficient way to reach that goal.

I think we've got off topic from the original question here and I agree with you there. The thing is though, that most Arduino users don't care about C or C++, they just want to get their (very simple) project done and for that, C++ is overkill.

PieterP:
However, more often than not, C programmers are used to using unnecessary mallocs, raw pointers, macros all over the place, etc. in places where C++ offers better or more appropriate tools.

didn't Scott Meyers write Effective C++ discussing the pitfalls of using new() and delete()?

PieterP:
There's a lot of good practices in C that are bad practices in C++, and it can take a lot of time for someone to re-learn old habits.

i'm curious about a good practice in C that is not a good practice in C++.

some would say about C++ "It does a lot of things half well and it's just a garbage heap of ideas that are mutually exclusive." and develop their own language, Go

i believe learning good programming techniques will help in any language

gcjr:
didn't Scott Meyers write Effective C++ discussing the pitfalls of using new() and delete()?

Yes, and he's absolutely right. You shouldn't be manually allocating stuff in C++, always prefer the standard containers (such as std::vector) and smart pointers with std::make_unique or std::make_shared. That eliminates any possible memory leaks altogether, and results in clearer code.

That being said, new and delete do have their place in the C++ language, it's what's used under the hood to implement std::vector and smart pointers, and they can be used for writing custom data structures and containers.

Malloc and free, on the other hand, have no place in C++, they should only be used in C. Using malloc in C++ doesn't call the constructor, and will most likely result in undefined behavior. C++ has a strict notion of object lifetime (it has constructors and deterministic destruction).
If you allocate storage for an object using malloc, it does just that: allocate memory. As far as C++ is concerned, it's just raw memory, not an object.

gcjr:
i'm curious about a good practice in C that is not a good practice in C++.

C programmers tend to use macros for a lot of things, such as constants, small inline functions. This leads to type errors, and unexpected behavior, such as the abs macro defined by the Arduino Core:

Notes and Warnings
Because of the way the abs() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results.

abs(a++); // avoid this - yields incorrect results

// use this instead:
abs(a);
a++;  // keep other math outside the function

It's much safer and more appropriate to use const or constexpr variables and functions in C++. Constexpr functions are evaluated at compile-time, and can be used in constant contexts such as array sizes, so there's really no reason to use macros instead.

Type punning using unions is fine in C, but invokes undefined behavior (and bad type safety) in C++.

C programmers are used to manually allocating memory in C, and manually initializing or cleaning up resources. C++ has smart pointers, as mentioned earlier, constructors for initialization, and patterns like RAII to handle cleanup. This eliminates any possible resource leaks.

In C, casting pointers to data and even functions to (void *) is common practice, especially when dealing with APIs that provide callbacks or communication libraries such as MPI. C++ has templates, lambda functions, and type-safe std::variant, std::any and std::function types for these purposes.

Some C programmers have some very stubborn misconceptions when it comes to performance, and refuse to use OOP or exceptions because of a possible performance hit, even when that's demonstrably false.
Don't get me wrong, some people abuse OOP for huge unrelated inheritance structures, but the pattern is very useful in many cases, and calling a method on an object in C++ is just as fast as passing a pointer to a free function in C.
There is a small performance hit if you use virtual functions (which you have to declare explicitly), but even then, it's faster or equally fast as the alternatives. Most importantly: it'll be much more readable than the procedural equivalent.

This list is getting long enough, I'll end my rant here :slight_smile:
Just to be absolutely clear: I'm not blaming any C programmers, and most of the things I mentioned are perfectly fine in a C code base. But old habits die hard, so some C programmers also apply the same techniques to C++ code where it's not appropriate.

gcjr:
some would say about C++ "It does a lot of things half well and it's just a garbage heap of ideas that are mutually exclusive." and develop their own language, Go

Well, Ken Thompson might be a bit biased in the C vs C++ debate :wink:

He did have a point though, especially back in 2009. There were times where C++ lacked a clear vision, but I don't think that's the case anymore since C++11 and more recent releases, although it still caries some of that earlier baggage in the name of backwards compatibility. This results in some alternative ways of achieving the same thing, which can be confusing. Large effort is being done in standardizing this, though, such as the C++ Core Guidelines.

gcjr:
i believe learning good programming techniques will help in any language

Absolutely, but not all concepts can be applied in the same way in all languages.

PieterP:
Some C programmers have some very stubborn misconceptions when it comes to performance, and refuse to use OOP

there's no reason you can't write good OOP in C (or assembler) and writing is C++ is no guarantee that your writing OOP much less good OOP

i think i bears repeating that Stroustrup said it take 10 years to become proficient in C++. I've yet to see a C++ program that really demonstrates its capability ... and i do believe there is an advantage to C++

gcjr:
there's no reason you can't write good OOP in C (or assembler) and writing is C++ is no guarantee that your writing OOP much less good OOP

I don't understand what you're getting at here.

C has never aimed to support OOP directly, C++ has supported it from day 1 and provides constructs that make OOP code much more readable and maintainable.

If you can't write good OOP code in C++, you're not going to write good OOP code in C either.

when I worked at Bell Labs ('85) I went to a talk on OOP. Within the first three sentences the speaker said there's no reason you can't write good OOP in other languages, including assembler, but not languages like Fortran or Basic. He also said just because you're writing in C++ does not mean you're writing (good or bad) OOP.

the concepts of OOP existed long before C++ (or Simula). C++ support classes, among other things. you don't need classes to write good OOP. And in fact, well written .c and .h files better supports data hiding and avoids the need to recompile code when changes are made to the implementation of the object than C++.

C++ can of course support many other programming paradigms that OOP.

gcjr:
when I worked at Bell Labs ('85) I went to a talk on OOP. Within the first three sentences the speaker said there's no reason you can't write good OOP in other languages, including assembler, but not languages like Fortran or Basic. He also said just because you're writing in C++ does not mean you're writing (good or bad) OOP.

the concepts of OOP existed long before C++ (or Simula). C++ support classes, among other things. you don't need classes to write good OOP.

Sure, but I don't see what all this brings to the table in a discussion about C++ and/or C programming tutorials and best practices?