The CodeBug is a cute, bug shaped board that is pushing to be a key tool for educational purposes, offering an introduction into coding and basic electronics.
The front of a CodeBug has a five by five grid of super bright red LEDs, used as the primary form of visual output. The two 'ears' of the CodeBug both have programmable tact switch buttons. The six 'legs' have super sensitive terminals for I/O and power interfacing. The bottom of the bug has a set of standard jumper sockets for more advanced serial interface and for attaching expansion boards, affectionately called 'tails'. Conveniently, this can be mated directly to the GPIO connector grid on a Raspberry Pi .
It's designed to be super easy, using a web browser based programming suite that functions without the need to install any software to get coding. Rather than typing by hand, code is assembled by dragging and dropping pieces of a colour coded jigsaw puzzle. You just select what kind of command you want to use; a loop, an if statement, a command light up the LEDs or respond to a button press, then drop it into your program. The coding environment also has a fully emulated and interactive CodeBug to run and test out programs without having to upload it to actual hardware.
A great and unique thing about programming the CodeBug is that it doesn't require installing any special drivers or software on your PC. Just hold down the left button on the CodeBug while plugging in the USB port to the PC and it will enter programming mode. The CodeBug will show up as a small USB flash drive, around 45 kilobytes in size. It might sound extremely small, but for holding the programmed code files needed it's more than enough space.
CodeBug programs are files with a .cfg extension. Loading the program is just a matter of putting the code file on the USB storage drive of the CodeBug, then hitting the button on the right to run it. Because no special drivers are needed, nearly any computer can be used to load a program - even a simple Raspberry Pi works flawlessly.
There are two methods to power the CodeBug. If you connect the micro USB to a power source without holding down a button, the CodeBug will just run like normal and draw its power over the USB connection. Alternatively, you can keep the unit portable and use a 3 volt CR2032 coin cell battery slotted into the back of the unit. The CodeBug uses a very small amount of power so should be able to run off battery power for a long time before draining it completely.
Connecting things to the input/output connections is uses big sockets ideal for croc clips or banana plug connectors. It's very simple and they don't bend or snap like traditional jumper pins can with constant use. Alternatively, as the CodeBug is designed to be used as a wearable, conductive thread can be used to both interface with the connectors and to mount the unit onto something like a shirt lapel or the front of a hat. Because the power requirements of the CodeBug are so low there isn't any concern of getting an electric shock or be a fire risk from the electricity flow.
Common sense might make you think that getting the best economy out of driving a car involves being easy on the accelerator, but often being too hard on the brakes can be the most expensive way to drive. If you regularly brake hard it not only leads to much lower fuel economy but also makes the cars brake pads wear out faster - and from personal experience they are very expensive things to replace.
To keep track of the heavy braking in a car I used a CodeBug wired up to the brake pedal. The CodeBug lights up with a number displaying how many times the brakes are used very hard. Having this right in the dashboard of the car gives a reminder of how often the brakes are heavily used.
To be clear: sometimes braking hard is the best possible thing you can do, but by paying attention and braking smartly you can make driving that little bit cheaper.
To connect the CodeBug to the car I used thin wires, aluminium foil and strong duct tape. I mounted foil to the back of the brake pedal then connected it with a wire to the ground terminal on the CodeBug, being careful not to impede the travel of the pedal. Originally I intended to use a micro switch to trigger the alert, but it was near impossible to mount it without it impeding the brake pedals full travel distance in any way. To ensure safety I used a small block of very light foam mounted to the floor of the car, right behind the shaft of the brake pedal. I connected leg 1 of the CodeBug to another small piece of foil attached to the top of the foam. When the brake gets pressed it makes the foil on the back of the brake pedal shaft and on top of the foam to come into contact, grounding leg 1 of the CodeBug and activating the alert.
The two foil pads that make contact when the brakes are applied.
Getting the position of the foil pads right can be challenging. Its a matter of trial and error to space it out so the sensor is only activated when the brake is near fully depressed. I started with a thick slab of foam and gradually cut it back until it fitted just right. The terminals on the CodeBug are extremely sensitive, so only the smallest of contact between the two pads is enough to trigger it.
Different cars have wildly different dashboard configurations. Just for simplicity I used solid double sided mounting tape fixed to the back of the CodeBug, on top of its battery slot. My 1983 Toyota has a big, bulky dashboard with plenty of flat surfaces so finding a suitable place to mount it was easy. I was able to run the wires from the sensors under the dashboard and have them come out from an unused slot right next to the steering wheel.
The CodeBug mounted on the dashboard
The code I assembled can be downloaded from the CodeBug space here. There you can see the Blocky view of the assembled jigsaw puzzle, but also switch over to the Code Viewer to see the corresponding traditional lines of code.
The description of the assembled code, piece by piece, is as follows:
- The first purple block starts the code then sets up leg 1 to receive the input from the brake. It also tells to CodeBug to stay awake indefinitely once powered on.
- Next the variable named 'count', used to keep count of how many times the sensor is activated, is set to the base value of 0.
- The number 0 is drawn to the LED grid on the CodeBug, the default state of having no recorded sensor activations.
- The main loop of the program is inside a while loop. This is continuously run while the CodeBug is powered on.
- The first if statement checks if the b button is pressed. If it is the CodeBug will sleep, essentially shutting down until another button press is detected.
- Now an if statement checks to see if the leg the sensor is on has been grounded, meaning the brake sensor has been tripped.
- The variable 'count' is incremented by 1, recording the number of times the brake sensor has been tripped.
- Then it draws to the LEDs the sprite of a number that corresponds to the number that our variable 'count' is at.
- It then pauses for 10 seconds, or 10,000 milliseconds, to wait for the brake pedal to be depressed.
- Following this a second nested loop starts, checking to see if the variable count is greater or equal to 10
- If the sensor has been triggered more than the10 times, a custom 'X' sprite is drawn to the LEDs. This is a warning to show that you've been braking hard too many times.
- Finally it checks if the A button on the CodeBug is pressed. If it is the count variable is changed back to its base value and the LEDs are reset to 0, restarting the count from the beginning.
After setting everything up I was surprised just how often I was abusing the brakes in my old car, even in low traffic areas. Keeping the counter from ticking up can turn into a bit of a game! I just have to remember to be careful not to disrupt traffic.
With the brake sensor attached only one of the four I/O plugs on the CodeBug are used. You could wire up something similar to other things in a car, like detecting how many times the hand brake is used or when the accelerator hits the floor. The super bright LEDs on the CodeBug are very clear, even while driving on a bright summers day. It's also not as imposing or distracting as an LCD screen or other display might be. Plus, having bright red LEDs on the dashboard can make your dashboard look like the one on KITT from the 80's TV show Knight Rider!
Have any questions about this CodeBug project? Leave them in the comments below or ask me on Twitter - @aaronights