Shenzhen I/O: Color-Changing Vape Pen

Welcome back to china and our electronic building company. This time we got a contract with Cool Dad to make a color-changing vape pen – or rather millions for all his fans, so let’s see how we can implement that.

The specs here says that we will get a packet over radio which is either -999 which means that we should just sleep or it is four values with the meaning red, green, blue, duration. Which means the intensity of the 3 colors and the duration to hold it.

It also says that if we get a new instruction while we are currently running, we should override it. However, it only happens in the test set if the duration is 999. Otherwise it seems to run out.

Structure of the solution

From the problem it is clear that we need 3 simple outputs to control the led. And this time we cannot use the DX300 trick. Because we need the value of the signal, not just 0 or 100. So we need at least to micro controllers.

I have chosen a solution that have the structure like this

Shenzhen IO - Color-Changing Vape Pen - structure

So if we can build a color-changing vape pen with that structure, we would end at 8¥for the cost. I do know that there exists 6¥ solutions out there, but I haven’t been able to condense the problem into two MC4000.

Coding a color-changing vape pen

The mc4000 code I chose as simple as I can.

  slx x0
  mov x0 p1

All it does, is to sleep until it receives something on the xbus and pass it on to p1 as a simple output.

The MC6000 is a little more tricky. The structure of that code is a little bigger. It goes something like

Read the first value from x1
Test if the value is -999
if not then set the red led to that
read and set green and blue
sleep for the duration specified unless it is 999
If 999 sleep 1 and try again

And how exactly do we fit that into 14 lines of code? Like this!

  mov x1 dat
  tgt dat -100 
+ mov dat x0
+ mov x1 p1
+ mov x1 p0
+ mov x1 acc
+ tlt acc 999
+ slp acc
+ mov 0 p1
+ mov 0 p0 
+ mov 0 x0
  slp 1

It wasn’t until I started writing this post that I got the last bits and pieces sorted out for this problem. Among the issues was that I checked the acc every run through, instead of making that a condition I checked only when I set the leds. That last change cut the energy almost in half.

The solution looks like this once it is implemented
Shenzhen IO - Color-Changing Vape Pen - solituion

It solves the problem in

Cost: 8¥
Energy: 238
Lines of code: 14

Did you do it better? Can it be done better? I know it can be done at a lower cost, but I don’t know of solutions using less power or lines of code.

Posted by Kristian

Leave a Reply