/*
*/
// Define Connections to 74HC595
// ST_CP pin 12
const int latchPin = 10;
// SH_CP pin 11
const int clockPin = 11;
// DS pin 2
const int switchPin = 2;
// DS pin 14
const int dataPin = 12;
// Define Connections to 74HC165
// PL pin 1
int load = 7;
// CE pin 15
int clockEnablePin = 4;
// Q7 pin 7
int dataIn = 5;
// CP pin 2
int clockIn = 6;
int incomingDecimal;
int pointsLit = 0;
int P1R = 0;
int P1G = 0;
int P3R = 0;
int P3G = 0;
int P4R = 0;
int P4G = 0;
int P11R = 0;
int P11G = 0;
String P1="";
String P3="";
String P4="";
String P11="";
unsigned long ledOnTime[8] = {0}; // Array to store the time each LED was turned on
const unsigned long holdTime = 10000; // 10 seconds in milliseconds
// Variable to store the current and last state of the button
int buttonState = LOW;
int lastButtonState = 0;
void setup()
{
// Setup Serial Monitor
Serial.begin(9600);
// Print the state of the button to the Serial Monitor
Serial.print("Button state: ");
Serial.println(buttonState);
// Setup 74HC165 connections
pinMode(load, OUTPUT);
pinMode(clockEnablePin, OUTPUT);
pinMode(clockIn, OUTPUT);
pinMode(switchPin, INPUT);
pinMode(dataIn, INPUT);
// Setup 74HC595 connections
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 255);
digitalWrite(latchPin, HIGH);
delay(500);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
digitalWrite(latchPin, HIGH);
delay(500);
}
void loop()
{
// Read the current state of the button
buttonState = digitalRead(switchPin);
// Check if the button state has changed
if (buttonState != lastButtonState) {
// Print the new button state
Serial.println("Button state changed to: ");
Serial.println(buttonState);
// Reset the button state (actually just updating the last state)
lastButtonState = buttonState;
// Perform any additional actions you need here
if (lastButtonState == 1){
Serial.println("Ejecuta codigo, lastButtonState = ");
Serial.println(lastButtonState);
lastButtonState = 0;
Serial.println("Change lastButtonState to = ");
Serial.println(lastButtonState);
// Write pulse to load pin
digitalWrite(load, LOW);
delayMicroseconds(5);
digitalWrite(load, HIGH);
delayMicroseconds(5);
// Get data from 74HC165
digitalWrite(clockIn, HIGH);
digitalWrite(clockEnablePin, LOW);
byte incoming = shiftIn(dataIn, clockIn, LSBFIRST);
digitalWrite(clockEnablePin, HIGH);
incomingDecimal = int(incoming);
// Print to serial monitor
// Serial.print("Pin States: ");
// Serial.println(incomingDecimal, BIN);
delay(100);
if (incoming != B11111111){
unsigned long currentTime = millis();
switch (incoming){
case B11111110:
P1R = 1;
P1 = "Red";
ledOnTime[0] = currentTime;
buttonState = 0;
break;
case B11111101:
P1G = 2;
P1 = "Grn";
ledOnTime[1] = currentTime;
buttonState = 0;
break;
case B11111011:
P3R = 4;
P3 = "Red";
ledOnTime[2] = currentTime;
buttonState = 0;
break;
case B11110111:
P3G = 8;
P3 = "Grn";
ledOnTime[3] = currentTime;
buttonState = 0;
break;
case B11101111:
P4R = 16;
P4 = "Red";
ledOnTime[4] = currentTime;
buttonState = 0;
break;
case B11011111:
P4G = 32;
P4 = "Grn";
ledOnTime[5] = currentTime;
buttonState = 0;
break;
case B1111111:
P11G = 128;
P11="Grn";
ledOnTime[6] = currentTime;
buttonState = 0;
break;
case B10111111:
P11R = 64;
P11 ="Red";
ledOnTime[7] = currentTime;
buttonState = 0;
break;
default:
//
buttonState = 0;
break;
}
}
}
else
{
Serial.println("Ejecuta codigo = 0");
}
// Turn off LEDs after hold time
unsigned long currentTime = millis();
if (P1R && (currentTime - ledOnTime[0] >= holdTime)) {
P1R = 0;
}
if (P1G && (currentTime - ledOnTime[1] >= holdTime)) {
P1G = 0;
}
if (P3R && (currentTime - ledOnTime[2] >= holdTime)) {
P3R = 0;
}
if (P3G && (currentTime - ledOnTime[3] >= holdTime)) {
P3G = 0;
}
if (P4R && (currentTime - ledOnTime[4] >= holdTime)) {
P4R = 0;
}
if (P4G && (currentTime - ledOnTime[5] >= holdTime)) {
P4G = 0;
}
if (P11G && (currentTime - ledOnTime[6] >= holdTime)) {
P11G = 0;
}
if (P11R && (currentTime - ledOnTime[7] >= holdTime)) {
P11R = 0;
}
pointsLit = P11G + P11R + P4G + P4R + P3G + P3R + P1G + P1R;
/*
Serial.print("Keypress: ");
Serial.print(incomingDecimal);
Serial.print(" Shifting out: ");
Serial.println(pointsLit);
*/
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, pointsLit);
digitalWrite(latchPin, HIGH);
delay(100);
}
}