Ik ben op zoek naar de analoogwaarde van de x-, y- en z-as (op pinnen 0, 2 en 4) en als het een bewegingstoestand is, voer dan een regenboog-, cylone- en cascade-ledsequentie uit met de Adafruit Neopixel-bibliotheek.
Met behulp van de seriële verbinding zag ik dat de x-, y- en z-as worden uitgelezen bij waarden van ongeveer 300.
Toen ik de code uitvoerde, voerde het de drie Neopixel-sequenties uit (redPin op pin 3, GreenPin op pin 5 en BluePin op pin 6) en gaat dan verder, zelfs als er geen verandering is in de positie van de versnellingsmeter.
Wat doe ik verkeerd?
Ik gebruik Arduino Trinket Pro 5V en werk alleen met de x-as en RGB-strip op pin 3 om te starten.
Dankjewel voor je hulp.
#include
#include "WS2812_Definitions.h"
#define PIN1 3
#define LED_COUNT1 5
#define PIN2 5
#define LED_COUNT2 5
#define PIN3 6
#define LED_COUNT3 5
// Create an instance of the Adafruit_NeoPixel class called "leds".
// That'll be what we refer to from here on...
Adafruit_NeoPixel RedPin = Adafruit_NeoPixel(LED_COUNT1, PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel GreenPin = Adafruit_NeoPixel(LED_COUNT2, PIN2, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel BluePin = Adafruit_NeoPixel(LED_COUNT3, PIN3, NEO_GRB + NEO_KHZ800);
const int sensorPinRed = A0;
const int sensorPinGreen = A2;
const int sensorPinBlue = A4;
int analogValue = 0;
void setup()
{
RedPin.begin(); //Call this to start up the LED strip.
//GreenPin.begin();
//BluePin.begin();
clearLEDs(); //This function, defined below, turns all LEDs off...
RedPin.show(); //...but the LEDs don't actually update until you call this.
//GreenPin.show();
//BluePin.show()}
void loop()
{
do{
analogValue = analogRead(sensorPinRed);
//Ride the Rainbow Road
for (int i=0; ibottom 20 times
for (int i=0; i<20; i++)
{
//First parameter is the color, second is direction, third is ms between falls
cascade(MEDIUMSPRINGGREEN, TOP_DOWN, 100);
}
} while (analogValue>300);
}
// Implements a little larson "cylon" sanner.
// This'll run one full cycle, down one way and back the other
void cylon(unsigned long color, byte wait)
{
//weight determines how much lighter the outer "eye" colors are
const byte weight = 4;
//It'll be easier to decrement each of these colors individually
//so we'll split them out of the 24-bit color value
byte red = (color & 0xFF0000) >> 16;
byte green = (color & 0x00FF00) >> 8;
byte blue = (color & 0x0000FF);
//Start at closest LED, and move to the outside
for (int i=0; i<=LED_COUNT1-1; i++)
{
clearLEDs();
RedPin.setPixelColor(i, red, green, blue); //Set the bright middle eye
//Now set two eyes to each side to get progressively dimmer
for (int j=1; j<3; j++)
{
if (i-j >= 0)
RedPin.setPixelColor(i-j, red/(weight*j), green/(weight*j), blue/(weight*j));
if (i-j <= LED_COUNT1)
RedPin.setPixelColor(i+j, red/(weight*j), green/(weight*j), blue/(weight*j));
}
RedPin.show(); //Turn the LEDs on
delay(wait); //Delay for visibility
}
//Now we go back to where we came. Do the same thing.
for (int i=LED_COUNT1-2; i>=1; i--)
{
clearLEDs();
RedPin.setPixelColor(i, red, green, blue);
for (int j=1; j<3; j++)
{
if (i-j >= 0)
RedPin.setPixelColor(i-j, red/(weight*j), green/(weight*j), blue/(weight*j));
if (i-j <= LED_COUNT1)
RedPin.setPixelColor(i+j, red/(weight*j), green/(weight*j), blue/(weight*j));
}
RedPin.show();
delay(wait);
}
}
// Cascades a single direction. One time.
void cascade(unsigned long color, byte direction, byte wait)
{
if (direction == TOP_DOWN)
{
for (int i=0; i=0; i--)
{
clearLEDs();
RedPin.setPixelColor(i, color);
RedPin.show();
delay(wait);
}
}
}
// Sets all LEDs to off, but DOES NOT update the display;
// call leds.show() to actually turn them off after this.
void clearLEDs()
{
for (int i=0; iorange->green->...->violet for 0-191.
RedPin.setPixelColor(i, rainbowOrder((rainbowScale * (i + startPosition)) % 192));
}
//Finally, actually turn the LEDs on:
RedPin.show();
}