#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345.h>
#define debug false
#define PIN 6 // the neopixel ring is on
Adafruit_ADXL345 accel = Adafruit_ADXL345(12345);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
int indicator = 0;
const byte level = 10;
float heading;
void setup(void){
strip.begin();
strip.show();
#ifdef debug
Serial.begin(9600);
Serial.println("Accelerometer Test");
Serial.println("");
if (!accel.begin()){
Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
while(1);
}
#endif
accel.setRange(ADXL345_RANGE_8_G);
if (accel.begin()) {
colorWipe(strip.Color(55, 0, 0), 50); // Red color will go through the whole circle
}
}
void loop(void) {
sensors_event_t event;
accel.getEvent(&event);
Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" ");
// Serial.println(Adafruit_ADXL345.getX());
Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" ");
delay(10);
const float Pi = 3.14159;
// Calculate the angle of the vector y,x
heading = (atan2(event.acceleration.y, event.acceleration.x) * 180) / Pi;
// Normalize to 0-360
if (heading < 0)
{
heading = 360 + heading;
}
// use heading to set & show NeoPixels on ring
setIndicator();
showIndicator();
delay(50); // just to let things settle a bit
}
void setIndicator(){
indicator = (int)(heading / (360/16));
delay (10);
}
void showIndicator(){
int indicatorLeft = indicator - 1;
int indicatorRight = indicator + 1;
// scale / normalize to 0 - 15
if (indicatorLeft < 0){
indicatorLeft += 16;
}
if (indicatorRight > 15){
indicatorRight -= 16;
}
colorWipe(strip.Color(0, 0, level), 0); //set All Blue (background dial color)
strip.setPixelColor(indicator, level, 0, 0); // set indicator RED
strip.setPixelColor(indicatorLeft, 0, level, 0); // set indicator border GREEN
strip.setPixelColor(indicatorRight, 0, level, 0); // set indicator border GREEN
strip.show();
}
// Fill the pixels one after the other with a color
// * Borrowed from ADAFRUIT Example Code
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
// adapted from several internet sources
0 Comments
Accelerometer: Adafruit ADXL345 [link] Gyroscope: Adafruit L3GD20 [link] For the Arduino Uno, SCL: A5 and SDA: A4 This is just a simple code to run the accelerometer and gyroscope at the same time. It is mostly adapted from the library. #include <Wire.h> |
Categories
All
|