switching the program when button is pressed

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!

Here is my code on Arduino:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#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;
    }
  }
Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.
Topic archived. No new replies allowed.