Today, I was going to do a video and post on how to use the HCSR04 Ultransonic Rangefinder, but I may have accidentally set it down on some energized wires and fried it. That's alright though, I have a box of them over at my local hackerspace, Makers Local 256, so I should be able to get that video up soon.
Instead, I'm going to talk about debugging on the Gemma, since it works a little bit differently than other Arduino-based microcontrollers. The Gemma board does not have a built-in serial-to-USB converter so you cannot send or receive data using the Arduino serial console or other console applications. If you have your own serial-to-usb cable or adapter, and have two IO pins on your Gemma to spare, you can use a software serial library to add data transfer capabilities by following Becky Stern's wonderful Gemma serial debugging tutorial on Adafruit. If you have a sensor that needs two IO pins or don't have access to a serial-to-usb converter, there are still some tricks you can use to provide a little bit of data feedback.
The idea I'm using to read back some simple values is to blink an LED a number of times equal to the value. So if my ultrasonic rangefinder sees an object three feet away, it will blink three times. If you're looking to check values higher than ten or so, you can divide the value by a number to keep the number of blinks from being too high. The code that I reference here and in the video is available on my GitHub repo for the Case Hat project here.
First define the pin we're going to use for the LED. The Gemma has a built-in LED on IO Pin 1, so we'll use that:
const int LED_PIN = 1;
Then in the setup() function, we need to delay by a couple of seconds because the Gemma LED fades in and out on boot, to show that the Gemma is ready to receive code. Since we want to count the number of blinks, this fading in and out can cause some confusion, so let's let that settle down first for 3000 ms (or 3 seconds).
void setup() {
delay(3000);
Set the LED pin as an output:
pinMode(LED_PIN, OUTPUT);
Blink three times for the value '3', and then pause for a bit (I'll explain how this function works later):
blinkDebug(3);
delay(2000);
Blink three times for the value '2', and then pause for a bit:
blinkDebug(2.4);
delay(2000);
Blink three times for the value '3', and then pause for a bit:
blinkDebug(2.6);
delay(2000);
}
Nothing is going on the main loop:
void loop() {
}
The blinkDebug() function takes in a floating-point number and blinks that many times rounded to the nearest integer:
void blinkDebug(double debugValue){
Rounding to an integer is not as simple as converting the floating point, because that just truncates (or cuts off) the digits after the decimal place. So to make sure it rounds to the nearest number rather than just round down, we add 0.5 to the value before converting it to an integer. So a number like 2.6 turns to 3.1 and then is truncated to 3, but 2.4 turns to 2.9 and truncates to 2. There are certain types of numbers that this doesn't work on through, what sort of numbers would cause a problem with this scheme?
int roundedValue = (debugValue+.5);
Loop the number of times we calculated, and blink the light on for a little while and then off.
for (int i=0; i<roundedValue; ++i) {
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
delay(200);
}
}
So this is just a quick little method I've used to get some values out of the Gemma due to its lack of serial port. If you need larger values you can divide the number before blinking, or multiple the value if you are trying to display small values less than one. Full source can be found at https://github.com/tylercrumpton/Gemma-Case-Hat/blob/master/LedDebugging/LedDebugging.ino