//#include "SPI.h"
int datapin = 33; //DS
int clockpin = 26; //SHCP
int latchpin = 25; //STCP
// We'll also declare a global variable for the data we're
// sending to the shift register:
byte data = 0;
const int dataPin = 12; /* Q7 */
const int clockPin = 27; /* CP */
const int latchPin = 14; /* PL */
const int numBits = 6; /* Set to 8 * number of shift registers */
bool BTN1 =0;
bool BTN2 =0;
bool BTN3 =0;
bool BTN4 =0;
bool BTN5 =0;
bool BTN6 =0;
#define LED6on shiftWrite(2, HIGH)
#define LED6off shiftWrite(2, LOW)
void Button_Check(){
digitalWrite(latchPin, LOW);
digitalWrite(latchPin, HIGH);
// Step 2: Shift
//Serial.print("Bits: ");
for (int i = 0; i < numBits; i++) {
int bit = digitalRead(dataPin);
if (bit == HIGH) {
if(i == 0){
BTN1 = HIGH;
Serial.println("Button 1");
return;
}
if(i == 1){
BTN2 = HIGH;
Serial.println("Button 2");
return;
}
if(i == 2){
BTN3 = HIGH;
Serial.println("Button 3");
return;
}
if(i == 3){
BTN4 = HIGH;
Serial.println("Button 4");
return;
}
if(i == 4){
BTN5 = HIGH;
Serial.println("Button 5");
return;
}
if(i == 5){
BTN6 = HIGH;
Serial.println("Button 6");
return;
}
} else {
BTN1 = LOW;
BTN2 = LOW;
BTN3 = LOW;
BTN4 = LOW;
BTN5 = LOW;
BTN6 = LOW;
}
digitalWrite(clockPin, HIGH); // Shift out the next bit
digitalWrite(clockPin, LOW);
}
}
void setup() {
Serial.begin(115200);
pinMode(dataPin, INPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
}
void loop() {
Button_Check();
if(BTN1 == HIGH){
Serial.println("Button 1 Pressed");
}
oneAfterAnother(); // All on, all off
//Serial.println();
delay(10);
}
void shiftWrite(int desiredPin, boolean desiredState){
// This function lets you make the shift register outputs
// HIGH or LOW in exactly the same way that you use digitalWrite().
bitWrite(data,desiredPin,desiredState); //Change desired bit to 0 or 1 in "data"
// Now we'll actually send that data to the shift register.
// The shiftOut() function does all the hard work of
// manipulating the data and clock pins to move the data
// into the shift register:
shiftOut(datapin, clockpin, MSBFIRST, data); //Send "data" to the shift register
//Toggle the latchPin to make "data" appear at the outputs
digitalWrite(latchpin, HIGH);
digitalWrite(latchpin, LOW);
}
void oneAfterAnother()
{
// This function will turn on all the LEDs, one-by-one,
// and then turn them off all off, one-by-one.
int index;
int delayTime = 100; // Time (milliseconds) to pause between LEDs
// Make this smaller for faster switching
if(BTN1 == HIGH){
shiftWrite(7, HIGH);
}
else{
shiftWrite(7, LOW);
}
if(BTN2 == HIGH){
shiftWrite(6, HIGH);
}
else{
shiftWrite(6, LOW);
}
if(BTN3 == HIGH){
shiftWrite(5, HIGH);
}
else{
shiftWrite(5, LOW);
}
if(BTN4 == HIGH){
shiftWrite(4, HIGH);
}
else{
shiftWrite(4, LOW);
}
if(BTN5 == HIGH){
shiftWrite(3, HIGH);
}
else{
shiftWrite(3, LOW);
}
if(BTN6 == HIGH){
//shiftWrite(2, HIGH);
LED6on;
}
else{
//shiftWrite(2, LOW);
LED6off;
}
// Turn all the LEDs on
//for(index = 0; index <= 7; index++)
//{
//shiftWrite(index, HIGH);
//delay(delayTime);
//}
// Turn all the LEDs off
//for(index = 7; index >= 0; index--)
//{
//shiftWrite(index, LOW);
//delay(delayTime);
//}
}