Skip to content

Instantly share code, notes, and snippets.

@brainmachine
Last active August 29, 2015 14:15
Show Gist options
  • Save brainmachine/6315fca181c74a4c8b37 to your computer and use it in GitHub Desktop.
Save brainmachine/6315fca181c74a4c8b37 to your computer and use it in GitHub Desktop.
VFS_gyro_pixel_final_signal
/**************************************************************************/
/*!
@file Adafruit_MMA8451.h
@author K. Townsend (Adafruit Industries)
@license BSD (see license.txt)
This is an example for the Adafruit MMA8451 Accel breakout board
----> https://www.adafruit.com/products/2019
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
// Include libraries
// Gyro
#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>
// NeoPixel
#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
// CONSTANTS:
#define PIN 6
#define NUMPIXELS 12 // How many NeoPixels are attached to the Arduino?
// Global variables - Declare any variables here that you need to access throughout the entire program
Adafruit_MMA8451 mma = Adafruit_MMA8451();
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 150; // delay for 150 ms
bool isBlinking = false; // Set this to true at the beginning of pattern methods, set it back to false when pattern is done
/*-----------------------
SETUP
Only runs once - when you start the Arduino.
This is where you put all your initialization code (anything that happens only once).
-----------------------*/
void setup(void) {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// Open a Serial connection
Serial.begin(9600);
Serial.println("Adafruit MMA8451 test!");
// If we can NOT start the accelerometer:
if (! mma.begin()) {
Serial.println("Couldnt start");
// If we can't connect to the accelerometer, stay in an endless loop of nothing:
while (1); // Read as "while true". This is never set to false (0) so it will run in a loop forever
}
Serial.println("MMA8451 found!");
mma.setRange(MMA8451_RANGE_2_G); // Gyro range in +/- meters/sec. (-2 to 2).
// Choose 2, 4, 6 or 8 m/s depending on how much acceleration you are expecting to occur
Serial.print("Range = ");
Serial.print(2 << mma.getRange());
Serial.println("G");
// Initialize our pixels
pixels.begin(); // This initializes the NeoPixel library.
//startupPattern();
fadePattern();
}
/*-----------------------
MAIN LOOP
-----------------------*/
void loop() {
// Read the 'raw' data in 14-bit counts
mma.read();
/* Get a new sensor event */
sensors_event_t event;
mma.getEvent(&event);
// Basic gesture recognition
float threshold = 0.5; // Detection threshold in m/s - Higher number results in less sensitivity
// If acceleration is greater than the threshold AND there is currently no blinking pattern, signal left
if (event.acceleration.x > threshold && !isBlinking) {
signalLeft();
}
// If acceleration is less than the NEGATIVE threshold (note: threshold * -1.0) AND there is currently no blinking patter, signal right
else if (event.acceleration.x < threshold * -1.0 && !isBlinking) {
signalRight();
}
Serial.println();
delay(40);
}
/*-----------------------
PATTERNS
-----------------------*/
void startupPattern() {
isBlinking = true;
int counter = 0;
int numCircles = 2; //how many times to circle the animation around
// While the counter is less than the number of pixels * the number of circles, light up one pixel at a time
while (counter <= NUMPIXELS*numCircles) {
// Set one pixel to green. The index is determined by counter%NUMPIXELS (always returns a number in range of 0 to 11
pixels.setPixelColor(counter % NUMPIXELS,0,255,0); // Green
pixels.show(); // Show the green pixel
delay(100); // Keep it on for 100 ms
pixels.setPixelColor(counter % NUMPIXELS, 0, 0, 0); // Set it to black
pixels.show(); // Turn off the green pixel
counter++;
}
isBlinking = false;
}
void fadePattern() {
int counter = 0;
int duration = 200;
while (counter < duration) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(counter, counter, counter)); // Moderately bright green color.
}
pixels.show();
delay(10); // How long to display each gradient
counter++;
}
}
void signalLeft() {
isBlinking = true; // Remember to set this to true to avoid overlapping patterns
int numBlinks = 5; // How many times to signal
// Each iteration of this loop blinks one time (on and off)
for (int i = 0; i < numBlinks; i++) {
// Iterate through pixels 0-5 (half of the pixel ring)
// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
for (int i = 0; i < NUMPIXELS/2; i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(190, 170, 0)); // Orange
}
pixels.show(); // This sends the updated pixel color to the hardware. Turns lights on.
// Make sure to wait before we turn the lights off
delay(300); // Delay for a period of time (in milliseconds).
for (int i = 0; i < NUMPIXELS/2; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Black
}
pixels.show(); // Remember to show your black pixels as well
delay(300); // Make sure the pixels stay black for x ms before repeating the pattern. Otherwise the black pixels would be 'visible' for only one frame
}
isBlinking = false; // Remember to set this to false at the end of every pattern, or the next pattern will not start.
}
void signalRight() {
isBlinking = true;
int numBlinks = 5;
for (int i = 0; i < numBlinks; i++) {
for (int i = 5; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(190, 170, 0)); // Orange
}
pixels.show(); // This sends the updated pixel color to the hardware.
delay(300); // Delay for a period of time (in milliseconds).
for (int i = 5; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Black
}
pixels.show();
delay(300);
}
isBlinking = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment