Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

momentary switch multiplexed/ leds TLC5940 #120

Open
anoat opened this issue Feb 15, 2020 · 1 comment
Open

momentary switch multiplexed/ leds TLC5940 #120

anoat opened this issue Feb 15, 2020 · 1 comment

Comments

@anoat
Copy link

anoat commented Feb 15, 2020

Hello,
Firstly I want to tell you that your work is fantastic and helps me a lot in my projects!

I would like to assign momentary switches to LEDs, my switches are multiplexed (4067) and my LEDs work via TLC5940.
The idea is that when I press a switch the corresponding LED lights up and when I press it it turns off.

I got there for an LED, however for many I am struggling to find a suitable formula.

For several leds I have tried:

if (button [0,1,2] .getButtonState () == Button :: Rising)

or

if (button []. getButtonState () == Button :: Rising)

not working ...

Cordially

//code for one led 

#include <Control_Surface.h>
#include <Tlc5940.h>
USBMIDI_Interface midi;


CD74HC4067 mux = { 
  A20,               
  {24, 25, 26, 1},   
};

CCButtonLatched button[] = {

  { mux.pin(3), {53, CHANNEL_1}},
  { mux.pin(4), {54, CHANNEL_1}},
  { mux.pin(5), {55, CHANNEL_1}},
}; 


boolean x = 0 ;


void setup() {
  
  Control_Surface.begin();
  Tlc.init();
  Tlc.clear();
}

void loop() {
  Control_Surface.loop();
  Tlc.update();

 if (button[1].getButtonState() == Button::Rising)
    {  x = 1 - x ; 
    }
 
if (x>0)
  {  Tlc.set (39, 4095);
  }
else if (x<1)
  { Tlc.set (39, 0);
  }

delay(75);
  
}
@tttapa
Copy link
Owner

tttapa commented Feb 15, 2020

Indexing arrays like that is not allowed in C++. You'll have to use a for loop to iterate over the array.

I would probably use something like this (untested):

constexpr size_t numbuttons = 3;
CCButtonLatched buttons[numbuttons] = {
  { mux.pin(3), {53, CHANNEL_1}},
  { mux.pin(4), {54, CHANNEL_1}},
  { mux.pin(5), {55, CHANNEL_1}},
}; 
uint8_t tlcpins[numbuttons] = {39, 40, 41};

// ...

for (uint8_t i = 0; i < numbuttons; ++i) {
  if (buttons[i].getButtonState() == Button::Falling) {
    Tlc.set(tlcpins[i], 4095 * buttons[i].getState());
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants