In today´s lesson, the latest entry of the Programming with Arduino and Protocoder for makers course, we will be learning how to use LED matrices to create animations for our robots. In this case, we will use the matrix to create an eye that we will control remotely using Protocoder.

List of materials

  • ZUM BT-328 board
  • LED matrix: This time we are using a board based on the  MAX72xx chip.
  • Comodoro: Download the files for our robots at the end of the post.

Arduino code

The Arduino code receives data from the Protocoder program and moves the eye to a specified position, depending on the data received. The eye will also blink randomly for a certain time. To control the LED matrix we will use the LedControl library, that you can download here or together with the rest of the code at the end of the entry. If you want to know all the possibilities that this library offers, more information can found on the Arduino Playground website. To start programming with Arduino, we will need to include the library, define the pins and create an LedControl object, which we have named LM in this case:

With the last line of the previous code, we created the LM object which we will use to control the LED matrix. We will  also create the inString variable that will store the data received via Bluetooth. The readFromAndroid and writeData, functions will store the data in inString and “choose” which way the eye “looks”, respectively. If you want to know more about these functions, you can visit this website.

In this case, once the  message as been fully read (after receiving the ‘=’ character), we clear the screen using the LM.clearDisplay(0); command to make sure it is correctly positioned. In the “setup”, we start the serial port, we activate the LED matrix (which is set on power-saving mode by default), set the brightness at maximum level, “delete” the LED matrix and, finally, we set the eye so that it looks to the centre.

Two test routines have been included, testPos(), which changes between the positions of the eye and test(), which lights up each LED one by one. To generate the different blink positions, we can use a matrix from a file available in the download section, where we will  mark which LEDs we want to light up and “translate” them to code. We will use this function to light up an LED:

The status can be true or false, to switch the LED on or off, respectively. Here is the matrix diagram for the lookCenter() function which moves the eye to face forward: GRID-LEDHere is the corresponding Arduino code:

This enables us to generate different patterns with the LEDs. In our case, functions have been generated that change the position of the pupil (lookLeftlookRightlookUplookDown and lookCenter), as well as functions to simulate blinking (blinkCloseblinkOpen and fullBlink). The delayBlink variable changes the transition speed between closing and opening of eye. We will use it to increase or reduce the blink speed. The fullblink function takes the time that passes between the open eye and the closed eye as a parameter. We will use it to increase or reduce the frequency of the blinking.

The blinkOpen and blinkClose functions open and close the eye respectively,  and use the delayBlink variable to determine the blink speed. Lastly, the loop function controls the blink routine and the receiving of data via Bluetooth by calling the readFromAndroid and writeData functions.

To control the blinking of the eye, we will use the first part of the code:

To make the blinking different every time the routine is executed, two types have been included (simple and double), which are controlled by a similar routine. First we need to check if the current execution time for the program is greater than the time set for the next blinking. If the condition is true, it will execute the blink routine and set the next time, by adding a variable quantity, using the random function, to the variable that determines the next time. To read the text sent from Protocoder via Bluetooth, the following code is used:

Protocoder code

The program we will generate on Protocoder will send data to Arduino via Bluetooth. bq_Aquaris_E4_250To start programming, we will use the using heading habitual for our programs, as well as managing the for Bluetooth connection/disconnection. If you want to know more about how this code works, take a look at this course entry.

bluetoothTo send data to the ZUM board, we will use the send function which takes string to be sent as a parameter. For example, to make the eye look to the right, the function must send the word “right”, hence send(“right”);. The code for the send function is:

Next we will create the buttons that determine the position of the eye. To do this, we will upload any files we want to use to Protocoder and then we will create the buttons in the following order:

botonesOJOSTo adjust the size and position of the buttons, we will use two variables:

  • WidthImg, used to assign the height and width to the image.
  • posX, used to determine position X of the buttons.

In our case, we include the send(); function within the function carried out when the button is pressed, sending the order corresponding to the position:

3D design: Comodoro

To create the Comodoro 3D design, we have used the program Blender. Comodoro-sepiaTo print the design, we will use Cura with the following settings to generate the support: preferencias-soporteThis support is very easy to delete and brings stability to the print job. Within Cura, the scale has been modified to 40.79 to adjust the design to the size of our LED matrix, but the scale could vary according to the size of the matrix being used. Comodoro Size