Instructions

How it Works
The two new images are set up in the constants CHARGING and DISCHARGING.
Each number represents one pixel on the micro:bit screen, 0 is off and 9 is full on.
These names are upper case because Python programmers use this convention to
remind them that they are constant and shouldn't change throughout the program.
The main program and barchart() functions are almost identical to the previous
program, so let's just look at the lines in bold that have changed in this program,
first looking at the main program.
The reading is read from pin P0 as before, and represents the amount of stored
charge in the Solar Store. This is passed into the new function trend() which
works out whether the reading is going up or going down, and depending on the
direction of change, it displays either the CHARGING or DISCHARGING image. If
the trend is 0, no image is displayed. Because the display is cleared and re-drawn
completely each time round the main loop, if there is no change in the amount of
stored charge, neither of these images is displayed.
The trend() function is a little bit complex, but basically what it does is to record a
history of the last 10 readings from the P0 pin as 'differences'. If the difference is
positive then the amount of stored charge has increased since last time, and if it is
negative it has decreased since last time. Readings from the P0 pin will jump up
and down a little bit naturally due to electrical noise and interference, but what is
important is the overall long term trend of the direction of the readings.
Every time trend() is called it shifts the numbers left along the list by one (like a
big shift register) and keeps a running total. If this running total becomes positive
enough, then it indicates the Solar Store is charging, and if it becomes negative
enough it indicates it is discharging. Anything else is just treated as insignificant
random noise, and ignored.
You can fine-tune the accuracy of this trend analyser by changing the constant
SIGNIFICANT. A smaller number will make it more sensitive, but it will pick up
more of the random electrical noise that will be present at P0. A larger number will
be less sensitive. This detector will easily detect the change in charge that occurs
when the fan or the Bulb is attached to the output. It might not always completely
detect tiny changes in charge that occur using indoor lighting, but it will detect when
the sun is shining on the Solar Panel.
Page 20