April 17, 2017

2. Dive into the Arduino Programming

Too many choices - too many decisions


The openness of the Arduino platform gives the unprecedented flexibility. Anyone can find own combination of tools and approaches which fit best his or her level and preferences. This also brings some complexity. Usually, it is hard to find information about all available options in a single place. So, for most of us, it takes many trial and error attempts. If the "errors" path is too long - it gets harder and harder to stay motivated and keep going.


We are not going to provide a training here - there are lots of specialized resources which do this much better. Our goal is to make your learning path as short as possible and gain enough knowledge to understand and enjoy the next steps in our robot building challenge.

Technically speaking - you don't need any programming skills to move forward. You can blindly follow the step-by-step instructions and copy-paste all the programs from the Internet. Start from recreating the experiments from your starting kit guide book - and you will get the overall impression on how does it feel like.

Programming Environments


  • Arduino IDE is a basic and very simple environment which allows you to create programs in C/C++. You have to install it anyway since it comes with the drivers for the Arduino board and all the required utilities. It works perfectly well for the initial experiments. It was built with the simplicity and reliability in mind. If your projects start to grow in size and include many modules and files - you will have to look for some more complicated tools.
  • Scratch - is a very simple environment for the visual programming, which does not need any programming language knowledge. It is easy to learn and use. Primarily targeted for children but also can be a good starting point for those, who starts their programming journey. My son use this environment since the age of 9. Mostly because Scratch is also used in his school for the kid's basic programming education.

    The main weakness of Scratch is that the program is not uploaded to the Arduino. It is being executed on a computer instead. Arduino, like a puppet, only reacts to the commands from the computer - blinks the LED, generates sounds, etc. So you can't create an autonomous robot using Scratch. But anyway that it not the purpose of this environment.

    The most popular Scratch versions are:
    • ScratchX Extension (we used this one). You will have to install a plug-in for your web browser, also you need Internet access to work with this environment. Even though this looks kind of unnatural, ScratchX can save programs to the local files on your computer and works perfectly well.
    • S4A autonomous program. This one is based on the previous version of Scratch. It installs as a stand-alone application and does not need an Internet connection to work. We did not try it yet, but from what we see - looks very promising.
  • Ardublock is very similar to Scratch. Provides the same simplicity in visual programming. The only difference is that the visual program you create is being translated to the real C++ code and injected into the native Arduino IDE. Thus your program is being uploaded to Arduino board and can work in the autonomous mode.

    This application was not updated for a while, so you may get some issues while installing and using it. If you face any issues, try downloading the most recent version from the Sourceforge repository. Also, recent changes to the Arduino IDE harmed the Ardublock work. You might need to downgrade your Arduino IDE back to the older version (i.e. 1.6.11).

    UPD: the most recent fixed versions of the Ardublock can be downloaded here: https://github.com/taweili/ardublock/releases

    There is also an alternative version - ArdublockEdu. It is more up to date and is more adopted toward learning standard C++ constructs and Arduino-specific features.

    Here is a simple LED blinking program built in Ardublock:


And here is the corresponding C/C++ program, generated by this visual sketch:
  void setup()
  {
    pinMode( 13 , OUTPUT);
  }

  void loop()
  {
    digitalWrite( 13 , HIGH );
    delay( 1000 );
    digitalWrite( 13 , LOW );
    delay( 1000 );
  }
  • Sloeber Eclipse plugin. This system is more suitable for the complicated projects with the large number of files, modules, classes and resources. It is based on the well-known Eclipse platform and can use the whole strength of this beast. Quick code editing, refactoring, work with the source repositories - all the things cool programmers need and enjoy. Don't miss the videos with a funny guy in a hat - he will explain everything.
  • Professional developers can use other tools. For example, it can be a straight GNU GCC system with couple specialized scripts to control the project build and upload process. It is unlikely such professionals will waste their precious time reading this blog. It is more for the programming and electronics newcomers. Still, if you are professional, and can share your wisdom - please feel free to add your comments below our posts.

In all our current projects (and particularly for the robot source code) we use Sloeber. But it is perfectly fine to download the source code to a single folder, edit it in Notepad and compile it for Arduino using the standard Arduino IDE (if you prefer this way more).

Programming Languages


Having even basic programming understanding you can read the code from the Internet and add minor modifications which you think will fit better your robot's behavior. For this purpose, it will be enough to pass some reasonable online programming course.

You can program microcontrollers using lots of the high-level programming languages. They will hide the hardware-level complexities from you. JavaScript is maybe the most trendy and the easiest one to learn.

We prefer classical approach - for our robot we use C/C++ (with the Arduino extensions). If you would like to build your robot using our source code - make sure you have some basic C++ programming book:
C++ For Dummies, 7th Edition

Alternatively, you can use online C++ tutorials like this one. Don’t waste time on STL topics or advanced file input/output as for now. Also, don't worry about installing C++ compilers and development environments from the tutorials. Arduino comes with the ready to use environments and takes care of all set-up steps.

A great starting point for the Arduino specifics is an official guide Getting Started With Arduino 3rd Edition by Massimo Banzi and Michael Shiloh. Once you finish the starting kit tutorials - switch to this book. It will explain how everything works.
Getting Started With Arduino 3rd Edition

Finished with the basic experiments? Can explain what is the difference between resistor and diode? You are ready for the next step - Jeremy Blum's training. Jeremy is really an exceptional person. He created a nice Video Tutorial Series for Arduino. He wrote Exploring Arduino - a cool book for the electronics newcomers. He also practices what he preaches and is one of the most prominent members of the electrical engineering community. Both the video tutorial and the book worth your attention - this can be a great conclusion of you guided learning curve.

Finally - join the electrical engineering communities on the web. You can find answers to most of your questions there. Also, friendly contributors will help you if the question was not answered in the past.
You don't need intensive knowledge to build your first robot. But as you learn - a huge number of the new opportunities will be revealed to you. This is the never ending and always exciting journey. Just don't stop.

No comments:

Post a Comment