1

I have two sets of 4 LEDs.

Set A (Led1 Led2, Led3, Led4) Set B (Led5 Led6, Led7, Led8)

Both sets have its own tactile button:

Tactile A, Tactile B,

Tactile A, turning on the LEDs set A and tactile B turning on the LEDs on set B.

So each time I press the tactile A, Led1 from Set A will turn on. The next press will turn on Led2 and LED1 off, then Led3 on, Led2 off, and Led4 on and led3 off.

Same for Tactile B and set B.

Tactile A will not change the set be and same tactile B.

So I came up with the below code. It works fine for Tactile A, for set A. But I am not able to turn on the set B with Tactile B. Tactile B is keep switching itself one after another the tactile B is no functioning. Each set should work with its own tactile button.

enter image description here

//Start -- Power supply on/off code

#include <Bounce2.h> 

#define POWER_PIN 13
#define power 12


//End -- Power supply on/off code

//Start -- Switching Leds
// Leds set A
#define button 2
#define led1 8
#define led2 7
#define led3 6
#define led4 5
//Leds set B
#define tactile 3
#define led5 11
#define led6 4
#define led7 9
#define led8 10
int state = 0;      //integer to hold the current state Set A
int old = 0;        //integer to hold the last state Set A
int tactitleState = 0;      //integer to hold the current state Set B
int oldState = 0;        //integer to hold the last state Set B
int buttonPoll = 0; //integer to hold the button state for set A
int tactilePoll = 0; //integer to hold the button state for set B
//End -- Switching power

//Start -- Power supply on/off code

int powerState = LOW;
Bounce b = Bounce(); // Instantiate a Bounce object

//End -- Power supply on/off code

void setup() {
  //Start -- Power supply on/off code

  b.attach(POWER_PIN, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  b.interval(25); // Use a debounce interval of 25 milliseconds
  pinMode(power, OUTPUT); // Setup the LED
  digitalWrite(power, powerState); // Turn off the LED

  //End -- Power supply on/off code

  //Start -- Switching power

  pinMode(button, INPUT); //button set as input
  pinMode(led1, OUTPUT); //Output set Led1
  pinMode(led2, OUTPUT); //Output set Led2
  pinMode(led3, OUTPUT); //Output set Led3
  pinMode(led4, OUTPUT); //Output set Led4

  digitalWrite (led1, HIGH); //set state Led1
  digitalWrite (led2, HIGH); //set state Led2
  digitalWrite (led3, HIGH); //set state Led3
  digitalWrite (led4, HIGH); //set state Led4

  digitalWrite (power, HIGH); //state of the input power 

  //Second relay
  pinMode(tactile, INPUT); //button set as input
  pinMode(led5, OUTPUT); //Output set Led5
  pinMode(led6, OUTPUT); //Output set Led6
  pinMode(led7, OUTPUT); //Output set Led7
  pinMode(led8, OUTPUT); //Output set Led8

  digitalWrite (led5, HIGH); //set state Led5
  digitalWrite (led6, HIGH); //set state Led6
  digitalWrite (led7, HIGH); //set state Led7
  digitalWrite (led8, HIGH); //set state Led8

  //End -- Switching power

}

void loop() {
  //Start -- Power supply on/off code
  b.update(); // Update the Bounce instance
  if ( b.fell() ) {  // Call code if button transitions from HIGH to LOW
    powerState = !powerState; // Toggle LED state
    digitalWrite(power, powerState); // Apply new LED state
  }
  //End -- Power supply on/off code

  //Start -- Switching power Set A. 

  buttonPoll = digitalRead(button); //Detect the state of the button
  if (buttonPoll == 1) {              //Check if the button pressed
    delay (50);                       //Delay 50ms
    buttonPoll = digitalRead (button); //Poll button again
    if (buttonPoll == 0) {           //if it is 0 considered one press
      state = old + 1;               //increas state by 1
    }
  }
  else {
    delay (100);                    //Delay 100ms
  }
  switch (state) {     //react tot button press & state
    case 1:
      digitalWrite (led1, LOW); //led1 is on
      digitalWrite (led2, HIGH); //led2 is off
      digitalWrite (led3, HIGH); //led3 is off
      digitalWrite (led4, HIGH); //led4 is off
      old = state;
      break;
    case 2:
      digitalWrite (led1, HIGH); //led1 is off
      digitalWrite (led2, LOW); //led2 is on
      digitalWrite (led3, HIGH); //led3 is off
      digitalWrite (led4, HIGH); //led4 is off
      old = state;
      break;
    case 3:
      digitalWrite (led1, HIGH); //led1 is off
      digitalWrite (led2, HIGH); //led2 is off
      digitalWrite (led3, LOW); //led3 is on
      digitalWrite (led4, HIGH); //led4 is off
      old = state;
      break;
    case 4:
      digitalWrite (led1, HIGH); //led1 is of
      digitalWrite (led2, HIGH); //led2 is off
      digitalWrite (led3, HIGH); //led3 is off
      digitalWrite (led4, LOW); //led4 is on
      old = state;
      break;
    default:                   //if the there is no state, then all leds are off
      digitalWrite (led1, HIGH); //led1 is of
      digitalWrite (led2, HIGH); //led2 is off
      digitalWrite (led3, HIGH); //led3 is off
      digitalWrite (led4, HIGH); //led4 is off
      old = 0;   // All states are reset to 0 off
      break;
  }
  //End -- Switching  led set A
  //Start -- Switching led set B

  tactilePoll = digitalRead(tactile); //Detect the state of the button
  if (tactilePoll == 1) {              //Check if the button pressed
    delay (50);                       //Delay 50ms
    tactilePoll = digitalRead (tactile); //Poll button again
    if (tactilePoll == 0) {           //if it is 0 considered one press
      tactitleState = oldState + 1;               //increas state by 1
    }
  }
  else {
    delay (100);                    //Delay 100ms
  }
  switch (tactitleState) {     //react tot button press & state
      case 1:
      digitalWrite (led5, LOW); //led5 is on
      digitalWrite (led6, HIGH); //led6 is off
      digitalWrite (led7, HIGH); //led7 is off
      digitalWrite (led8, HIGH); //led8 is off
      oldState = tactitleState;
      break;
    case 2:
      digitalWrite (led5, HIGH); //led5 is off
      digitalWrite (led6, LOW); //led6 is on
      digitalWrite (led7, HIGH); //led7 is off
      digitalWrite (led8, HIGH); //led8 is off
      oldState = tactitleState;
      break;
    case 3:
      digitalWrite (led5, HIGH); //led5 is off
      digitalWrite (led6, HIGH); //led6 is off
      digitalWrite (led7, LOW); //led7 is on
      digitalWrite (led8, HIGH); //led8 is off
      oldState = tactitleState;
      break;
    case 4:
      digitalWrite (led5, HIGH); //led5 is off
      digitalWrite (led6, HIGH); //led6 is off
      digitalWrite (led7, HIGH); //led7 is off
      digitalWrite (led8, LOW); //led8 is on
      oldState = tactitleState;
      break;
    default:                   //if the there is no state, then all leds are off
      digitalWrite (led5, HIGH); //led5 is off
      digitalWrite (led6, HIGH); //led6 is off
      digitalWrite (led7, HIGH); //led7 is off
      digitalWrite (led8, HIGH); //led8 is off
      oldState = 0;   // All states are reset to 0 off
      break;
  }
  //End -- Switching Leds set B
}
1
  • The link doesn't seem to work without an account. You code for checking if a button is pressed is very odd, and it surprises me that it even seems to work for the A set button. How are you buttons connected? Do they have a pull-up/pull-down resistor? Might I also suggest renaming you variables something less random. For example use stateA and stateB instead of state and tactileState. Idem for buttonPoll and tactilePoll. old and oldState.
    – Gerben
    Nov 22, 2020 at 15:07

1 Answer 1

1

Your code is rather hard to follow.

Your code to read a button press doesn't make much sense:

  buttonPoll = digitalRead(button); //Detect the state of the button
  if (buttonPoll == 1) {              //Check if the button pressed
    delay (50);                       //Delay 50ms
    buttonPoll = digitalRead (button); //Poll button again
    if (buttonPoll == 0) {           //if it is 0 considered one press
      state = old + 1;               //increas state by 1
    }
  }
  else {
    delay (100);                    //Delay 100ms
  }

That code will, I think, only detect a button press if you press the button for exactly 50 mS. Given that you already use the Bounce library, why don't you create Bounce instances for your LED controller buttons and use those to read those buttons as well?

Next issue: you are setting your button and tactile pins to INPUT rather than INPUT_PULLUP. Do you have pulldown resistors on those? (when your input is set to INPUT mode you need pulldown resistors, and need to connect the switches to +5V. If you use INPUT_PULLUP mode you don't need any resistors, but you connect your switches to ground. They then read HIGH until you press a switch, at which point the switch connects the pin to ground and pulls it LOW.)

Next issue: You are treating a value of 1 as pressed and 0 as not pressed. You should use the constants HIGH and LOW instead. (And note that if you use INPUT_PULLUP mode for your pins, you should wire your buttons to VCC, and treat HIGH as not pressed and LOW as button pressed.)

I would suggest renaming your variables and using arrays of LED pin numbers:

int set1Button 2;
int set1LEDs[] = {8, 7, 6, 5};
int set1State = 0; //No LEDs lit

int set2Button 3;
int set2LEDs[] = {11, 4, 9, 10};
int set2State = 0; //No LEDs lit

Pressing setButton1 should advance set1State, and pushing set2Button should advance set2State.

For value 0, set all LEDs to off. any other value, set the LED at index setXState - 1 on and all others off.

This code would work for the first set of LEDs:

for (int index = 1; index <= 4; index++) {
     int value = set1State == index ? HIGH : LOW;
     digitalWrite (set1LEDs[index-1], value); 
}
13
  • Small correction: If using INPUT_PULLUP, one should wire the button to ground, not to Vcc. And of course the array definition should look like in Mats edit of your question. As far as I know, the above array definition does not work in C/C++.
    – chrisl
    Nov 22, 2020 at 15:46
  • Chrisl, the way my answer was worded it wasn't clear that for a pin set to INPUT you need a pulldown resistor and the switch connected to +5V, but for INPUT_PULLUP mode you need to connect the switch to ground. I edited the answer to make it clearer.
    – Duncan C
    Nov 22, 2020 at 17:08
  • I work mostly in Swift these days, and couldn't remember the C/C++ static array syntax. Thanks @Mat for fixing it.
    – Duncan C
    Nov 22, 2020 at 17:09
  • @DuncanC, Can you please provide a full code? I tried but it is not working. Nov 28, 2020 at 20:15
  • This is not a code writing service, and my partial code might not fit with the rest of your sketch. Edit your question and provide the updated code at the end, along with detailed information on how it's behaving.
    – Duncan C
    Nov 28, 2020 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.