Hi I'm trying to make some neopixels run on a Nano whilst a button is pressed then stop when the button is released, I have located a program that does this but it does not display what I want, I also have a program that displays what I want it to but can't seem to put the two together.
Can anyone help please. Thanks Mark
This is the code that displays correctly on the neopixel leds:
#include <Adafruit_NeoPixel.h>
#define PIN 6
// 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(25, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
// Some example procedures showing how to display to the pixels:
reverseColorWipe(strip.Color( 255, 255, 255), 50); // white
delay(10);
reverseColorWipe(strip.Color(0, 0, 0), 50); // blank
delay(10);
reverseColorWipe(strip.Color( 255, 255, 255), 50); //white
delay(10);
reverseColorWipe(strip.Color(0, 0, 0), 50); //blank
delay(10);
}
void reverseColorWipe(uint32_t c, uint8_t wait)
{
for(int16_t i=(strip.numPixels()-1); i>=0; i--)
{
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
This code works fine ie, runs when a button is pressed and stops after the final loop when the button is released. I just cannot remove the unwanted part of the code and fit in the part I need.
#include <Adafruit_NeoPixel.h>
// Define pin to connect Neopixels to
#define PIN 6
//Define pin for pushbutton to connect to
const int buttonPin = 2;
const int ledPin = 13;
// Variables will change:
int buttonState = 0; // current state of the button
Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN);
uint8_t mode = 1, // Current animation effect
offset = 0; // Position of spinny eyes
uint32_t color1 = 0xF8F8FF; // White 0x006688; // Light Blue
uint32_t prevTime;
boolean turn_on=false;
void setup() {
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
strip.begin();
prevTime = millis();
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
turn_on = true;
}
else {
}
uint8_t i;
uint32_t t;
if (turn_on == true) {
digitalWrite(ledPin, HIGH);
switch(mode) {
case 0: // Spinny wheels (8 LEDs on at a time)
for(i=0; i<30; i++) {
uint32_t c = 0;
if(((offset + i) & 6) < 2) c = color1; // 2 pixels on...
// if(((offset + i) & 5) < 2) c = color1; // 4 pixels on...
strip.setPixelColor( i, c); // First eye
// strip.setPixelColor(31-i, c); // Second eye (flipped)
}
strip.show();
offset++;
delay(40);
break;
case 1: // Black Out
strip.show();
turn_on = false;
digitalWrite(ledPin, LOW);
}
t = millis();
if((t - prevTime) > 1000) { // Every 8 seconds...
mode++; // Next mode
if(mode > 1) { // End of modes?
mode = 0; // Start modes over
}
for(i=0; i<32; i++) strip.setPixelColor(i, 0);
prevTime = t;
}
}
}