🎉 Black Friday is Here! Enjoy Up to 35% Off ~ Offer Ends November 30th!
In this build, we'll explore how to use the CodeCell's onboard 9-axis motion sensor to detect taps. This project demonstrates how to use tap detection for interactive controls, making it perfect for creating responsive actions with a simple tap on the device.
The CodeCell is equipped with a BNO085 motion sensor, which offers a variety of sensing capabilities, including tap detection. This feature uses accelerometer data to detect taps, making it ideal for interactive controls like toggling lights, triggering sound effects, or other actions based on a simple tap gesture.
The BNO085 motion sensor detects taps by monitoring sudden accelerations along its axes. When a tap is detected, the sensor registers the event, allowing you to trigger actions like lighting up an LED or toggling other devices. This functionality is particularly useful for creating touch-based interactions without the need for mechanical buttons.
In this example, the CodeCell continuously monitors for taps. When a tap is detected, the onboard LED lights up in yellow for one second. You can expand on this basic functionality to create more complex interactions, such as controlling multiple LEDs, motors, or other connected devices based on tap inputs.
Below is the example code to get you started. Make sure your CodeCell is properly connected via USB-C, and follow the comments in the code to understand each step.
#include <CodeCell.h>
CodeCell myCodeCell;
void setup() {
Serial.begin(115200); // Set Serial baud rate to 115200. Ensure Tools/USB_CDC_On_Boot is enabled if using Serial
myCodeCell.Init(MOTION_TAP_DETECTOR); // Initializes tap detection sensing
}
void loop() {
if (myCodeCell.Run()) {
// Runs every 100ms to check for taps
if (myCodeCell.Motion_TapRead()) {
// If a tap is detected, shine the LED yellow for 1 second
myCodeCell.LED(0xA0, 0x60, 0x00); // Set LED to yellow
delay(1000); // Keep the LED on for 1 second
}
}
}
In this next example, we use a CoilCell to flip its polarity, and actuate a flip-dot. This expands the interactivity by using tap detection to control external devices, creating a more dynamic response.
#include <CoilCell.h>
#include <CodeCell.h>
#define IN1_pin1 5
#define IN1_pin2 6
CoilCell myCoilCell(IN1_pin1, IN1_pin2);
CodeCell myCodeCell;
void setup() {
Serial.begin(115200); // Set Serial baud rate to 115200. Ensure Tools/USB_CDC_On_Boot is enabled if using Serial.
myCodeCell.Init(MOTION_TAP_DETECTOR); // Initializes tap detection sensing.
myCoilCell.Init(); // Initializes the CoilCell.
myCoilCell.Tone(); // Plays a tone to confirm initialization.
}
void loop() {
if (myCodeCell.Run()) {
// Runs every 100ms to check for taps.
if (myCodeCell.Motion_TapRead()) {
// If a tap is detected, shine the LED yellow and flip the CoilCell's polarity.
myCodeCell.LED(0xA0, 0x60, 0x00); // Set LED to yellow.
myCoilCell.Toggle(100); // Toggle the CoilCell's polarity.
delay(1000); // Delay to keep the LED on and polarity flipped for 1 second.
}
}
}
This project introduces the basics of using tap detection with CodeCell. Experiment with the code, customize the responses, and explore the potential of tap detection in your next project!
Be the first to know about new projects and get exciting deals!
© 2024 Microbots.