Diving Into Arduino
Last month I came a cross a video of a small dancing robot called OTTO. What makes OTTO special for me is that it is completely OpenSource. I am a professional software developer and I work with OpenSource software for years. But this little robot reminded me that hardware can also be open sourced. You can build a clone of this cute robot at home in a day : 3D print the moving parts, buy the required electronics, download the software and build the robot by reading the construction manual.
The brain of this robot is powered by an Arduino Nano board. Arduino is an open source prototyping platform. An Arduino board can read inputs and turn these inputs into outputs. For example it can read commands over bluetooth and turn these commands to outputs that activate a motor. Arduino is a trade mark name but the hardware is open source. Anyone can freely copy the design and sell their own clones.
Last month I ordered a Starter Kit For Arduino from AliExpress. My 28$ starter kit from China has a UNO board which is a clone of the original Arduino UNO.
After reading the documentation and some sample projects I am able to turn on and off LEDs attached to breadboard. I think blinking a LED is the Hello World of programming an Arduino board. I am a software developer, the only thing I remember about electronics is the Ohm’s Law. V = I * R. If you are also new to electronics like me you can virtually design your circuits and test your code by simulation on circuits.io. Below is an Arduino “Hello World” example:
An Arduino project has mainly two parts:
- Hardware and electronics (which I have a little understanding)
- Software (this is where I am good at.)
Here is a simple Arduino program that blinks a LED attached to input pin 9 of an Arduino Uno board:
An Arduino program has two main functions:
- void setup()
- void loop()
The loop function is very similar to game loop. Which is the heart of every digital game on this planet. A game loop:
- runs continuously
- processes user input (each turn)
- updates the state of the game
- renders the game
If we apply the game loop concepts to an Arduiono program the loop function can be writen as a game loop:
- Arduino program runs continouosly, until we press the reset button or the cut the power off.
- On each turn we can process user input. For example read the input pins and detect if the user pressed a button. Read an analog signal from an analog input pin to which a sensor is attached.
- Update global state of the program. For example update the counters
- Write to digital or analog output pins, so we can control the attached peripherals. (Turn LEDS on, write text to LCD screen, activate motors)
I am trying to write a C++ library that will simplify the loop function. The library will:
- act as an event handler. I will be able to attach functions to specific input events.
- simplify the implementation of timed actions. For this I am planning to implement something similar to Libgdx Scene2d Actions Here is what I mean:
I am going to share a more detailed example when this library called “Looper” is finished.