Salutations! So, I am trying to learn Arduino programming better and have bought one official Arduino kit, and the "Arduino Cookbook, 2nd Edition". While the kit is good and the book is informative, I have had issues with some of the codes in them and have sought guidance through, here that was helpful for the most part. Does anyone have any advice on how to progress further where I get to the point of writing my own code without having to rely on copying? Any other books recommended? thank you in advance
My advice here is to think of a small project using some of the bits in your kit.
For example, if your kit has a motor, a servo, some buttons and a couple of potentiometers, you could simulate a car.
The motor will drive the car forwards or backwards. The speed of the motor will be driven by one potentiometer.
The servo will control the steering. This can be controlled by another potentiometer. A button could control direction of the motor.
Design/plan how you want to control the 'car' first (read through this post). Lay out a sketch in Pseudocode (this will allow you to check the logic of your program.)
Once you're satisfied on the logic, start to write your own code.
The project book(s) should have given you examples of each individual component which you can use as a guide
Check references such as cplusplus.com for syntax of the C/C++ language
Along the way, you can post questions to this forum and get advice on bits you're having problems with.
Jump in, and good luck
I agree with @darrob - find a project that you would like to implement and use that to drive your learning.
Another mechanism that I find very useful for learning is reading lots of stuff - this Forum, for example, is full of projects people are struggling with. Study some of them and the advice that is given. Maybe try to see if you can get some of the problem programs to work - a bit like doing a cross-word puzzle in a newspaper or magazine.
And, for learning purposes, don't just read Topics that specifically interest you. Other Topics might introduce you to Arduino techniques that would otherwise never have occurred to you.
...R
Agree and re-enforce the above posts ... If you like the book approach the two Simon Monk books are worth a look - the explanations of how the code works is very good and there is an on line portal to download the examples .
The idea is to study the examples so you understand how they work . There is a lot of guidance on this forum and on the other pages on this site . The Playground section is full of ideas.
Don’t forget to play with the examples in the Arduino IDE and work out how they work - eg use blink - alter the delay , make it flash two leds, control it with a button etc
Having a simple project gives you a target - I think my first project was to make an led simulate a candle by flickering in a random fashion . I then used an LDR , so if I put my finger over it , it snuffed the candle out.
Don’t be too ambitious - there are too many posts on here of the like “ I’m just starting out and want to control a nuclear submarine..”
Oh have fun !!!
There are loads of tutorials online for both C/C++ and Arduino.
Learn C before C++, save that for later. C is leaner and simpler but moreso the ways of C are more suited to small computing environments like Arduino Uno with 32K flash and 2K RAM.
Learn C strings. The functions in string.h have code-like names made of shortened names for what they do, it's not as difficult as it looks unless you try to memorize them all without any system. Also you will probably never use even half of the string.h functions, keep a reference page bookmarked and you can use any of them pretty quickly. The most important thing about C strings is what they are, arrays of chars with ASCII text values (or no text but ready for text) that end in a char==0, the terminator. You can work C strings by working the array, that's what the string.h functions do.
When you know C strings, avoid C++ String variables on Arduino as they tend to waste RAM and CPU cycles.
Nick Gammon's microprocessor blogs/tutorials has a number of links on various aspects of programming MCUs.
If you don't know "Do multiple things at once" then that should be on your A list with "Serial port: reading without blocking" soon after. Those two are keys to writing automation code.
Look into "Switches, transitions, debouncing" if you want to work with contact buttons (or just touching wires together) and know the mystery of "bounce" before you get false presses in your newly fast code. You don't see the bounce in slow code but slow code will never let you see a LOT of things. This lesson does tell that "what should be" fall over simple may be complicated. That's often true, what you don't know can screw up your day as you walk right into it.
I've spent a lot of time finding out how small things work. Most fun for the least money was making my own capacitive sensors, using an article from the Arduino Playground, another source of projects and info.
The important part in my opinion is to thoroughly understand the examples that you tried.
If you don't understand why an example is written the way it is written, ask. Someone will be able to explain.
Check the Arduino reference section for Arduino specific functions that you use and don't understand / want to know more about; check C/C++ references when you encounter standard C/C++ functions that you want to know more about.
It's good to look functions/commands up. Every time you do, you get positive reinforcement and become less likely to forget.
I did like the instant help in my old Borland C++. Any syntax and use could be checked quickly enough for work to keep going.
Divide and conquer.
Most Arduino projects have four parts that run sequentially inside the main loop:
-
Gather data from switches or other sensors. The example code supplied with libraries should be enough to get you started.
-
Make decisions using if/then/else statements. This is where your core logic lives. For example if a temperature sensor is above a certain value then a fan should be in the 'on' state.
-
Send data that controls external switches, for example the fan mentioned in the previous step. This may require some knowledge of electronics and power supplies.
-
Optionally, display something to the end user so that they can tell that the Arduino is behaving correctly given the sensor input values.
Each of these is a separate sub-project in its own right and can be tacked in isolation using known dummy data. Once you have got a sub-section working reliably you can chain it to the next. If the project as a whole doesn't work as expected then you debug each section in isolation as before.
darrob, Robin2, hammy, and everyone else, thank you all for the useful suggestions. I will try the motor and servo suggestion as well as tweaking the other projects. I will probably post something else soon for advice on these projects. thank you again!
Post about the problems you had following the book. Show what didn't work as you expected it to. Folks here can certainly help with that!