Hi all,
New to Arduino and fairly new to programming, but usually good at figuring how to change existing code. But alas I've searched and pondered and now I'm here. Anyway to the setup.
I have a NeoPixel 16 ring and a Trackball. I have the analog singles feeding into the Adruino and I can get a good reading. I can also get a number of NeoPixel coding to work. Just not together.
In essence I'm trying to get the Arduino to detect trackball movement and light the NeoPixel according to the "simple" pattern and continue for x seconds after the trackball movement stops. If there is no movement I want the NeoPixel to do the "rainbow" pattern function.
I have some additional testing code in there ie (Serial.prints), but that shouldn't interfere. Any help or guidance is much appreciated.
Here is the code:
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define LED_PIN 6
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 16
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int sensorY1 = A0; // select the input pin for the potentiometer
int sensorY2 = A1; // select the input pin for the potentiometer
int sensorX1 = A2; // select the input pin for the potentiometer
int sensorX2 = A3; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValueY1 = 0; // variable to store the value coming from the sensor
int sensorValueX2 = 0; // variable to store the value coming from the sensor
int sensorValueX1 = 0; // variable to store the value coming from the sensor
int sensorValueY2 = 0; // variable to store the value coming from the sensor
int mouseState = 0;
int lastmouseState = 0;
void setup() {
Serial.begin(9600);
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}
void loop() {
// read the value from the sensor:
sensorValueY1 = analogRead(sensorY1);
sensorValueY2 = analogRead(sensorY2);
sensorValueX1 = analogRead(sensorX1);
sensorValueX2 = analogRead(sensorX2);
//define mousestate variable
mouseState = sensorValueY1; //+ sensorValueY2+sensorValueX1+ sensorValueX2;
if (mouseState != lastmouseState) {
simple();
} else {
rainbow();
}
delay (1000);
}
void simple() {
strip.clear();
for (int i = 0; i < LED_COUNT; i++) { // For each pixel...
strip.setPixelColor(i, strip.Color(0, 150, 0));
strip.show();
//delay (50);
Serial.println("Not Equal");
Serial.println(mouseState);
Serial.println(lastmouseState);
lastmouseState = mouseState;
}
}
void rainbow() {
// Hue of first pixel runs 5 complete loops through the color wheel.
// Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
// means we'll make 5*65536/256 = 1280 passes through this outer loop:
for (long firstPixelHue = 0; firstPixelHue < 50 * 65536; firstPixelHue += 256) {
for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
// Offset pixel hue by an amount to make one full revolution of the
// color wheel (range of 65536) along the length of the strip
// (strip.numPixels() steps):
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
// optionally add saturation and value (brightness) (each 0 to 255).
// Here we're using just the single-argument hue variant. The result
// is passed through strip.gamma32() to provide 'truer' colors
// before assigning to each pixel:
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
}
strip.show(); // Update strip with new contents
Serial.println("Equal");
Serial.println(mouseState);
Serial.println(lastmouseState);
lastmouseState = mouseState;
//delay(500);
}
}