Example_3_Accelerometer_Tap.ino
1 // SPDX-FileCopyrightText: 2019 Tony DiCola for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 // Example of tapping accelerometer to light NeoPixels. 6 // Author: Tony DiCola 7 // License: MIT 8 #include <Adafruit_NeoPixel.h> 9 #include <Wire.h> 10 #include <SPI.h> 11 #include <Adafruit_LIS3DH.h> 12 #include <Adafruit_Sensor.h> 13 14 15 #define PIXEL_PIN 8 // Pin connected to NeoPixels. Use 8 for the NeoPixel built into Flora V2. 16 17 #define PIXEL_COUNT 1 // Number of NeoPixels. 18 19 #define PIXEL_TYPE NEO_GRB + NEO_KHZ800 // NeoPixel type, stick with default unless using different pixels. 20 // See the NeoPixel Uberguide for information on different types: 21 // https://learn.adafruit.com/adafruit-neopixel-uberguide/overview 22 23 #define SINGLE_COLOR Adafruit_NeoPixel::Color(255, 0, 0) // RGB color for single tap/click. 24 25 #define DOUBLE_COLOR Adafruit_NeoPixel::Color(0, 0, 255) // RGB color for double tap/click. 26 27 #define LIGHT_ON_MS 1000 // Number of milliseconds to turn on the LED 28 // when a tap or double tap occurs. 29 30 31 #define ACCEL_RANGE LIS3DH_RANGE_2_G // Range of acceleration values for the LIS3DH. 32 // Can change to 2G, 4G, 8G, or 16G values. 33 34 #define CLICK_THRESHOLD 80 // Sensitivity of click force. Depends on the range above, 35 // for 16G try 5-10, for 8G try 10-20, for 4G try 20-40, 36 // for 2G try 40-80. 37 38 39 // Create NeoPixel object. 40 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); 41 42 // Create LIS3DH accelerometer library object, this will by default use an I2C connection. 43 Adafruit_LIS3DH accel = Adafruit_LIS3DH(); 44 45 // Keep track of when to turn off the pixels if they've been lit by a tap. 46 uint32_t pixelOff = 0; 47 48 void lightPixels(uint32_t color, Adafruit_NeoPixel& strip) { 49 // Light all the NeoPixels in the strip with the provided color. 50 for (int i=0; i < strip.numPixels(); ++i) { 51 strip.setPixelColor(i, color); 52 } 53 strip.show(); 54 } 55 56 void setup() { 57 // Initialize serial port. 58 Serial.begin(9600); 59 // Initialize LIS3DH accelerometer library. 60 if (!accel.begin()) { 61 Serial.println("Couldn't find LIS3DH, is it connected?"); 62 while(1); 63 } 64 accel.setRange(ACCEL_RANGE); 65 accel.setClick(2, CLICK_THRESHOLD); 66 // Initialize NeoPixels and clear them. 67 pixels.begin(); 68 pixels.clear(); 69 pixels.show(); 70 } 71 72 void loop() { 73 // Turn off the pixels if we're past the time they should turn off. 74 if (millis() >= pixelOff) { 75 pixels.clear(); 76 pixels.show(); 77 } 78 // Check for an accelerometer tap/click. 79 uint8_t click = accel.getClick(); 80 // Stop if nothing was detected or an error. 81 if ((click == 0) || !(click & 0x30)) { 82 return; 83 } 84 // Check if a single click and light the pixels. 85 if (click & 0x10) { 86 lightPixels(SINGLE_COLOR, pixels); 87 // Set time for pixels to turn off 88 pixelOff = millis() + LIGHT_ON_MS; 89 } 90 // Check if a double click and light the pixels. 91 if (click & 0x20) { 92 lightPixels(DOUBLE_COLOR, pixels); 93 pixelOff = millis() + LIGHT_ON_MS; 94 } 95 // Small delay to keep from grabbing LIS3DH data too quickly. 96 delay(10); 97 } 98