hi!
I am trying to select different functions in a programm with a switch. Each function is a different programm for a led strip
I am using a code published by adafruit and I did some adaptations for my project, but the button was not really working. I posted the code at arduino forum and I was explained that the mistake is that the code is not looking at the button very often because the other functions take time and it only gets to look at the button at the end of each light display.
Somebody could guide me on the way to figure this out?
Thanks!
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define TPIXEL 128 //The total amount of pixel’s/led’s in your connected strip/stick (Default is 60)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int switchPin = 10; // switch is connected to pin 10
int val; // variable for reading the pin status
int val2;
int buttonState; // variable to hold the button state
int lightMode = 0; // how many times the button has been pressed
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Adafruit_NeoPixel strip = Adafruit_NeoPixel(TPIXEL, PIN, NEO_GRB + NEO_KHZ800);
// our RGB → eye-recognized gamma color
byte gammatable[256];
void setup() {
Serial.begin(9600); // Set up serial communication at 9600bps
strip.setBrightness(100); //adjust brightness here
pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input
pinMode(PIN, OUTPUT);
buttonState = digitalRead(switchPin); // read the initial state
strip.begin();
strip.show(); // Initialize all pixels to ‘off’
// thanks PhilB for this gamma table!
// it helps convert RGB colors to what humans see
for (int i=0; i<256; i++) {
float x = i;
x /= 255;
x = pow(x, 2.5);
x *= 255;
gammatable[i] = x;
//Serial.println(gammatable[i]);
}}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
delay (20);
val2 = digitalRead(switchPin);
if (val == val2) {
if (val != buttonState && val==LOW) { // the button state has changed!
if (lightMode == 0) {
lightMode = 1;
}
else if (lightMode == 1) {
lightMode = 2;
}
else if (lightMode == 2){
lightMode = 0;
}
}
}
buttonState = val; // save the new state in our variable
if (lightMode == 0) {
rain();
delay(20);
}
if (lightMode == 1) {
rain_rc();
delay(20);
}
if (lightMode == 2) {
rainbowCycle(10);
delay(20);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////// Rain Blue Program
void rain() {
// Create an array of 20 raindrops
const int count = 20;
int pos[count];
// Set each rain drop at the starting gate.
// Signify by a position of -1
for( int i=0; i < count; i++) {
pos[i]=-1;
}
// Main loop. Keep looping until we’ve done
// enough “frames.”
boolean done=false;
int counter = 0;
while(!done) {
// Start by turning all LEDs off:
for(int i=0; i<strip.numPixels(); i++)
strip.setPixelColor(i, 0);
// Loop for each rain drop
for( int i=0; i < count; i++) {
// If the drop is out of the starting gate,
// turn on the LED for it.
if( pos[i] >= 0 ) {
//////////////////////////////////////////////////////////COLOR
strip.setPixelColor(pos[i], strip.Color(0,0,127));
// Move the drop down one row
pos[i] -= 7;
// If we’ve fallen off the strip, but us back at the starting gate.
if( pos[i] < 0 )
pos[i]=-1;
}
// If this drop is at the starting gate, randomly
// see if we should start it falling.
if ( pos[i] == -1 && random(40) == 0 && counter < 380 ) {
// Pick one of the 6 starting spots to begin falling
pos[i] = 143-random(6);
}
strip.show();
delay(2);
}}}
//////////////////////////////////////////////////////////////////////////////////////////////// Rain Random Color Program
void rain_rc() {
// Create an array of 20 raindrops
const int count = 20;
int pos[count];
// Set each rain drop at the starting gate.
// Signify by a position of -1
for( int i=0; i < count; i++) {
pos[i]=-1;
}
// Main loop. Keep looping until we’ve done
// enough “frames.”
boolean done=false;
int counter = 0;
while(!done) {
// Start by turning all LEDs off:
for(int i=0; i<strip.numPixels(); i++)
strip.setPixelColor(i, 0);
// Loop for each rain drop
for( int i=0; i < count; i++) {
// If the drop is out of the starting gate,
// turn on the LED for it.
if( pos[i] >= 0 ) {
///////////////////////////////////////////////////////////////////////COLOR
strip.setPixelColor(pos[i], strip.Color(random(256),random(256),random(256)));
// Move the drop down one row
pos[i] -= 7;
// If we’ve fallen off the strip, but us back at the starting gate.
if( pos[i] < 0 )
pos[i]=-1;
}
// If this drop is at the starting gate, randomly
// see if we should start it falling.
if ( pos[i] == -1 && random(40) == 0 && counter < 380 ) {
// Pick one of the 6 starting spots to begin falling
pos[i] = 143-random(6);
}
strip.show();
delay(2);
}}}
//////////////////////////////////////////////////////////////////////////////////////////////// Rainbow Cycle Program - Equally distributed
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);
}
}
// 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) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}}
////////////////////////////////////////////////////////////////////////////////////////////////