0
\$\begingroup\$

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.

https://www.tinkercad.com/things/cXJcRgVJzdu-start-simulating/editel?lessonid=EHD2303J3YPUS5Z&projectid=OIYJ88OJ3OPN3EA&collectionid=OIYJ88OJ3OPN3EA&sharecode=GwYpBuLv5rWZX1Y2wyrG3B5AnuXh98v_PV0H45DvCy4

//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
}
\$\endgroup\$
  • \$\begingroup\$ Please edit, select all your code and press the {} code formatting button. Then check your indentation before submitting the post. Oh, and ask a question ending with a question mark '?'. \$\endgroup\$ – Transistor Nov 22 at 0:10
  • \$\begingroup\$ @Transistor, thanks, \$\endgroup\$ – Habib Anwari Nov 22 at 0:13
  • \$\begingroup\$ There's still no actual question in your post. I don't any input pull-ups for your A and B switches. Did I miss them or did you? \$\endgroup\$ – Transistor Nov 22 at 0:22
  • \$\begingroup\$ @Transistor, the question is that the set B is keeping switching on itself. The first part of code after "void loop" works find, //Start -- Switching power Set A and //End -- Switching led set A. But set B does not work. It switches itself on randomly. \$\endgroup\$ – Habib Anwari Nov 22 at 0:26
  • \$\begingroup\$ Does your title refer to a tact(ile) button? Because a "tactical" button might look more like this \$\endgroup\$ – Spehro Pefhany Nov 22 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.