Hi
Following my work on trying to understand the 4051 Multiplexer (also discussed - and MUCH helped - in this thread: Understanding the 4051Multiplexer code - Programming Questions - Arduino Forum) my next step is to have 8 buttons on a 4051 Multiplexer lighting 8 LEDs on a 74HC595 Shift Register.
I found a nice library (Shifter - http://bildr.org/2011/08/74hc595-breakout-arduino/) that greatly simplifies the process of using the shift register.
What am i trying to achieve?
When i press a button, i would like for the corresponding led to blink 3 times. So if i press button1 led1 should blink 3 times, if i press button4 led4 should blink 3 times,...
At the moment my code is like this:
#include <ClickButton.h>
#include <Shifter.h>
const int buttonPin = A0;
int buttonValue[8] = {0,0,0,0,0,0,0,0};
int lastButtonValue[8] = {0,0,0,0,0,0,0,0};
int b0 = 0;
int b1 = 0;
int b2 = 0;
#define SER_Pin 7 // SER_IN
#define RCLK_Pin 6 // L_CLOCK
#define SRCLK_Pin 5 // CLOCK
#define NUM_REGISTERS 1 //how many shift registers are in the chain
// Initialize Shifter using the Shifter library
Shifter shifter(SER_Pin, RCLK_Pin, SRCLK_Pin, NUM_REGISTERS);
void setup() {
pinMode(10,OUTPUT); // S0
pinMode(9, OUTPUT); // S1
pinMode(8, OUTPUT); // S2
Serial.begin(115200);
} // end void setup
void loop() {
for (int buttonCount = 0; buttonCount < 8; buttonCount++){
b0 = bitRead(buttonCount,0);
b1 = bitRead(buttonCount,1);
b2 = bitRead(buttonCount,2);
digitalWrite(10,b0);
digitalWrite(9,b1);
digitalWrite(8,b2);
buttonValue[buttonCount] = digitalRead(buttonPin);
if (buttonValue[buttonCount] == CLICK_SINGLECLICK && buttonValue[buttonCount] != lastButtonValue[buttonCount]) {
Serial.println(buttonCount);
ledBlinkSlow(buttonCount);
}
lastButtonValue[buttonCount] = buttonValue[buttonCount];
} // end buttonCount
} // end void loop
void ledBlinkSlow (int led2light) {
shifter.setPin(led2light,HIGH);
shifter.write();
delay(200);
shifter.clear();
shifter.write();
delay(200);
shifter.setPin(led2light,HIGH);
shifter.write();
delay(200);
shifter.clear();
shifter.write();
delay(200);
shifter.setPin(led2light,HIGH);
shifter.write();
delay(200);
shifter.clear();
shifter.write();
}
The code is more or less working as i wanted.
When i press a button the corresponding LED blinks.
But the problem is that while that LEDS is blinking nothing else can happen. It needs to wait until the ledBlinkSlow Function is over before it can go on...
Is there a way to make it so that each "button+led" can be independent of the others? That i can press a button while another is already blinking?
(i will probably use this on a midi foot controller, so i wouldn't want to have to wait a second between button pushes!)
I hope you understand what i mean...
Thanks!