Where to find info on how to code

Hello, Am attempting (for a second time) to understand the coding thing. Have searched multiple times over various errors to find how to repair code to satisfy the IDE so it will verify. Getting multiple errors on code that is supposed to work. Have found 1 or 2 solutions to 7 or 8 errors... frustrating (I think that is why I gave up last time).
Where can I find something that will give me more information on code and errors than just the blink sketch?

Hello riverrunner06

Welcome to the worldbest Arduino forum ever.

Take a view here to get some ideas.

hth

Have a nice day and enjoy coding in C++.

Have you taken a good look at the Blink code ?

Do you understand what every line does ?

Going one step further, once you have Blink working in your toolkit, try loading Blink Without Delay (uses millis timing)???
(both are included in the IDE Examples)

In reality , it should take two evenings to get through these two examples from zero - to a point of understanding. Use the language references to learn what each command does.

You must understand EVERY line of those short programs.

Then set yourself a simple idea, and think through the logic needed to achieve it.
Don’t write the code yet, just learn to ‘think’ like a computer.

Only then step into turning it into a program". Don’t get too ambitious, something simple that takes maybe 20-50 lines of code.

Then sit back, and explain to yourself exactly what EVERY line does, and why it’s there.
If you need to - ask, because there’s no point making more complex problems if you can’t solve simple ones.

Good luck.

I'm not aware of specific Arduino programming tutorials that describe the general structures of programming; this is one that I just found (Arduino Coding Basics - JavaTpoint) which might help (not checked).

Most tutorials that you will find are for a PC; the generics are the same (variables, functions, statements, ...) but the problem is that they use e.g. printf to print to the screen and that does not exist in the Arduino world (at least, not standard).

There is no golden rule regarding errors. In general you work your way from top to bottom.

Common mistakes by beginners are missing semicolons and '{' and/or '}' in the wrong place. Using tools → autoformat can help in finding them (if you know what to look for). Those errors are in general easy to solve although it can be confusing if the error refers to another line.

Other errors can relate to the fact that C/C++ is case sensitive; a variable called riverrunner06 is not the same as a variable called RiverRunner06.

And than there are scope errors; a variable declared in e.g. a for-loop or in setup() is unknown outside the for-loop or setup().

Another error can be that you found a sketch on the web, copied it and it does not compuile because you don't have the specific library installed. The error is very clear (at least for the more experienced users).

Compilation error: LiquidCrystal_I2C.h: No such file or directory

So you find a LiquidCrystal_I2C library, install it and compile again and you possibly get the next error

Compilation error: 'class LiquidCrystal_I2C' has no member named 'init'

This indicates that the sketch that you found on the web did use a different LiquidCrystal_I2C library than the one you did install (LiquidCrystal_I2C is notorious for that). You will either have to find the library that was used or you have to look at the examples that came with the library to understand how to use it and next adjust your sketch.

There are many more types of errors, it's too much to describe them all.

what is your background?

the K&R book is brief and may be helpful with well written examples

not suggesting you should read elements of programming style, but it discusses bugs in textbook examples of code teaching programming.

Hello, what is the error message that you're seeing?

Programming Electronics Academy just wants your email address and they will send lessons to you. They have a full YouTube channel, and tips for better programming.

The secret with errors is just to read them literally

  • If they say something is "not defined" - then you need to define it;
  • if they say something is "duplicated" - then you need remove the duplication;
  • etc, ...

Now the compiler does assume that you are familiar with the language syntax and terminology.
The language used by Arduino is C++ - so any C++ textbooks, tutorials, etc should help with that.

As @sterretje a lot of general C++ textbooks, tutorials, etc will be focussed on desktop systems, so you might want to search specifically for embedded C++

eg,

https://www.udemy.com/course/embedded-software-development-using-cpp/

Disclaimer: Those are just from a Google search - I haven't studied them in detail.

Another Top Tip with errors:

Always start with the first-reported error.

Very often, one error will lead to other things being wrong - often, lots of other things. Therefore fixing that first error will instantly get rid of all those consequent errors.

The error messages aren't always helpful. :( The complier doesn't know what's wrong and it doesn't know what you're trying to do. It just knows that it won't compile or that it "doesn't make sense".

I once had one misplaced curly-bracket in a large program and I got hundreds of errors. And of course, nothing that said I had a bracket in the wrong place... I couldn't find the problem "visually" so I had to comment-out large sections of code to narrow-down where the problem was.

..I think I had copy-and-pasted a big chunk of code from another part of the program so I didn't know where the real problem was.

And the compiler is just checking for syntax errors... You can have a program that compiles without errors, but it has a logical error so the program doesn't do what you want or what you expect.

So my advice to beginners is - Start with a known good program or "shell". I usually start with the Blink Example and then take-out the blink part when I've got something else that it can test.

Then, write one or two lines of code at a time and test-compile as you go-along. And test run whenever you have enough code to test what you've just added.

Of course pros usually write more than one or two lines at a time but almost nobody writes a whole program without testing as they go-along.

That's not as easy as it sounds.... You can't just start at the top and work-down... i.e. If you delete the last half of a program it won't compile... The compiler has to see a "complete program".

Add some (temporary) extra print statements so you can "see" (with the serial monitor) what your program is doing. You can make little messages like "calculating average" and you can print-out variables. You can also make & print-out "fake" temporary values if you don't have the real data yet.

Oh... I also (now) like to add comments for the curly-brackets, especially the closing bracket, and especially when I have nested brackets. i.e. "//End of for- loop". Or more detail if I have nested for-loops, etc.

That depends. Sometimes it knows & tells you exactly what's wrong - examples in post #8.

But, indeed, other times it just knows that whatever you've given it is not what it was expecting at that point; so it tells you what it was expecting, or what would be valid there.

The trouble is the point at which realises this might be some way beyond your mistake; eg, if you miss an opening bracket, it might not be able to tell that until it reaches the "orphaned" closing bracket.

Another common example is where there's a mistake near the end of a header file - that might not be noticed until somewhere later in the file which included that header.

Mistakes in macros can cause some really weird messages - because the compiler is not talking about the text as you see it, but the text after the preprocessor has done with it.

awneil, thank you. One of the problems I am having trouble with is "not declared". Have been trying to find how to declare something. Like "mp3_play".
The code I am working with is from an Instructables post. Some of the errors, I am finding, are details. How is something declared in the scope?

The best thing to do in this case is to post your code (in code tags) and the error (complete error also in code tags) here and then somebody can point out the specific answer to your question. Which you can then use to figure out answers to future problems of the same type.

Not a good place to learn anything about coding techniques and practices. The code you'll find there is generally crap.

This is basic C++ (and C) stuff - so would be covered in general textbooks & tutorials.

The C++ language requires that things are declared before they are used. The declaration is what tells the compiler:

  1. The name of the thing;
  2. The type of the thing;
  3. optionally, some other stuff like whether it is const

eg,

int an_int; // declares a variable called 'an_int' of type 'int'.
int an_int;  // declared globally - always in scope

void setup()
{
    int an_int_in_setup;  // declared in setup() - in scope only in setup()
    Serial.begin(115200);
}

void loop()
{
    int an_int_in_loop;  // declared in loop() - only in scope in loop()
    for(int x; x<10; x++)  // x declared as an int in a for loop - only in scope in the for loop
        {
            Serial.println(x); 
        }
}

A similar thread here:

And another - specifically on dealing with compiler errors:

As stated before, one error may result in a cascading series of errors, a typo in a variable type can cause a variable not to be declared, causing all references to that error to come up as 'not declared within this scope'
The first error is the one to have a proper look at, but that usually does mean that you need to scroll up through the output window of the IDE

The most annoying and hard to find error i encounter is when i miss a closing brace or curly brace, which then tends cause an error of a function not being declared. I see the function clearly in the code, and it is declared, but somewhere else there is a closing brace or curly brace missing, messing up the whole code and causing the first function called within setup() to be not declared.

Usually doing an auto-format will help you locate the missing brace, because the code will make a significant indent somewhere in case of a normal brace, or will stay indented when it shouldn't be in case of a curly brace.
That curly braces are also used to assign values to an array can compound this issue, and curly-braces and normal braces look quite similar inside the IDE editor. Fortunately, if you place the cursor next to a brace or curly-brace, it will find it's counterpart for you within the code by highlighting it.

When working in projects with multiple .ino files, the auto-format will not always help as much, but then the obvious place to look is where you have modified things last, unfortunately, the cursor will point to the first function it can not find. It is what it is.

There error messages are usually quite specific about where the error is, with a line number and cursor position included and the file where it was found, which will make it a lot easier to find, i do suggest you tick the box in File->Preferences that shows those numbers in the editor.

About declaration, some has been said, post #17explains in short what is 'scope' , basically the difference between a local and a global variable, but if you find

is it actually a variable ? or is it a function, and in that case, maybe just the pair of braces behind it is missing

mp3_play();

sometimes it may be helpful to use Edit->Find to locate any mention of those words inside of your code to clarify what is actually wrong.

Sometimes what is a variable, is a variable which is part of an object. First the object is declared and all the members of that class are available through the object, as you see in many part of code
(just an example)

WiFiUDP Udp;

void setup() {
  Udp.begin(6454);
}

calling begin() without the object it belongs to will throw the same error, 'not declared in this scope'

Main thing is though that you understand what is going on with the basics of the code.

I read 'C++ for dummies', and there also an 'Arduino for dummies' Maybe a bit outdated but still very decent study material.
All of the IDE included examples are good to learn to understand, but they will not teach you about how objects can be used. Fortunately many of the external libraries create objects and use member functions, but it does help if you actually understand what it is that is being compiled.

Totally agree with that, people that post code on instructables tend to be more interested in showing others what they've done (look Mummy i built a ...) and are rarely really good and error free. Still some of those instructables can be quite good and can point you in a direction that can be useful, and since you tend to learn most from mistakes, learning is almost guaranteed.
If you pull code from instructables, do read the comments and try and seek answers there. Most authors have long abandoned these projects unfortunately, so any chance of support is marginal.

Again, if you post a specific question here with proper documentation, such as the sketch, the error message (both within code-tags please so it is properly formatted) and where you got any of the included files from (github link) People tend to help you.

or even a Class ... ?