• aditya mehrotra.

emo-eyes Arduino code generation. [tutorials/resources]


what are emo eyes?

Great question, didn't know myself until I came across this on Amazon, and they were just too cute to not try out. It's a 24x8 LED Dot Matrix with a fancy laser cut cover over it which communicates over SPI (serial peripheral interface) to any Arduino. It takes 5V power and comes with a great library and set of example code for may fun faces including "sleepy3" shown above.


So I was playing around with one of these things on an Arduino NANO one day after following the following tutorial to set it up: http://wiki.sunfounder.cc/index.php?title=24x8_LED_Dot_Matrix_Module_-_Emo


The library it comes with includes a file called emo_maps.h which looks like a huge list of these, these are called structs:


const byte OFF[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};


Where OFF is the name of the face to display from the function my_emo.show_emo([name]); included when you type #include <SunFounder_Emo.h> and #include <emo_maps.h>. emo_maps contains the faces, SunFounder_Emo contains the code that lets you write the faces to the LCD.


For example: my_emo.show_emo(OFF); will turn off all the LEDs on the emo. NOTE: you must import the libraries and initiate the emo correctly like in the sample code.


#include <SunFounder_Emo.h>

#include <emo_maps.h> // some emotions


Emo my_emo(10); // CS pin - SEE TUTORIAL


void setup() {

Serial.begin(9600);

Serial.println("Emo go!");

}


Towards the bottom of the tutorial I noticed you can program your own faces in into the emo_maps.h file included in the code. You can edit these pixel row by pixel row to create your own faces like in the sun founder tutorial on the bottom of the page but this was annoying. No one wants to code each face pixel by pixel and then convert it to hex by hand. So here we went to google sheets to try to see if we could create a code generation tool where you can design a face and then copy and paste the struct generated into emo_maps.h and run the Arduino code.


creating the emo_maps code generation spreadsheet

The first thing to notice is what do the hexadecimals in the byte construction even mean.

Each of the hexadecimal values in the struct are the binary values of ONE of the rows in ONE of the dot matrixes that make up the screen. And when they are written to the struct, the list of hexadecimals is in order of top row Matrix1, top row Matrix2, top row Matrix 3, second row Matrix 1... and so on according to the documentation.


So the first thing was to create a grid-like space where you inserted a binary number "1" or "0" for pixel on/off and doing so would change the color of the square to white or black so we could VISUALIZE what we are creating.



This is the visualization part of the spreadsheet, inserting the 1's and 0's into the locations where we want black square and white squares in the FIRST of the two tables will change the LCD into whatever we want it to display. This was simply done using conditional formatting. If you input a 1 into the square, the background goes white and so does the text. If you input a 0, it goes black and so does the text (so we see the face but not the numbers).


The second table you see, the dot matrix, copies the values over into individual matrices 1, 2, and 3 representing the individual screens we need to control. It is a direct copy of the first box, except it is divided into the three parts representing the three screens and has NO conditional formatting for the visualization.



Now, like we established before, each of the hexadecimals in the file, represents one whole row of one of the matrices in the three-matrix LED grid. A matrix with all of the LEDs off will have binary values 00000000 (because 8 pixels, all of them off) and the conversation from this to hex is 00000000(bin) = 0(hex). So the spreadsheet then takes the dot matrix we created above and creates an 8x3 matrix which takes the rows of the matrices and makes them binary strings, and then the binary string to hex section converts them to hex. All of the conversions were done using the built-in Google Sheets BIN2HEX() function.



Now all thats left is some formatting. C-code likes the hexadecimals in 0x00 format, so we make sure the hexadecimals are double digits and add the '0x' prefix to tell C it's a hexadecimal. And then the Formula-Based-C-Code-Conversion simply combines the matrix into the format that emo_maps.h takes by ordering the hexadecimals correctly, adding commas in between, adding "const byte NAME" and adding {} brackets and a semicolon.


NAME can be set in the upper right-corner of the spreadsheet and the name will be included in the formula-based conversion, so what you'll end up with is a struct with your desired name for whatever you're displaying.



Then all you do is copy the cell "TEXT OUTPUT" and paste it into emo_maps.h using your favorite text editor, in my case VS Code. The emo_maps.h file can be found in the Arduino folder under libraries, SunFounder_Emo folder. TEXT OUTPUT just creates the whole output as a string so you don't accidentally copy the google sheets formula




Now in the Arduino code you can simply use whatever name you chose for the function and call it like you'd call any other one of SunFounder's in built emojis. We created a function named OFF to turn off the LCD by setting the value of every cell to "0" and then writing OFF in the name box, we pasted it above in emo_maps.h and now we can call it with the following code.


#include <SunFounder_Emo.h>

#include <emo_maps.h> // some emotions


Emo my_emo(10); // CS pin - SEE TUTORIAL


void setup() {

Serial.begin(9600);

Serial.println("Emo go!");

}


void loop() {

my_emo.show_emo(OFF); //we use our custom emoji the same way we use the built-in

}


Now we can make any emoji or display we want, we can make it say hi, display text, or whatever just by using the visualization tool to turn pixels on and off, and inserting the desired name for the display in the NAME box!


here is the final spreadsheet, PLEASE MAKE A COPY FOR YOUR OWN USE

https://docs.google.com/spreadsheets/d/1H4INa7uK3nCWlFkM6XR5ohNPKfhvRzvbv4K2fGakUhg/edit#gid=0


Hope you all enjoyed this quick project to help make programming the LCD easier! You can use this same spreadsheet with ANY other LED matrix that communicates using hexadecimals. Now that you know what we did, it shouldn't be too hard to make your own for a custom LED matrix even if you don't have the SunFounder Emo. We'll use this in future projects, for now hope you enjoyed and signing off!


~<3


#led #matrix #dotdisplay #arduino #robotics #visuals #computer_science




8 views0 comments
© copyright 2019 | aditya mehrotra