stock-cube

Stockcube Performance Indicator

Message to those who have blogs

I knew when I started writing that I wouldn't be able to keep a consistent posting schedule, but man I underestimated just how sparse the posts really would be. I think 1-2 a month is doable, I don't want this to end up as another venture that ends up getting dropped.

I'm always disappointed when someone says they have a blog and it turns out they only posted a few posts and shortly gave up after, so the goal is to not end up like that. If you're someone reading this who has a blog and haven't posted in a while, do try to post again.

If I know you, chances are I've read your blog and look forward to future content and if I don't know you, feel free to send me an email with a link to your blog, I'd love to read it!


The project

That aside, let's take a look at Stockcube, the project I've been working on for the last month or so.

My goal with this project was to create a physical piece of hardware that would sit on a desk and glow a certain color depending on a stock's performance. Simple enough you might say, but hardware has this habit of being finicky to get working, especially when I'm the one trying to put it together.

I knew from the start that I wanted it to be as simple as possible; red for negative percent, green for positive percent, with the intensity of the color indicating how strongly the performance was leaning towards.


Attempt 1: Arduino

I have experimented with the arduino before, but am still fairly new at it, so I figured this would be a perfect project to learn some more. As I laid out the foundations for what I needed to get this working I was a bit concerned about the WIFI requirement needed to get the current stock price, but I told myself I'd figure it out when it came to it and to hope for the best.


LEDs

Using LEDs was immediately what I thought of to use as an indicator. I picked up some RGB LEDs off Amazon that had 4 pins, 1 for each color and a ground pin. Getting this to work with the arduino wasn't too complicated.

I learned about a functionality that's possible with IO pins, normally a pin can be ON or OFF, but you can fake an analog signal (any number between 0 and 1, useful for "inbetween" levels of brightness) using something called PWM, or Pulse Width Modification.

The gist of this is that you can rapidly switch a pin from 0 to 1 in such a way that the input device attached to that pin treats the signal as analog. This is all done internally, all I had to do was pass a decimal to a function, but it's definitely interesting.



Venn diagram effect

I finished hooking up the LEDs to the Arduino and put together a demo program that would cycle between a rainbow of different RGB values. There was a problem however, the LEDs were not diffused.

I didn't know the term was called diffusion at the time but basically the red green and blue outputs of the LED weren't being "mixed" together to form a color, there were effectively 3 very small LEDs inside for each color.

This would result in a "venn diagram" effect where you could see the individual colors. The picture on the right is an example of this, a single RGB LED outputting green and blue. It's a bit poor quality, but you can see how this effect looks, not very appealing.


Diffuser

My solution to this was to 3D print a white diffusion box that would scatter the light around and blend it up a bit, getting rid of the distinct ring effect.

I tried several methods, but the best was just distance. Likely because after a certain distance, the light of the smaller LEDs will have spread out in a big enough region to not be individually noticable.

I started off with a much longer box than this and kept cutting it until the distance was good.


The WIFI Problem

Now that the LEDs were setup, I needed to implement a way for the arduino to communicate with the internet to gather stock quotes. This proved to be much more difficult than I first expected, as the arduino doesn't have built-in WIFI so I had to purchase and try to adapt a WIFI module.

I toiled with that cursed metal thing for hours, but could not get it to even detect any WIFI router, let alone connect and communicate with it.

With no way to communicate with the internet, the project was as good as dead. I did some research and decided it would be less of a waste of my time to go an alternate route, with the raspberry pi, than to try and get the arduino to work. So begrudgingly, I bought a Raspberry Pi Zero W and got to work coding that.


Attempt 2: Raspberry Pi Zero W

The script for the Pi ended up being quite concise with all the original features I intended (typical Python). The script would communicate with a Yahoo Finance API to pull real time stock quotes and then it would light the LEDs accordingly.


The stock prices pulled from the API were run through a formula to get the appropriate values for the LEDs.

The price is inversely interpolated in relation to a "best case" and "worst case" value, which is then returned as a decimal value from 0 to 1. The closer the value is to 1, the closer the price is to the best case.

The green LED would then be lit according to this intensity, and the red LED would be lit in reverse, 0 being the brightest and 1 being the dimmest. This would essentially translate to a color gradient from bright green, to a yellowish-orange color, to a bright red, all depending on the stock price and what the user defined as a good/bad percentage.

Another minor thing to mention was that unlike the arduino with its flashed single-purpose memory, the raspberry pi has a full operating system, so it had to be instructed to autorun this script on startup. The end result being that all you have to do to get the full functionality of the stockcube is plug in the pi, no typing or logging in required.


Circuit

In an effort to reduce the footprint of this project, I cut some header pin wires to size and soldered them to the ends of the LED so I can easily "plug in" the LED to the raspberry pi.

I then crudely insulated each pin with some tape and wrapped the whole thing up.


Printing

With the software out of the way, I turned to 3d printing an enclosure for the project.

I measured the dimensions of the electronics and modelled a quick box in Blender before printing it.

Unfortunately it turned out smaller than intended, and I had to place the board at an awkward angle to fit it in. The diffusion box laid on top of the bottom piece and was attached with a piece of clear tape.

This assembly had the unintended outcome of looking sortof like a tombstone and casket, a bad omen for my stock futures indeed.

Final Result

Here is a picture of the final result in action (I took the pic on a red day), I ended up using clear tape to hold the pieces together incase I need to make adjustments to the code or wiring.

The diffuser worked very well, I didn't notice the venn diagram effect anymore. I personally didn't like the black border around the cube's edges, but that can be easily fixed with some sanding.



Next Steps

There are many many improvements to be made to everything but I had already spent weeks on this project inbetween waiting on shipping deliveries of new parts and starting from scratch with a different circuit board, so I had enough. I wrote down some suggestions I had for Stockcube 2: