MUX to SR can this code be written better?

Hi there,

For a part in my personal project I have 8 push buttons connected to a MUX (4051). Then I have 8 bi color leds (red/yellow) connected to 2 shift registers (HC595)
The program is fairly simple when I push button 1, led 1 (red) turns on. When I push the button a second time led 1 (yellow) turn on and led 1 red turns off. When I push a third time led 1 (yellow) turns off. This is the same for all the other buttons.

Therefore I wrote a piece of code for the Arduino which does what it needs to do. I got this from reading other tutorials and mainly things I knew from my limited knowledge. My code is probably not the "correct way" to do this. I was wondering if there is a better (faster/shorter) way to do this. I came a cross things like bitwise operators, bit masks etc... and was wondering If my code needs an update with that. Please keep in mind that in a later stage data needs to be written and read from the EEPROM. Maybe this can be useful to know... or not.

here is my code:
sketch_jan31c_orig.ino (3.0 KB)

#include <Bounce.h>

/* Shift register IC2
 * QA - L9_R
 * QB - L10_R
 * QC - L11_R
 * QD - L12_R
 * QE - L13_R
 * QF - L14_R
 * QG - L15_R
 * QH - L16_R
 * 
 * Shift register IC3 
 * QA - L9_Y
 * QB - L10_Y
 * QC - L11_Y
 * QD - L12_Y
 * QE - L13_Y
 * QF - L14_Y
 * QG - L15_Y
 * QH - L16_Y
 */

//Toggle switches
#define SW1_A_pin 24 //Direction forward
#define SW1_B_pin 23 //Direction backward 
#define SW2_A_pin 26 //Bank 1 select
#define SW2_B_pin 25 //Bank 2 select  
#define SW3_A_pin 28 //Play
#define SW3_B_pin 27 //Pause

//MUX pins
#define MUX_data_pin 18
#define MUX_A_pin 17
#define MUX_B_pin 16
#define MUX_C_pin 15

//Push buttons
Bounce button[] = {

  Bounce( MUX_data_pin,10),
  Bounce( MUX_data_pin,10),  
  Bounce( MUX_data_pin,10),  
  Bounce( MUX_data_pin,10),
  Bounce( MUX_data_pin,10),
  Bounce( MUX_data_pin,10),
  Bounce( MUX_data_pin,10),
  Bounce( MUX_data_pin,10)        
  
};

int button_counter[] = {0,0,0,0,0,0,0,0};

//Shift registers

#define SR_latch_pin 13
#define SR_clock_pin 12
#define SR_data_pin 14

byte SR_data[4];

void setup() {

pinMode(MUX_data_pin, INPUT);

pinMode(SW1_A_pin, INPUT);
pinMode(SW1_B_pin, INPUT);
pinMode(SW2_A_pin, INPUT);
pinMode(SW2_B_pin, INPUT);
pinMode(SW3_A_pin, INPUT);
pinMode(SW3_B_pin, INPUT);

pinMode(MUX_A_pin, OUTPUT);
pinMode(MUX_B_pin, OUTPUT);
pinMode(MUX_C_pin, OUTPUT);

pinMode(SR_latch_pin, OUTPUT);
pinMode(SR_clock_pin, OUTPUT);
pinMode(SR_data_pin, OUTPUT);

SR_data[0] = 0x00;
SR_data[1] = 0x00;
SR_data[2] = 0x00;
SR_data[3] = 0x00;
SR_data[4] = 0x00;

Serial.begin(9600);  

}

void loop() {

//process push buttons
  for (int i = 0; i < 8; i++) { //loop 8 times for 8 PB

//select MUX pin
    int pin_A_value = bitRead(i,0);
    int pin_B_value = bitRead(i,1);
    int pin_C_value = bitRead(i,2); 

    digitalWrite(MUX_A_pin, pin_A_value);
    digitalWrite(MUX_B_pin, pin_B_value);
    digitalWrite(MUX_C_pin, pin_C_value);
 
//allow 50 us for signals to stablize    
    delayMicroseconds(50); 
        
//read push buttons    
    button[i].update();
    
    if (button[i].fallingEdge()) { 
      
      button_counter[i]++;
        
      if (button_counter[i] > 2){
        
        button_counter[i] = 0;
        }

      if (button_counter[i] == 0){
        
        bitClear (SR_data[0],i);
        bitClear (SR_data[1],i);
      }

      if (button_counter[i] == 1){

        bitClear (SR_data[1],i);
        bitSet (SR_data[0],i);
      }

      if (button_counter[i] == 2){

        bitClear (SR_data[0],i);
        bitSet (SR_data[1],i);
      }      
        
      Serial.print("Button : ");
      Serial.print(i);
      Serial.print(" pressed - Button position: ");
      Serial.println(button_counter[i]);
      Serial.print("shift register 0 data: "); 
      Serial.print(SR_data[0], BIN);
      Serial.print(" Shift register 1 data: ");
      Serial.println(SR_data[1], BIN);

      //test
      digitalWrite(SR_latch_pin, LOW);
      for (int i = 0; i < 2; i++) {
      shiftOut(SR_data_pin, SR_clock_pin, MSBFIRST, SR_data[i]);
      }
      digitalWrite(SR_latch_pin, HIGH);
      
    }    
  }

}

Anyway thanks in advance!

Jazzy

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