Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Arduino
#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM9DS0.h>
const float Pi = 3.14159;
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(1000);
#define BUTTON_PIN 10
#define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
#define BG 1
#define PIXEL_COUNT 50
// Parameter 1 = number of pixels in strip, neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
int color_1;
int center = 0;
int step = -1;
int maxSteps = 16;
float fadeRate = 0.6;
int diff;
uint8_t mode = 1, // Current animation effect
offset = 0; // Position of spinny eyes
uint32_t color = 0x0000ff;
uint32_t color1 = 0x0000ff;
uint32_t color2 = 0xff0000;
uint32_t color3 = 0x00ff00;
uint32_t prevTime;
uint32_t currentBg = random(256);
uint32_t nextBg = currentBg;
bool oldState = HIGH;
int showType = 0;
void configureSensor(void)
{
// 1.) Set the accelerometer range
lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G);
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_4G);
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_6G);
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_8G);
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_16G);
// 2.) Set the magnetometer sensitivity
lsm.setupMag(lsm.LSM9DS0_MAGGAIN_2GAUSS);
//lsm.setupMag(lsm.LSM9DS0_MAGGAIN_4GAUSS);
//lsm.setupMag(lsm.LSM9DS0_MAGGAIN_8GAUSS);
//lsm.setupMag(lsm.LSM9DS0_MAGGAIN_12GAUSS);
// 3.) Setup the gyroscope
lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_245DPS);
//lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_500DPS);
//lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_2000DPS);
}
void displaySensorDetails(void)
{
sensor_t accel, mag, gyro, temp;
lsm.getSensor(&accel, &mag, &gyro, &temp);
Serial.println(F("------------------------------------"));
Serial.print (F("Sensor: ")); Serial.println(accel.name);
Serial.print (F("Driver Ver: ")); Serial.println(accel.version);
Serial.print (F("Unique ID: ")); Serial.println(accel.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(accel.max_value); Serial.println(F(" m/s^2"));
Serial.print (F("Min Value: ")); Serial.print(accel.min_value); Serial.println(F(" m/s^2"));
Serial.print (F("Resolution: ")); Serial.print(accel.resolution); Serial.println(F(" m/s^2"));
Serial.println(F("------------------------------------"));
Serial.println(F(""));
Serial.println(F("------------------------------------"));
Serial.print (F("Sensor: ")); Serial.println(mag.name);
Serial.print (F("Driver Ver: ")); Serial.println(mag.version);
Serial.print (F("Unique ID: ")); Serial.println(mag.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(mag.max_value); Serial.println(F(" uT"));
Serial.print (F("Min Value: ")); Serial.print(mag.min_value); Serial.println(F(" uT"));
Serial.print (F("Resolution: ")); Serial.print(mag.resolution); Serial.println(F(" uT"));
Serial.println(F("------------------------------------"));
Serial.println(F(""));
Serial.println(F("------------------------------------"));
Serial.print (F("Sensor: ")); Serial.println(gyro.name);
Serial.print (F("Driver Ver: ")); Serial.println(gyro.version);
Serial.print (F("Unique ID: ")); Serial.println(gyro.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(gyro.max_value); Serial.println(F(" rad/s"));
Serial.print (F("Min Value: ")); Serial.print(gyro.min_value); Serial.println(F(" rad/s"));
Serial.print (F("Resolution: ")); Serial.print(gyro.resolution); Serial.println(F(" rad/s"));
Serial.println(F("------------------------------------"));
Serial.println(F(""));
Serial.println(F("------------------------------------"));
Serial.print (F("Sensor: ")); Serial.println(temp.name);
Serial.print (F("Driver Ver: ")); Serial.println(temp.version);
Serial.print (F("Unique ID: ")); Serial.println(temp.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(temp.max_value); Serial.println(F(" C"));
Serial.print (F("Min Value: ")); Serial.print(temp.min_value); Serial.println(F(" C"));
Serial.print (F("Resolution: ")); Serial.print(temp.resolution); Serial.println(F(" C"));
Serial.println(F("------------------------------------"));
Serial.println(F(""));
delay(500);
}
void setup() {
{
strip.begin();
strip.setBrightness(100); // 1/3 brightness
prevTime = millis();
}
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Serial.begin(9600);
Serial.println(F("LSM9DS0 9DOF Sensor Test")); Serial.println("");
/* Initialise the sensor */
if(!lsm.begin())
{
/* There was a problem detecting the LSM9DS0 ... check your connections */
Serial.print(F("Ooops, no LSM9DS0 detected ... Check your wiring or I2C ADDR!"));
while(1);
}
Serial.println(F("Found LSM9DS0 9DOF"));
/* Display some basic information on this sensor */
displaySensorDetails();
/* Setup the sensor gain and integration time */
configureSensor();
/* We're ready to go! */
Serial.println("");
strip.begin();
strip.show();
}
void loop() {
// Get current button state.
bool newState = digitalRead(BUTTON_PIN);
// Check if state changed from high to low (button press).
if (newState == LOW && oldState == HIGH) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState = digitalRead(BUTTON_PIN);
if (newState == LOW) {
showType++;
if (showType > 8) //Amount of cases
showType=0;
}
}
startShow(showType);
// Set the last button state to the old state.
oldState = newState;
}
void ripple() {
if (BG){
if (currentBg == nextBg) {
nextBg = random(256);
}
else if (nextBg > currentBg) {
currentBg++;
} else {
currentBg--;
}
for(uint16_t l = 0; l < strip.numPixels(); l++) {
strip.setPixelColor(l, Wheel1(currentBg, 0.1));
}
} else {
for(uint16_t l = 0; l < strip.numPixels(); l++) {
strip.setPixelColor(l, 0, 0, 0);
}
}
if (step == -1) {
center = random(strip.numPixels());
color = random(256);
step = 0;
}
if (step == 0) {
strip.setPixelColor(center, Wheel1(color, 1));
step ++;
}
else {
if (step < maxSteps) {
strip.setPixelColor(wrap(center + step), Wheel1(color, pow(fadeRate, step)));
strip.setPixelColor(wrap(center - step), Wheel1(color, pow(fadeRate, step)));
if (step > 3) {
strip.setPixelColor(wrap(center + step - 3), Wheel1(color, pow(fadeRate, step - 2)));
strip.setPixelColor(wrap(center - step + 3), Wheel1(color, pow(fadeRate, step - 2)));
}
step ++;
}
else {
step = -1;
}
}
strip.show();
delay(50);
}
int wrap(int step) {
if(step < 0) return strip.numPixels() + step;
if(step > strip.numPixels() - 1) return step - strip.numPixels();
return step;
}
void startShow(int i) {
switch(i){
sensors_event_t accel, mag, gyro, temp;
lsm.getEvent(&accel, &mag, &gyro, &temp);
case 0: colorWipe(strip.Color(0, 0, 0),0 );
break;
case 1:
{
int i = random(strip.numPixels());
int j = random(strip.numPixels());
int k = random(strip.numPixels());
int q = random(strip.numPixels());
int w = random(strip.numPixels());
int e = random(strip.numPixels());
int z = random(strip.numPixels());
int x = random(strip.numPixels());
int c = random(strip.numPixels());
strip.setPixelColor(i, color1);
strip.setPixelColor(j, color2);
strip.setPixelColor(k, color3);
strip.setPixelColor(q, color1);
strip.setPixelColor(w, color2);
strip.setPixelColor(e, color3);
strip.setPixelColor(z, color1);
strip.setPixelColor(x, color2);
strip.setPixelColor(c, color3);
strip.show();
delay(100);
strip.setPixelColor(i, 0);
strip.setPixelColor(j, 0);
strip.setPixelColor(k, 0);
strip.setPixelColor(q, 0);
strip.setPixelColor(w, 0);
strip.setPixelColor(e, 0);
strip.setPixelColor(z, 0);
strip.setPixelColor(x, 0);
strip.setPixelColor(c, 0);
strip.show();
}
break;
case 2:
for(i=0; i<50; i++) {
uint32_t c = 0;
if(((offset + i) & 29) < 15) c = color;
else if(((offset + i) & 29) < 30) c = color2;
strip.setPixelColor( i, c); // First eye
strip.setPixelColor(49-i, c); // Second eye (flipped)
}
strip.show();
offset++;
delay(50);
break;
case 3: ripple();
break;
case 4: colorWipe(strip.Color(255, 0, 0), 0);
break;
case 5: colorWipe(strip.Color(0, 255, 0), 0);
break;
case 6: colorWipe(strip.Color(0, 0, 255), 0);
break;
case 7: Compass();
break;
case 8: GyroFade(gyro.gyro.x);
break;
}
}
// Purple Values
const int purple_R = 165;
const int purple_G = 0;
const int purple_B = 255;
// Orange Values
const int orange_R = 255;
const int orange_G = 115;
const int orange_B = 0;
// Blue Values
const int blue_R = 0;
const int blue_G = 0;
const int blue_B = 255;
// Gyro Range
const int gyroMin = -80;
const int gyroMax = 40;
uint32_t GyroFade(int gyroValue)
{
sensor_t accel, mag, gyro, temp;
lsm.getSensor(&accel, &mag, &gyro, &temp);
gyroValue = constrain(gyroValue, gyroMin, gyroMax);
if (gyroValue < 0) // orange-purple fade
{
int red = map(gyroValue, gyroMin, 0, purple_R, orange_R);
int green = map(gyroValue, gyroMin, 0, purple_G, orange_G);
int blue = map(gyroValue, gyroMin, 0, purple_B, orange_B);
return strip.Color(red, green, blue);
}
else // orange-blue fade
{
int red = map(gyroValue, 0, gyroMax, orange_R, blue_R);
int green = map(gyroValue, 0, gyroMax, orange_G, blue_G);
int blue = map(gyroValue, 0, gyroMax, orange_B, blue_B);
return strip.Color(red, green, blue);
}
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
}
}
void Compass()
{
// Read the magnetometer and determine the compass heading:
sensor_t accel, mag, gyro, temp;
lsm.read();
// Calculate the angle of the vector y,x from magnetic North
float heading = (atan2(lsm.magData.x,lsm.magData.y) * 180) / Pi;
Serial.println(heading);
// map the heading to position on the color wheel:
int wheelPosition = map(heading, -180, 180, 0, 255);
for (int i = 0; i < strip.numPixels(); i++)
{
Serial.print("Pixel ");
Serial.print(i);
Serial.print(" = ");
Serial.println(wheelPosition);
strip.setPixelColor(i, Wheel(wheelPosition & 255));
}
strip.show();
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos)
{
WheelPos = 255 - WheelPos;
if(WheelPos < 85)
{
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170)
{
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
uint32_t Wheel1(byte WheelPos, float opacity) {
if(WheelPos < 85) {
return strip.Color((WheelPos * 3) * opacity, (255 - WheelPos * 3) * opacity, 0);
}
else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color((255 - WheelPos * 3) * opacity, 0, (WheelPos * 3) * opacity);
}
else {
WheelPos -= 170;
return strip.Color(0, (WheelPos * 3) * opacity, (255 - WheelPos * 3) * opacity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.