October 31, 2016 · Arduino

Tiny Dashboard (Mini Python + Arduino Project): Part 3 - The Arduino Code

This is Part 3 of my Arduino dashboard project where I try to display a PC's system utilization stats onto an LCD display via USB.

The complete code is already hosted on my Github page. On this post, however, I'll try to explain what is happening on each chunk.

Here it is, for those of us who are too lazy to go to Github:

Code Breakdown

First, we import and initialize an object on Arduino's built in LiquidCrystal library meant for controlling LCD screens like the one we have.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Then, we initialize our strings that would contain each line of our message.

Next, I made a function that would use the LiquidCrystal library to clear the current content of a line and display the new content right after.

The setup sequence done in setup() is pretty straightforward. We open the serial stream, initialize the LCD display, and then print out "Waiting for connection..." -- if anything, the last part is for us to know that our hardware setup works.

Here, I suggest you compile and load the script we've done so far onto your Arduino, fingers crossed, and check. It should display something like this:

Okay! If that went well, we proceed to building our main loop.

On each iteration, we start with waiting for any input from our serial port.

To avoid garbage being displayed on the screen by unintentional connections to our serial port, we use a key (in our case, Clujlusjarr7) to tell our Arduino that the next 2 lines should be displayed on the screen.

The readStringUntil function from the Serial library is perfect for this, as it will discard any input from the serial buffer until it sees a specific string.

When the script finally continues, meaning the readStringUntil function finally saw our token, we read the next two lines, assuming each line ends with a \n character. We then use our printSerialLine function to print both lines onto the screen.

    line1 = Serial.readStringUntil('\n');
    line2 = Serial.readStringUntil('\n');
    printSerialLine(0, line1);
    printSerialLine(1, line2);

There you have it! By now, we should have working Arduino code that displays 2 lines sent through USB.

Up next...

In the following part, we finish up our project with the Python code that sends information from our PC.

Go to Part 4 - The Python Client