Monday, February 29, 2016

Self playable game on smartphone

Connecting Android to servo motor via USB OTG


I recently put together a Lego phone holder that can be rotated in one direction (y-axe the longer side of phone). The end of the holder is attached to a servo motor. Servo motor is connected to Android smart phone with USB OTG via motor module. Motor module is controller that can control up to 24 servo motors and runs on 5 volts.


The module controller part has already burned its own protocol. It was a combination of rotation and speed separated by new line symbols.

Second part was Android application that sends messages via serial protocol to this servo-module. I used an example of USB serial communication with Arduino and rewrote it for my device.


For this exercise I chose a game that I made about year ago. It is a simple game that uses accelerometer to move the rocket through gates. Leaning device to sides allows you to control the position of the rocket. The goal is to run through as many gates as you can. The speed is increasing within entered gates.


My first idea was to detect the values of maximum rotation to both sides. Then to use this values and dynamically calculate all rotations of holder. But after few changes in my holder construction I realized that my configuration is a little bit different each time. So I created a calibration method that runs at start up and detects the limit values by itself. Then during the game the values are recalculated from these limits. For better understanding please watch the video below.


The practical usage of "self playable holder" is literally none. But if you look at it as an exercise, it is a nice introduction to connecting an android to servo motor.


Tuesday, February 9, 2016

Avr clock

Wall clock made of avr mcu and led display using internal timer


The main goal was to try numeric LED display with MCU, so to make it a little bit more interesting I decided to create a clock that shows time in digital and analogue way. I used Atmega16L MCU because I needed 26 I/O pins (12 for numeric display, 12 for 2mm LEDs and 2 for buttons). The clock has got two buttons - one for adding hours and one for minutes. 2mm LEDs around edge shows seconds while numeric display inside shows hours and minutes.


I bought my display from ebay without any data sheet, but it is almost the same for all kinds of displays. You can easily find the right combination of pins. Four 7-segment digits are controlled via 12 I/O pins. Each digit is made of 8 parts (7 for number, 1 for dot). The other four pins are ground for each number. So to light up 4 different digits you have to toggle between all of them quickly.


A timer is a simple counter. The ATmega16 has two 8 bit and one 16 bit timer. The 8 bit counter (uses 8 registers) can count up to 2^8 = 255 while the 16 bit counter (uses 16 registers) can count up to 2^16 = 65 536. After reaching the maximum value for counter it simply overflows back to zero.

Lets say the MCU works on 1MHz frequency, that means it does 1 000 000 ticks per second, but the counters can only count up to 255 (8 bit timer) or 65 536 (16 bit timer) then it overflows. To set the counter properly you have to set the limit (prescaler) and also setup bits for right mode. All of the necessary configuration can be found in data sheet.

I used 16 bit timer with CTC mode (clear time on compare - when reached predefined value it is automatically set back to zero) with external interruption. The main thread is used for logic and redrawing while second thread (external interrupt) is used for counting time. There is a nice youtube tutorial for avr timers and also a avr timer calculator can be handy.


Using an internal timer might not be a best solution for a precise counter, because the error can be up to few percentage. So the clocks will lose its accuracy very soon. To avoid this error you have to use an external timer (e.g. Crystal Oscillator - more to come later on).


So I decided to use an external timer DS1307 RTC (real time clock) module. I've found a RTC library, removed buttons and added the timer.

the numbers have better visibility in the dark than in light room