Hello everybody! Hey, I have another question! I have a button and LED. I'd like to change the patterns when press the button. I wrote a code and is compiling without problems, everything is OK! But when i press button, nothing changes. Can anybody explain and fix the code?! Thanx a lot!
#include <FastLED.h>
#define NUM_LEDS 16
#define DATA_PIN 3
#define CLOCK_PIN 2
CRGB leds[NUM_LEDS];
// Pin initialization for button
int buttonPin = 10;
// Values
int brightValue = 0;
int satValue = 0;
int hueValue = 0;
int ledState = LOW;
long previousMillis = 0; // will store last time LED was updated
long intervalOne = 100; // interval at which to blink (milliseconds)
long intervalTwo = 100; // interval at which to blink (milliseconds)
long intervalThree = 100; // interval at which to blink (milliseconds)
long intervalFour = 100; // interval at which to blink (milliseconds)
long intervalFive = 100; // interval at which to blink (milliseconds)
long intervalSix = 100; // interval at which to blink (milliseconds)
// Button
boolean buttonValue = true;
boolean buttonState = true;
// Pattern
int patternProgram = 0;
void setup() {
// Declare signal pins
pinMode(buttonPin,INPUT_PULLUP);
// Sanity check delay - allow reprogramming if accidently blowing power w/leds
delay(2000);
FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, GBR>(leds, NUM_LEDS);
Serial.begin(9600); // Initialize serial communication at 9600 bits per second
}
void loop() {
// Serial.println(buttonValue);
Serial.println(patternProgram);
// button presses cycle through modes
buttonValue = !digitalRead(buttonPin); // read input value and store it in val
if (buttonValue != buttonState) { // the button state has changed!
if (buttonValue == 0) { // check if the button is pressed
if (patternProgram == 0) { // if set to smooth logarithmic mapping
patternProgram = 1; // switch to stepped chromatic mapping
}
else {
if (patternProgram == 1) { //
patternProgram = 2; // switch to next mode
}
else {
if (patternProgram == 2) { //
patternProgram = 3; // switch to next mode
}
else {
if (patternProgram == 3) { //
patternProgram = 0; //switch to next mode
}
}
}
}
}
buttonState = buttonValue; // save the new state in our variable
}
Serial.println(hueValue);
}
void switcher(int patternProgram)
{
switch (patternProgram)
{
case 0:
for(int dot = 0; dot < NUM_LEDS; dot++)
{
//Clear this led the next time around the loop
leds[dot] = CRGB::Blue;
FastLED.show();
leds[dot] = CRGB::Black;
delay(40);
}
}
switch (patternProgram)
{
case 1:
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the led tate of the variable
for(int dot = 0; dot < NUM_LEDS; dot++)
//Clear this led the next time around the loop
leds[dot] = CRGB::Red;
FastLED.show();
delay(40);
// delay(100); // wait for a second
break;
}
}
first off: notice that there is an else-if statement and that you can switch/case for numbers.
And it looks like you just want to increment the patternProgram value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void loop() {
// Serial.println(buttonValue);
Serial.println(patternProgram);
// button presses cycle through modes
buttonValue = !digitalRead(buttonPin); // read input value and store it in val
if (buttonValue != buttonState) { // the button state has changed!
if (buttonValue == 0) { // check if the button is pressed
patternProgram++;
if(patternProgram >= 3)
patternProgram = 0;
}
buttonState = buttonValue; // save the new state in our variable
}
Serial.println(hueValue);
}
Next off: you don't need two switch/case for 2 cases:
switch (patternProgram)
{
case 0:
for(int dot = 0; dot < NUM_LEDS; dot++)
{
//Clear this led the next time around the loop
leds[dot] = CRGB::Blue;
FastLED.show();
leds[dot] = CRGB::Black;
delay(40);
}
break;
}
switch (patternProgram)
{case 1:
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the led tate of the variable
for(int dot = 0; dot < NUM_LEDS; dot++)
//Clear this led the next time around the loop
leds[dot] = CRGB::Red;
FastLED.show();
delay(40);
// delay(100); // wait for a second
break;
}
and the most important part:
you never call the function switcher so the pattern will never change.