Skip to content

Instantly share code, notes, and snippets.

@gragib
Forked from ShawnHymel/Prop_Shield_Demo.ino
Created September 6, 2016 15:44
Show Gist options
  • Save gragib/599f3cdb33085456ebcf7f23e871f314 to your computer and use it in GitHub Desktop.
Save gragib/599f3cdb33085456ebcf7f23e871f314 to your computer and use it in GitHub Desktop.
Change light color/brightness and sound frequency/volume based on the roll and pitch of the device.
/**
* Prop Shield Demo
* Author: Shawn Hymel
* Date: August 31, 2016
*
* Description:
* Connect a single WS2812B to the LED output on the Prop Shield
* and a >2W speaker to the +/- speaker output. Attach the shield
* to a Teensy 3.2 and run the NXPMotionSense calibration sketch
* along with the MotionCal application to calibrate the IMU.
*
* Run this sketch. Rock the board back and forth (pitch) to
* change the LED brightness and audio volume. Rock the board
* side to side (roll) to change the LED color and audio
* frequency.
*
* License:
* Public Domain
*/
#include <FastLED.h>
#include <NXPMotionSense.h>
#include <Wire.h>
#include <EEPROM.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioSynthWaveformSine sine1; //xy=473,452
AudioMixer4 mixer1; //xy=641,470
AudioOutputAnalog dac1; //xy=847,460
AudioConnection patchCord1(sine1, 0, mixer1, 0);
AudioConnection patchCord2(mixer1, dac1);
// GUItool: end automatically generated code
// Pins
const int LED_DATA_PIN = 7;
// Constants
const int NUM_LEDS = 1;
// Global variables
CRGB leds[NUM_LEDS];
NXPMotionSense imu;
NXPSensorFusion filter;
void setup() {
// Initialize IMU and filter
imu.begin();
filter.begin(100);
// Initialize LED
FastLED.addLeds<WS2812B, LED_DATA_PIN, GRB>(leds, 1);
// Turn on amp
AudioMemory(20);
dac1.analogReference(EXTERNAL); // much louder!
delay(50); // time for DAC voltage stable
pinMode(5, OUTPUT);
digitalWrite(5, HIGH); // turn on the amplifier
delay(10); // allow time to wake up
}
void loop() {
float ax, ay, az;
float gx, gy, gz;
float mx, my, mz;
float roll, pitch;
uint8_t hue, val;
long freq;
float vol;
// Read motion sensors and filter the results
imu.readMotionSensor(ax, ay, az, gx, gy, gz, mx, my, mz);
filter.update(gx, gy, gz, ax, ay, az, mx, my, mz);
roll = filter.getRoll();
pitch = filter.getPitch();
// Change LED hue and brightness based on the roll and pitch
hue = floatMap(roll, -90, 90, 0, 255);
val = 255 - floatMap(pitch, -90, 90, 0, 255);
leds[0] = CHSV(hue, 255, val);
FastLED.show();
// Change audio frequency and volume based on roll and pitch
freq = floatMap(roll, -90, 90, 27, 4187);
vol = 0.5 - floatMap(pitch, -90, 90, 0, 0.5);
// Set frequency and volume for sound
mixer1.gain(0, vol);
sine1.frequency(freq);
sine1.amplitude(1);
}
float floatMap(float x,
float inMin,
float inMax,
float outMin,
float outMax){
// Set bounds
if ( x < inMin ) x = inMin;
if ( x > inMax ) x = inMax;
return (x - inMin) * (outMax - outMin) /
(inMax - inMin) + outMin;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment