Have you ever thought about logging your clicks? Whether you're counting things, timing your marathon or just having fun, I made this cool little project in less than ten minutes. I used Hexabitz uSD card module (H1BR60) connected to a push button and powered by a coin-cell battery holder module (H04R20). Happy clicking!
This project is a battery-powered click logger. You get visual indication and uSD card log for your clicks. It's powered using 2 x CR2032 coin lithium batteries in series. They should last a few hours. Of course, you can use other types of batteries as well but I just had these lying around and wanted to use them in something cool :)
Estimated hardware build time: 5 minutes
Estimated software development time: 5 minutes
Code Description:This project code is very simple. I define a button and an event-based log. The button has two event callbacks: one that triggers on clicks and another on a 3-second-press. The click callback increments an SRAM variable and blinks the indicator LED on each click. Every 10 counts, the LED gives a longer 1-second blink.
Define the button and link it to its callbacks like this:
AddPortButton(MOMENTARY_NO, P1); // Define a button connected to port P1
SetButtonEvents(P1, 1, 0, 3, 0, 0, 0, 0, 0); // Activate a click event and a pressed_for_x event for 3 seconds
Next, I create an event-based log and log two variables, the button state and the counter variable:
if ( CreateLog("Click Logger", EVENT, 10, FMT_TAB, FMT_SAMPLE, "Sample @ 10Hz") == H05R0_OK )
{
LogVar("Click Logger", PORT_BUTTON, P1, "Logger");
LogVar("Click Logger", MEMORY_DATA_UINT32, (uint32_t)&counter, "Counter");
// Do not reset button state after writing the log since we need it to blink LED as well!
needToDelayButtonStateReset = true;
}
Logging starts at first button click and is stopped when the button is pressed for 3 seconds in the PressedForX callback. Button events are also disabled there so that you don't see any more logs or LED flashes.
void buttonClickedCallback(uint8_t port)
{
++counter;
if (counter == 1) StartLog("Click Logger");
if ( counter%10 == 0 ) {
IND_blink(1000);
} else {
IND_blink(200);
}
needToDelayButtonStateReset = false; // Reset button state now
}
void buttonPressedForXCallback(uint8_t port, uint8_t eventType)
{
// The first PressedForX event we defined in SetButtonEvents was called
if (eventType == 1)
{
StopLog("Click Logger");
SetButtonEvents(P1, 0, 0, 0, 0, 0, 0, 0, 0);
IND_blink(400); Delay_ms(400); IND_blink(400);
}
needToDelayButtonStateReset = false; // Reset button state now
}
Note how I used needToDelayButtonStateReset flag to log the button state and at the same time use it to blink the indicator LED. With this variable set to false, the button state will be reset automatically after being logged. Here's how a sample-based log looks like:
You can also log time stamps by replacing the index column format parameter with FMT_TIME as follows:
if ( CreateLog("Click Logger", EVENT, 10, FMT_TAB, FMT_TIME, "Sample @ 10Hz") == H05R0_OK )
And you get this log:
Example log files are attached to this project. Note that it's better to load the firmware using power from USB cable connection without batteries (to avoid power confusion and reset states). Once done, you can insert the batteries and then remove the USB cable.
Check the Click Logger in action in this video!
Comments