Skip to content

Instantly share code, notes, and snippets.

@brainmachine
Created February 18, 2015 22:07
Show Gist options
  • Save brainmachine/98fd4e3ef04c815eb02d to your computer and use it in GitHub Desktop.
Save brainmachine/98fd4e3ef04c815eb02d to your computer and use it in GitHub Desktop.
arduinoVFS
/**************************************************************************/
/*!
@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?
// Variables
Adafruit_MMA8451 mma = Adafruit_MMA8451();
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 500; // delay for half a second
// Only runs one
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
Serial.begin(9600);
Serial.println("Adafruit MMA8451 test!");
if (! mma.begin()) {
Serial.println("Couldnt start");
while (1);
}
Serial.println("MMA8451 found!");
mma.setRange(MMA8451_RANGE_2_G); // Gyro range in meters/sec
Serial.print("Range = "); Serial.print(2 << mma.getRange());
Serial.println("G");
// Initialize our pixels
pixels.begin(); // This initializes the NeoPixel library.
}
void loop() {
// 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; i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(i*20, 255-i*20, 0)); // Moderately bright green color.
}
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
for (int i = 0; i < NUMPIXELS; i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Moderately bright green color.
}
pixels.show();
delay(delayval);
// Read the 'raw' data in 14-bit counts
mma.read();
/* Get a new sensor event */
sensors_event_t event;
mma.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */
// Values range from -2 to 2
/*
Serial.print("X: \t"); Serial.print(event.acceleration.x); Serial.print("\t");
Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t");
Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");
Serial.println("m/s^2 ");
*/
/* Get the orientation of the sensor */
uint8_t o = mma.getOrientation();
switch (o) {
case MMA8451_PL_PUF:
Serial.println("Portrait Up Front");
break;
case MMA8451_PL_PUB:
Serial.println("Portrait Up Back");
break;
case MMA8451_PL_PDF:
Serial.println("Portrait Down Front");
break;
case MMA8451_PL_PDB:
Serial.println("Portrait Down Back");
break;
case MMA8451_PL_LRF:
Serial.println("Landscape Right Front");
break;
case MMA8451_PL_LRB:
Serial.println("Landscape Right Back");
break;
case MMA8451_PL_LLF:
Serial.println("Landscape Left Front");
break;
case MMA8451_PL_LLB:
Serial.println("Landscape Left Back");
break;
}
Serial.println();
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment