Tag Archives | Maths

Programming challenge: a temperature-sensitive light.

This project builds on the previous post, where we explained how to set Sparkle colours using the “RGB” block:

Sparkle RGB block

It was inspired by the type of night-lights that are available for babies’ bedrooms, where the colour of the light indicates to parents if the room is getting too hot or too cold.

We want the Sparkles to display predominantly “cool” colours if the room is too cold and “warm” colours if it is too hot. As we are using the matrix display, it would also be nice to have a pretty “twinkling” effect.

The block which randomly changes the colour of each Sparkle in the matrix display

The program is, therefore, based around the block, above. The colour of each Sparkle in the array is updated, randomly. The level of green light also changes, randomly. If the value of “red” is low and the value of “blue” is high, the Sparkle colour will vary between blue and cyan (depending on how bright the green light is) while if “blue” is low and “red” is high, it will vary between red, orange and yellow.

A previous night-light project shows how to connect up the Crumble, battery pack and Sparkle matrix display. The input from a light-dependent resistor (LDR) was used to control whether the lights are on or off. In this project, instead of the LDR, we are using a thermistor (a resistor whose resistance varies depending on temperature). You can simply attach a croc clip to each “leg” of the component but, to make it a bit easier to handle, we have soldered ours onto a “crumbliser”

The first thing we need to do is calibrate the input, i.e. work out what temperature the resistance of the thermistor corresponds to. The thermistor is connected to “A” and “B” on the Crumble: “A” will be the output and “B” will be the input. The following program allows us to monitor the value of “B”. As it depends on temperature, we have assigned it to a variable called “t”.

Something I learnt during the course of this project is that, if you want to calibrate an analogue input, it is better to connect it across 2 of the Crumble I/O pads (A,B,C & D) as we have done, here, rather than connecting one pad to the Crumble and the other directly to the “+” pad on the battery box. This is because the voltage level from the battery box can vary, whereas the Crumble itself outputs a fixed voltage.

We then immersed the thermistor in hot water; in iced water; put it outside; and, finally, my 6-year-old held it in his hand. We watched how the value of “t” changed (in the “Variables” menu of the Crumble software) and my 9-year-old made a table and then a graph of the results.

Based on this investigation, we decided that the red emitter should be off (0) when t = 85 and fully on (255) when t = 170. (Ice water and boiling water being a bit too extreme in this case!) The blue emitter can then simply be the inverse of this (i.e. 255 minus the value “red”).

Our table of results and graph

I briefly explained the formula for a straight-line graph (y = mx + c) and “we” determined that m = 3 and c = -250. We then input these numbers into the Crumble program in order to convert the value of the input “t” into our outputs “red” and “blue”:

We also used two “if” blocks to set limiting conditions to prevent the value of “red” (and, hence, “blue”) going higher than 255 or lower than zero.

The completed program is:

Crumble program for our temperature-controlled night-light

The program is inside a “do forever” loop so the colour of one random Sparkle in the array is changed every 100 ms.

It is difficult to capture the different colours on camera, but here is the finished night-light (mounted in the Sparkle matrix pixelator) at three different temperatures:

Aaah…. just right!