"PushButtonstoggleLEDsshiftregmultiplexer" example from Control Surface library

Hello.
I have tried to follow an arduino example (Pushbuttonstoggleledsshiftregmultiplexer).

This example it´s in examples (control surface/examples.old/medium.old/extendedinputoutput/)

I don´t know what am i doing wrong…if someone could help me…
Sin-t-tulo

No led lighting on any multiplexer pin press.
Here its the example code

/**
 * @brief   This example demonstrates the use of push buttons and LEDs and how 
 *          to use shift registers and analog multiplexers to save pins.  
 * 
 * Connections:  
 * 
 *   - 10: 74HC595 ST_CP
 *   - 12: 74HC595 DS
 *   - 13: 74HC595 SH_CP
 *   - 18: 74HC4067 A (COM OUT/IN)
 *   - 19: 74HC4067 S0
 *   - 20: 74HC4067 S1
 *   - 21: 74HC4067 S2
 *   - 22: 74HC4067 S3
 * 
 * Connect 16 momentary push buttons between the input pins of the 
 * multiplexer and ground.  
 * The internal pull-up resistor for the buttons will be enabled automatically,
 * so no external resistors are necessary.
 * 
 * Connect 16 LEDs (and series resistors) between the eight outputs of the
 * two shift registers and ground. 
 * 
 * Remember to connect the enable pins of both the multiplexer and the shift
 * register to ground in order to enable them.  
 * Connect the serial data output of the first shift register (QH' or Q7S) to
 * the serial input of the second shift register.
 *  
 * Pressing the first button will turn on the first LED. Pressing it again will
 * turn it off again.
 * Pressing the second button will turn on the second LED. Pressing it again will
 * turn it off again, and so on.
 * 
 * Written by PieterP, 2018-08-28
 * https://github.com/tttapa/Control-Surface
 */

#include <Control_Surface.h> // Include the Control Surface library.

/* Instantiate a multiplexer with three address lines. */
CD74HC4067 mux = { 8, {2, 3, 4, 5} };

/* The number of buttons and LEDs */
constexpr size_t n = 16;

const pin_t latchPin = 10;  // Pin connected to ST_CP of 74HC595
const pin_t dataPin  = 11;  // Pin connected to DS of 74HC595
const pin_t clockPin = 12;  // Pin connected to SH_CP of 74HC595

/* Instantiate a shift registers on the correct pins, most significant bit
 * first, and a total of 16 outputs (two eight-bit registers). */
ShiftRegisterOut<n> sreg = { dataPin, clockPin, latchPin, MSBFIRST };

/* Instantiate the momentary push buttons.
 * It generates an array of Buttons on pins:
 * { mux.pin(0), mux.pin(1) ... mux.pin(15) } */
auto buttons = generateIncrementalArray<Button, n>(mux.pin(0));

void setup() { /* Initialize everything. */
  mux.begin();
  sreg.begin();
  for (Button &button : buttons)
    button.begin();
}

void loop() { /* Check if a button is pressed, if so toggle the LED. */
  for (uint8_t i = 0; i < n; ++i)
    if (buttons[i].getState() == Button::Falling)
      sreg.digitalWrite(i, !sreg.digitalRead(i));
}

Thanks.

chichiforever:
I have tried to follow an arduino example (Pushbuttonstoggleledsshiftregmultiplexer).

This example it´s in examples (control surface/examples.old/medium.old/extendedinputoutput/)

These old examples are no longer supported in the latest version, I keep them around until I find the time to port them to the latest version, but they should not be expected to work in their present form.

One of the changes that might be relevant here is that Button::getState() no longer reads the input. You have to call Button::update() before requesting the state.

You could try using something like this (untested):

void loop() { /* Check if a button is pressed, if so toggle the LED. */
  for (uint8_t i = 0; i < n; ++i)
    if (buttons[i].update() == Button::Falling)
      sreg.digitalWrite(i, !sreg.digitalRead(i));
}

Pieter

I have tried change the word (changestate to update) as you have explained me..
But nothing happens when pressing button..no led.

But now every single pin on the multiplexer board its enabled..

before only c0 was enabled.

I’ve just tried it and the following code works as expected for me, when I press one of the buttons connected to the multiplexer, the corresponding LED turns on/off. If it doesn’t work for you, there might be an issue with your wiring.

#include <Control_Surface.h> // Include the Control Surface library.

/* Instantiate a multiplexer with four address lines. */
CD74HC4067 mux = { 8, {2, 3, 4, 5} };

/* The number of buttons and LEDs */
constexpr size_t n = 16;

const pin_t latchPin = 10;  // Pin connected to ST_CP of 74HC595
const pin_t dataPin  = 11;  // Pin connected to DS of 74HC595
const pin_t clockPin = 12;  // Pin connected to SH_CP of 74HC595

/* Instantiate a shift registers on the correct pins, most significant bit
 * first, and a total of 16 outputs (two eight-bit registers). */
ShiftRegisterOut<n> sreg = { dataPin, clockPin, latchPin, MSBFIRST };

/* Instantiate the momentary push buttons.
 * It generates an array of Buttons on pins:
 * { mux.pin(0), mux.pin(1) ... mux.pin(15) } */
auto buttons = generateIncrementalArray<Button, n>(mux.pin(0));

void setup() { /* Initialize everything. */
  mux.begin();
  sreg.begin();
  for (Button &button : buttons)
    button.begin();
}

void loop() { /* Check if a button is pressed, if so toggle the LED. */
  for (uint8_t i = 0; i < n; ++i)
    if (buttons[i].update() == Button::Falling)
      sreg.digitalWrite(i, !sreg.digitalRead(i));
}

Here no success…
i will give it another try…
But…Looking for other solutions i have found Midi Controller 97 example (you can found it on examples/Control Surface/Other/Github-issues/Other/Midi-controller-97)

So…this solution its working for me…so great!
The leds are on or off when you are selecting banks (in my case changing pedals)

My next step will be a led for each pedal on or off…could you help me in this?

Here its my code right now… (you can see i have got a ws2812b ledstrip, 6 pixels, but now only second pixel its alwayson …maybe will be great configuring ws2812b ledstrip to pedals on or off…

#include <Control_Surface.h>  /*Program--include library-- then select "control surface" library..Vamos a programar el codigo con la librería Control Surface*/
USBMIDI_Interface usbmidi;    /*initializes device as usb_midi interface...see other examples like hardwareserial,hairles etc*/

Bank<6> bank(4); /* A bank with three channels, and 6 bank settings..creamos 6 bancos con 6 ajustes posibles distintos con tres canales..el intervalo de cambio entre los valores sera de +-3, Cada vez que cambiemos de un banco a otro sumara o decrecera +-3 MIDI CC*/

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>

//Designamos nuestro pin de datos
#define PIN 40
//Designamos cuantos pixeles tenemos en nuestra cinta led RGB
#define NUMPIXELS      6

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


CCPotentiometer potentiometer4 = { A4, {42, CHANNEL_6} };  /*establecemos el pin analogico A4 para un potenciometro analogico 10k al que llamaremos potentiometer4..la salida analogica A4 mandara mensajes MIDI 42 CC en el canal 6*/
CCPotentiometer potentiometer5 = { A5, {43, CHANNEL_6} };
CCPotentiometer potentiometer6 = { A6, {44, CHANNEL_6} };
CCPotentiometer potentiometer7 = { A7, {45, CHANNEL_6} };
CCPotentiometer potentiometer8 = { A8, {46, CHANNEL_10} };
CCPotentiometer potentiometer9 = { A9, {47, CHANNEL_11} };
CCPotentiometer potentiometer10 = { A10, {48, CHANNEL_12} };
CCPotentiometer potentiometer11 = { A11, {49, CHANNEL_13} };
CCPotentiometer potentiometer12 = { A12, {50, CHANNEL_14} };
CCPotentiometer potentiometer13 = { A13, {51, CHANNEL_15} };
CCPotentiometer potentiometer14 = { A14, {MIDI_CC::Channel_Volume, CHANNEL_16} };


//CCButton button12 = { 12, {MIDI_CC::General_Purpose_Controller_8, CHANNEL_1}, };
CCButtonLatched button9 = { 9, {32, CHANNEL_1}, };   
CCButtonLatched button10 = { 10, {33, CHANNEL_1}, }; 
CCButtonLatched button22 = { 22, {37, CHANNEL_1}, }; 
CCButtonLatched button23 = { 23, {38, CHANNEL_1}, }; 
CCButtonLatched button24 = { 24, {39, CHANNEL_1}, }; 
CCButtonLatched button25 = { 25, {60, CHANNEL_1}, }; 
CCButtonLatched button26 = { 26, {61, CHANNEL_1}, }; 
CCButtonLatched button27 = { 27, {62, CHANNEL_1}, }; 
CCButtonLatched button28 = { 28, {63, CHANNEL_1}, }; 
CCButtonLatched button29 = { 29, {64, CHANNEL_1}, }; 
CCButtonLatched button30 = { 30, {65, CHANNEL_1}, }; 
CCButtonLatched button31 = { 31, {15, CHANNEL_1}, }; 
CCButtonLatched button32 = { 32, {14, CHANNEL_1}, }; 
CCButtonLatched button33 = { 33, {13, CHANNEL_1}, }; 
CCButtonLatched button34 = { 34, {12, CHANNEL_1}, }; 
CCButtonLatched button35 = { 35, {11, CHANNEL_1}, }; 


Bankable::CCPotentiometer pots[] = {    /*los potenciometros conectados A0;A1 y A2 empiezan en 67,68 y 69 respectivamente de CC y cada vez que pulsemos el boton 8 incrementaran un valor de CC hasta 4 veces (el potenciometro con CC 67 llegara al  CC 71 y luego volvera a 67 en la siguiente pulsacion del boton 8)*/
  
  {bank, A0, 66},
  {bank, A1, 67},
  {bank, A2, 68},
  {bank, A3, 69}
  };
  

Bankable::CCButtonLatched<6>  bankbuttons [] = {
  { bank, 5, 90, },   //los botones de los pines 5 y 6 empiezan en 90 y 91 respectivamente de CC y cada vez que pulsemos el boton 8 incrementaran un valor de CC hasta 4 veces (el 90 llegara al 94 y luego volvera a 90 en la siguiente pulsacion del boton 8).el boton 7 es lo contrario del 8
  { bank, 6, 91, },
   };
Bankable::CCButtonLatched<6> button6 { bank, 6, 91, };

//IncrementSelector<2> selector { bank, 2, };

IncrementDecrementSelectorLEDs<6> bankSelector = {
  bank,
  {7, 8},         // button pins
  {46, 48, 49, 50, 51, 52}, // LED pins
};  //establecemos los pines 7 y 8 como los pines para crecer o decrecer el banco configurado (cuatro ajustes distintos para ese banco configurado)

constexpr pin_t ledPin = 22;


void setup() {
  Control_Surface.begin();
  pixels.begin(); 
  pinMode(ledPin, OUTPUT);

} 

void loop() { 
  Control_Surface.loop();

   uint32_t rojo = pixels.Color(150,0,0);
  
 pixels.setPixelColor(1, rojo); // Brillo moderado en rojo (el led numero dos de la tira de leds se queda encendido con color rojo)

 pixels.show();   // Mostramos y actualizamos el color del pixel de nuestra cinta led RGB
/*lo siguiente es para los leds de los bancos*/
    // Function that checks if a given button is pressed
  auto checkButtonPressed = [](const Bankable::CCButtonLatched<6> &button) {
    return button.getButtonState() == Button::Pressed;
  };
  // If any of the push buttons is pressed
  bool pressed = std::any_of(std::begin(bankbuttons), std::end(bankbuttons),
                             checkButtonPressed);
  // Turn on the LED
  digitalWrite(ledPin, pressed);
}

For the shift registers…i have found one example working for me perfect…
(examples/control surface/arduino helpers/hardware/extended input & output/In & out/ TOGGLE LEDS.ino)

So…in this example for arduino uno:

    • SS: 74HC595 ST_CP— pin10 (SS in arduino uno) /// pin 53 (SS in arduino MEGA)
    • MOSI: 74HC595 DS— pin11(MOSI in arduino uno) /// PIN 51(mosi in arduino mega)
    • SCK: 74HC595 SH_CP— pin13(SCK in arduino uno)////pin 52 (sck in arduino mega)
      Then… follow this picture for connections on cd74hc595
      Sin-t-tulohh

With this information…no problem following the example…

I will prefer reading cc for switch on or off leds but hey…this its good for me by now

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.