Created
December 25, 2023 22:30
-
-
Save acedrew/0539e76ef1b7a1bcaefd32a99e36e0ae to your computer and use it in GitHub Desktop.
RP2040 Light MPU based NeoPixel Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <Adafruit_NeoPixel.h> | |
// #include <I2Cdev.h> | |
// #include <Wire.h> | |
#include <MPU6050_6Axis_MotionApps20.h> | |
// MPU Constants | |
#define I2C_SCL_PIN 13 | |
#define I2C_SDA_PIN 12 | |
#define MPU_PWR_PIN 15 | |
#define MPU_GND_PIN 14 | |
#define MPU_INT_PIN 11 | |
#define BUFFER_LENGTH 64 | |
#define LED_PIN 9 | |
#define LED_COUNT 10 | |
// Parameter 1 = number of pixels in strip | |
// Parameter 2 = Arduino pin number (most are valid) | |
// Parameter 3 = pixel type flags, add together as needed: | |
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) | |
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) | |
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) | |
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) | |
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); | |
uint8_t brightnessFader = 0; | |
int lastFade = 0; | |
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across | |
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input | |
// and minimize distance between Arduino and first pixel. Avoid connecting | |
// on a live circuit...if you must, connect GND first. | |
// MPU Variable/init | |
MPU6050 mpu; | |
bool dmpReady = false; | |
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU | |
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error) | |
uint16_t packetSize; // expected DMP packet size (default is 42 bytes) | |
uint16_t fifoCount; // count of all bytes currently in FIFO | |
uint8_t fifoBuffer[BUFFER_LENGTH]; | |
uint32_t frameCounter = 0; | |
// orientation/motion vars | |
Quaternion qt; // [w, x, y, z] quaternion container | |
VectorInt16 aa; // [x, y, z] accel sensor measurements | |
VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements | |
VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements | |
VectorFloat gravity; // [x, y, z] gravity vector | |
float euler[3]; // [psi, theta, phi] Euler angle container | |
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector | |
float lastReading = 0.0; | |
uint8_t lastMotion = 0; | |
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high | |
void dmpDataReady() | |
{ | |
mpuInterrupt = true; | |
} | |
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); | |
} | |
// Fill the dots one after the other with a color | |
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); | |
} | |
} | |
void rainbow(uint8_t wait) { | |
uint16_t i, j; | |
for(j=0; j<256; j++) { | |
for(i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, Wheel((i+j) & 255)); | |
} | |
strip.show(); | |
delay(wait); | |
} | |
} | |
// Slightly different, this makes the rainbow equally distributed throughout | |
void rainbowCycle(uint8_t wait) { | |
uint16_t i, j; | |
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel | |
for(i=0; i< strip.numPixels(); i++) { | |
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); | |
} | |
strip.show(); | |
delay(wait); | |
} | |
} | |
//Theatre-style crawling lights. | |
void theaterChase(uint32_t c, uint8_t wait) { | |
for (int j=0; j<10; j++) { //do 10 cycles of chasing | |
for (int q=0; q < 3; q++) { | |
for (uint16_t i=0; i < strip.numPixels(); i=i+3) { | |
strip.setPixelColor(i+q, c); //turn every third pixel on | |
} | |
strip.show(); | |
delay(wait); | |
for (uint16_t i=0; i < strip.numPixels(); i=i+3) { | |
strip.setPixelColor(i+q, 0); //turn every third pixel off | |
} | |
} | |
} | |
} | |
//Theatre-style crawling lights with rainbow effect | |
void theaterChaseRainbow(uint8_t wait) { | |
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel | |
for (int q=0; q < 3; q++) { | |
for (uint16_t i=0; i < strip.numPixels(); i=i+3) { | |
strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on | |
} | |
strip.show(); | |
delay(wait); | |
for (uint16_t i=0; i < strip.numPixels(); i=i+3) { | |
strip.setPixelColor(i+q, 0); //turn every third pixel off | |
} | |
} | |
} | |
} | |
// Input a value 0 to 255 to get a color value. | |
// The colours are a transition r - g - b - back to r. | |
void setup() { | |
Serial.begin(115200); | |
pinMode(MPU_PWR_PIN, OUTPUT_12MA); | |
digitalWrite(MPU_PWR_PIN, LOW); | |
pinMode(MPU_GND_PIN, OUTPUT_12MA); | |
digitalWrite(MPU_GND_PIN, LOW); | |
delay(10); | |
digitalWrite(MPU_PWR_PIN, HIGH); | |
delay(10); | |
Wire.setSDA(I2C_SDA_PIN); | |
Wire.setSCL(I2C_SCL_PIN); | |
Wire.begin(); | |
Wire.setClock(400000); | |
mpu.initialize(); | |
// pinMode(INTERRUPT_PIN, INPUT); | |
devStatus = mpu.dmpInitialize(); | |
if (devStatus == 0) | |
{ | |
// Calibration Time: generate offsets and calibrate our MPU6050 | |
mpu.setFullScaleAccelRange(16); | |
mpu.setRate(5); | |
mpu.setXGyroOffset(220); | |
mpu.setYGyroOffset(76); | |
mpu.setZGyroOffset(-85); | |
mpu.setZAccelOffset(0); | |
// mpu.CalibrateAccel(6); | |
// mpu.CalibrateGyro(6); | |
mpu.PrintActiveOffsets(); | |
// turn on the DMP, now that it's ready | |
Serial.println(F("Enabling DMP...")); | |
mpu.setDMPEnabled(true); | |
// enable Arduino interrupt detection | |
Serial.print(F("Enabling interrupt detection (Arduino external interrupt ")); | |
// Serial.print(digitalPinToInterrupt(INTERRUPT_PIN)); | |
Serial.println(F(")...")); | |
attachInterrupt(digitalPinToInterrupt(MPU_INT_PIN), dmpDataReady, RISING); | |
mpuIntStatus = mpu.getIntStatus(); | |
// set our DMP Ready flag so the main loop() function knows it's okay to use it | |
Serial.println(F("DMP ready! Waiting for first interrupt...")); | |
dmpReady = true; | |
// get expected DMP packet size for later comparison | |
packetSize = mpu.dmpGetFIFOPacketSize(); | |
} | |
else | |
{ | |
// ERROR! | |
// 1 = initial memory load failed | |
// 2 = DMP configuration updates failed | |
// (if it's going to break, usually the code will be 1) | |
Serial.print(F("DMP Initialization failed (code ")); | |
Serial.print(devStatus); | |
Serial.println(F(")")); | |
} | |
strip.begin(); | |
strip.setBrightness(128); | |
strip.show(); // Initialize all pixels to 'off' | |
} | |
void loop() { | |
if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) { | |
mpu.dmpGetQuaternion(&qt, fifoBuffer); | |
mpu.dmpGetGravity(&gravity, &qt); | |
mpu.dmpGetYawPitchRoll(ypr, &qt, &gravity); | |
mpu.dmpGetAccel(&aa, fifoBuffer); | |
// Serial.print("\n"); | |
// Serial.print(qt.w); | |
// Serial.print(" "); | |
// Serial.print(qt.x); | |
// Serial.print(" "); | |
// Serial.print(qt.y); | |
// Serial.print(" "); | |
// Serial.print(qt.z); | |
// Serial.print("\t"); | |
// Serial.print(ypr[1] * 180 / PI); | |
// Serial.print("\t"); | |
// Serial.print(ypr[2] * 180 / PI); | |
// Serial.println(""); | |
} | |
// put your main code here, to run repeatedly: | |
// Fire2012WithPalette((uint8_t)(abs(qt.w*255))); | |
// Some example procedures showing how to display to the pixels: | |
// colorWipe(strip.Color((uint8_t)(abs(qt.w*255)), (uint8_t)(abs(qt.x*255)), (uint8_t)(abs(qt.y*255))), 10); // Red | |
if (brightnessFader > lastMotion && millis() - lastFade > 2) { | |
lastFade = millis(); | |
brightnessFader -= (uint8_t)(pow(brightnessFader, 0.5)); | |
} else { | |
brightnessFader = lastMotion; | |
} | |
// lastMotion = max((uint8_t)((max(max(abs(aa.x), abs(aa.y)), abs(aa.z)) - 5800) / 32.0), random(4,12)); | |
lastMotion = max((uint8_t)((max(max(abs(aa.x), abs(aa.y)), abs(aa.z)) - 1) / 32.0), random(4,12)); | |
colorWipe(strip.ColorHSV((uint16_t)abs(qt.w*255*255), 255, max(lastMotion, brightnessFader)), 10); | |
// colorWipe(strip.Color(0, 255, 0), 50); // Green | |
// colorWipe(strip.Color(0, 0, 255), 50); // Blue | |
// //colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW | |
// // Send a theater pixel chase in... | |
// theaterChase(strip.Color(127, 127, 127), 50); // White | |
// theaterChase(strip.Color(127, 0, 0), 50); // Red | |
// theaterChase(strip.Color(0, 0, 127), 50); // Blue | |
// rainbow(20); | |
// rainbowCycle(20); | |
// theaterChaseRainbow(50); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment