propa
Well-known member
Hi my name's Richard and just got a 3.2 today and soldered on the prop shield. Went through calibration and I'm actually surprised how simple the set up process was, wicked work Teensy peeps!
So I have a project where I've got a strip of LEDs and would like to be able to move the prop shield to control a directional fade of colour, I've got some simple sketches working, and can turn on a single pixel and make it move from side to side based on the pitch orientation, done by mapping the pitch data from 0 - number of LEDs, and then clearing the strip on the next loop round.
I've got an idea for some code, but I'm not entirely sure how to execute it, I've been fiddling around with the fill_gradient, but I'm not sure how to progress it further. What I'm trying to achieve is when the prop shield is moved from right to left the pixels change into a new colour, or rainbow one direction and then the inverse rainbow the opposite direction.
Here's a shortened down version of the code I'm playing about with, does anyone have any ideas how to progress this? Or has done something similar before? Any heads up would be greatly appreciated!
Here's the full load of rambling if anyone's interested and has a strip of LEDs and a prop shield knocking around, apologies for being very hacky:
So I have a project where I've got a strip of LEDs and would like to be able to move the prop shield to control a directional fade of colour, I've got some simple sketches working, and can turn on a single pixel and make it move from side to side based on the pitch orientation, done by mapping the pitch data from 0 - number of LEDs, and then clearing the strip on the next loop round.
I've got an idea for some code, but I'm not entirely sure how to execute it, I've been fiddling around with the fill_gradient, but I'm not sure how to progress it further. What I'm trying to achieve is when the prop shield is moved from right to left the pixels change into a new colour, or rainbow one direction and then the inverse rainbow the opposite direction.
Here's a shortened down version of the code I'm playing about with, does anyone have any ideas how to progress this? Or has done something similar before? Any heads up would be greatly appreciated!
Code:
void fillGradient(float pitch, float roll) {
fill_gradient(leds, 0, CHSV(pitch,255,255), NUM_LEDS, CHSV(roll,255,255), SHORTEST_HUES);
FastLED.show();
}
Here's the full load of rambling if anyone's interested and has a strip of LEDs and a prop shield knocking around, apologies for being very hacky:
Code:
// Full orientation sensing using NXP's advanced sensor fusion algorithm.
//
// You *must* perform a magnetic calibration before this code will work.
//
// To view this data, use the Arduino Serial Monitor to watch the
// scrolling angles, or run the OrientationVisualiser example in Processing.
#include <NXPMotionSense.h>
#include <Wire.h>
#include <EEPROM.h>
#include <FastLED.h>
#define DATA_PIN 11 // output pin of Teensy to input pin of prop shield
// connect the data line of the LED strip to the LED_DAT output pin of the prop shield
#define NUM_LEDS 10 // dot 0-143
CRGB leds[NUM_LEDS];
uint8_t gHue = 0;
NXPMotionSense imu;
NXPSensorFusion filter;
void setup() {
Serial.begin(9600);
imu.begin();
filter.begin(100);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500); // power strip from USB port
pinMode(7, OUTPUT);
digitalWrite(7, HIGH); // enable access to LEDs
}
void loop() {
float ax, ay, az;
float gx, gy, gz;
float mx, my, mz;
float roll, pitch, heading;
if (imu.available()) {
// Read the motion sensors
imu.readMotionSensor(ax, ay, az, gx, gy, gz, mx, my, mz);
// Update the SensorFusion filter
filter.update(gx, gy, gz, ax, ay, az, mx, my, mz);
// print the heading, pitch and roll
roll = filter.getRoll();
pitch = filter.getPitch();
heading = filter.getYaw();
//nxp2HSV (roll, pitch, heading);
//rainbow(pitch, heading);
//FastLED.show();
//nxp2Pos(roll,pitch, heading);
fillGradient(pitch,roll);
EVERY_N_MILLISECONDS( 20 ) { gHue++; }
// FastLED.clear();
//rainbowInverse(pitch, heading);
//FastLED.clear();
// FastLED.show();
}
}
void rainbow(float pitch, float heading)
{
byte pitch2byte = map(pitch, 0,180, 0, NUM_LEDS);
fill_rainbow( leds, pitch2byte, gHue, 255/NUM_LEDS);
}
void rainbowInverse(float pitch, float heading)
{
byte pitch2byte = map(pitch, 0,180, NUM_LEDS, 0);
fill_rainbow( leds, pitch2byte, gHue, 255/NUM_LEDS);
}
void nxp2Pos(float roll, float pitch, float heading)
{
byte pitchMap = map(pitch, 0,170, 0, NUM_LEDS);
leds[pitchMap].setRGB (255,255,0);
FastLED.show();
leds[pitchMap].setRGB (0,0,0);
// for (int i = 0; i > NUM_LEDS; i++)
// {
// leds[i].setRGB (255,255,255);
//// if (i = !pitchMap)
//// {
////
////
//// //fill_rainbow( leds, NUM_LEDS, gHue, 255/NUM_LEDS);
//// }
//fill_rainbow( leds, NUM_LEDS, gHue, 255/NUM_LEDS);
FastLED.show();
// }
}
void nxp2HSV (float roll, float pitch, float heading)
{
byte rollByte = abs(roll);
byte pitchByte = abs(pitch);
byte headingByte = map(heading, 0,170, 0, 255);
Serial.print("Orientation: ");
Serial.print(roll);
Serial.print(" ");
Serial.print(pitch);
Serial.print(" ");
Serial.println(heading);
setColorHSV(pitch, 255 , 255);
FastLED.show();
}
void setColorHSV(byte h, byte s, byte v) {
// create a new HSV color
CHSV color = CHSV(h, s, v);
// use FastLED to set the color of all LEDs in the strip to the same color
fill_solid(leds, NUM_LEDS, color);
}
void oldRainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<NUM_LEDS; i++) {
leds[i].setHSV (Wheel((i+j) & 255), 255, 255);
}
FastLED.show();
delay(wait);
}
}
void fillGradient(float pitch, float roll) {
fill_gradient(leds, 0, CHSV(pitch,255,255), NUM_LEDS, CHSV(roll,255,255), SHORTEST_HUES);
FastLED.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 (255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return (0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return (WheelPos * 3, 255 - WheelPos * 3, 0);
}