// Arduino + Shift Register Help needed
// https://forum.arduino.cc/t/arduino-shift-register-help-needed/1323488
// Pin connected to ST_CP of 74HC595/164
int latchPin = 8;
// Pin connected to SH_CP of 74HC595/164
int clockPin = 12;
// Pin connected to DS of 74HC595/164
int dataPin = 11;
// Clear 164 output
int Clear = 9;
// holders for information you're going to pass to shifting function
byte dataRED;
byte dataGREEN;
byte dataYELLOW;
// Data to FGC Controller
byte dataToFGCU1;
byte dataToFGCU2;
byte dataToFGCU3;
byte dataArrayToFGC[4];
// Switches
const int SW1 = 7; // dataArrayYELLOW
const int SW2 = 6; // dataArrayYELLOW
const int SW3 = 5; // dataArrayYELLOW
const int SW4 = 4; // dataArrayYELLOW
const int SW5 = 3; // dataArrayYELLOW
const int SW6 = 2; // dataArrayYELLOW
const int SW7 = 22; // dataArrayYELLOW
const int SW8 = 23; // dataArrayYELLOW
const int SW9 = 24; // dataArrayGREEN
const int SW10 = 25; // dataArrayGREEN
const int SW11 = 26; // dataArrayGREEN
const int SW12 = 27; // dataArrayGREEN
const int SW13 = 28; // dataArrayGREEN
const int SW14 = 29; // dataArrayGREEN
const int SW15 = 30; // dataArrayGREEN
const int SW16 = 31; // dataArrayGREEN
const int SW17 = 32; // dataArrayRED
const int SW18 = 33; // dataArrayRED
const int SW19 = 34; // dataArrayRED
const int SW20 = 35; // dataArrayRED
const int SW21 = 36; // dataArrayRED
const int SW22 = 37; // dataArrayRED
const int SW23 = 38; // dataArrayRED
const int SW24 = 39; // dataArrayRED
// Switches to binary/hex converter
int bitVal;
String stringBit;
String stringBinary;
long binaryNumber;
int decimalNumber;
void setup()
{
// set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
digitalWrite(latchPin, LOW);
pinMode(Clear, OUTPUT);
digitalWrite(Clear, LOW); // Set low to sett 164 to LOW out. Set HIGH to receiver data.
dataArrayToFGC[0] = 0x00; // U1
dataArrayToFGC[1] = 0x00; // U2
dataArrayToFGC[2] = 0x00; // U3
Serial.begin(115200);
}
void loop()
{
ReadSwitchesU1();
Serial.print("U1 data: ");
Serial.print(dataArrayToFGC[0], HEX);
ReadSwitchesU2();
Serial.print(" ");
Serial.print("U2 data: ");
Serial.print(dataArrayToFGC[1], HEX);
ReadSwitchesU3();
Serial.print(" ");
Serial.print("U3 data: ");
Serial.println(dataArrayToFGC[2], HEX);
// set latch pin Low
digitalWrite(latchPin, LOW);
digitalWrite(Clear, HIGH);
// send data to 595/
shiftOut(dataPin, clockPin, dataArrayToFGC[0]); // Yellow Lights
shiftOut(dataPin, clockPin, dataArrayToFGC[1]); // Green Light
shiftOut(dataPin, clockPin, dataArrayToFGC[2]); // Red Light
// return the latch pin high to signal chip that it
// no longer needs to listen for information
digitalWrite(latchPin, HIGH);
digitalWrite(Clear, LOW);
delay(200); // only show to see LED data.
}
// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
// on the rising edge of the clock,
// clock idles low
// internal function setup
int i = 0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
// clear everything out just in case to
// prepare shift register for bit shifting
digitalWrite(myDataPin, LOW);
digitalWrite(myClockPin, LOW);
// for each bit in the byte myDataOut�
// NOTICE THAT WE ARE COUNTING DOWN in our for loop
// This means that %00000001 or "1" will go through such
// that it will be pin Q0 that lights.
for (i = 7; i >= 0; i--) {
digitalWrite(myClockPin, LOW);
// if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1 << i) ) {
pinState = 1;
}
else {
pinState = 0;
}
// Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
// register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, HIGH);
// zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, LOW);
}
// stop shifting
digitalWrite(myClockPin, LOW);
}
void ReadSwitchesU1() {
uint8_t data = digitalRead(SW1);
data = (data << 1) + digitalRead(SW2);
data = (data << 1) + digitalRead(SW3);
data = (data << 1) + digitalRead(SW4);
data = (data << 1) + digitalRead(SW5);
data = (data << 1) + digitalRead(SW6);
data = (data << 1) + digitalRead(SW7);
data = (data << 1) + digitalRead(SW8);
dataArrayToFGC[0] = data;
}
void ReadSwitchesU2() {
uint8_t data = digitalRead(SW9);
data = (data << 1) + digitalRead(SW10);
data = (data << 1) + digitalRead(SW11);
data = (data << 1) + digitalRead(SW12);
data = (data << 1) + digitalRead(SW13);
data = (data << 1) + digitalRead(SW14);
data = (data << 1) + digitalRead(SW15);
data = (data << 1) + digitalRead(SW16);
dataArrayToFGC[1] = data;
}
void ReadSwitchesU3() {
uint8_t data = digitalRead(SW17);
data = (data << 1) + digitalRead(SW18);
data = (data << 1) + digitalRead(SW19);
data = (data << 1) + digitalRead(SW20);
data = (data << 1) + digitalRead(SW21);
data = (data << 1) + digitalRead(SW22);
data = (data << 1) + digitalRead(SW23);
data = (data << 1) + digitalRead(SW24);
dataArrayToFGC[2] = data;
}