Created
March 17, 2020 12:43
-
-
Save antelio/bc926599fb0f46e7f3233dce83cd4eef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* How many shift register chips are daisy-chained. | |
| */ | |
| #define NUMBER_OF_SHIFT_CHIPS 2 | |
| /* Width of data (how many ext lines). | |
| */ | |
| #define DATA_WIDTH NUMBER_OF_SHIFT_CHIPS * 8 | |
| /* Width of pulse to trigger the shift register to read and latch. | |
| */ | |
| #define PULSE_WIDTH_USEC 5 | |
| /* Optional delay between shift register reads. | |
| */ | |
| #define POLL_DELAY_MSEC 1 | |
| /* You will need to change the "int" to "long" If the | |
| * NUMBER_OF_SHIFT_CHIPS is higher than 2. | |
| */ | |
| #define BYTES_VAL_T unsigned long | |
| int ploadPin = 8; // Connects to Parallel load pin the 165 | |
| int clockEnablePin = 9; // Connects to Clock Enable pin the 165 | |
| int dataPin = 11; // Connects to the Q7 pin the 165 | |
| int clockPin = 12; // Connects to the Clock pin the 165 | |
| BYTES_VAL_T pinValues; | |
| BYTES_VAL_T oldPinValues; | |
| /* This function is essentially a "shift-in" routine reading the | |
| * serial Data from the shift register chips and representing | |
| * the state of those pins in an unsigned integer (or long). | |
| */ | |
| BYTES_VAL_T read_shift_regs() | |
| { | |
| long bitVal; | |
| BYTES_VAL_T bytesVal = 0; | |
| /* Trigger a parallel Load to latch the state of the data lines, | |
| */ | |
| digitalWrite(clockEnablePin, HIGH); | |
| digitalWrite(ploadPin, LOW); | |
| delayMicroseconds(PULSE_WIDTH_USEC); | |
| digitalWrite(ploadPin, HIGH); | |
| digitalWrite(clockEnablePin, LOW); | |
| /* Loop to read each bit value from the serial out line | |
| * of the SN74HC165N. | |
| */ | |
| for(int i = 0; i < DATA_WIDTH; i++) | |
| { | |
| bitVal = digitalRead(dataPin); | |
| /* Set the corresponding bit in bytesVal. | |
| */ | |
| bytesVal |= (bitVal << ((DATA_WIDTH-1) - i)); | |
| /* Pulse the Clock (rising edge shifts the next bit). | |
| */ | |
| digitalWrite(clockPin, HIGH); | |
| delayMicroseconds(PULSE_WIDTH_USEC); | |
| digitalWrite(clockPin, LOW); | |
| } | |
| return(bytesVal); | |
| } | |
| /* Dump the list of zones along with their current status. | |
| */ | |
| void display_pin_values() | |
| { | |
| Serial.print("Pin States:\r\n"); | |
| for(int i = 0; i < DATA_WIDTH; i++) | |
| { | |
| Serial.print(" Pin-"); | |
| Serial.print(i); | |
| Serial.print(": "); | |
| if((pinValues >> i) & 1) | |
| Serial.print("HIGH"); | |
| else | |
| Serial.print("LOW"); | |
| Serial.print("\r\n"); | |
| } | |
| Serial.print("\r\n"); | |
| } | |
| void display_pin_pressed() | |
| { | |
| for(int i = 0; i < DATA_WIDTH; i++) | |
| { | |
| if((pinValues >> i) != (oldPinValues >> i)){ | |
| Serial.print(" Pin-"); | |
| Serial.print(i); | |
| Serial.print("\r\n New Value"); | |
| Serial.print(pinValues >> i); | |
| Serial.print("\r\n Old Value"); | |
| Serial.print(oldPinValues >> i); | |
| Serial.print("\r\n"); | |
| } | |
| } | |
| } | |
| int rl11 = 23; | |
| int rl12 = 22; | |
| int rl13 = 25; | |
| int rl14 = 24; | |
| int rl15 = 27; | |
| int rl16 = 26; | |
| int rl17 = 29; | |
| int rl18 = 28; | |
| int rl21 = 31; | |
| int rl22 = 30; | |
| int rl23 = 33; | |
| int rl24 = 32; | |
| int rl25 = 35; | |
| int rl26 = 34; | |
| int rl27 = 37; | |
| int rl28 = 36; | |
| // the setup routine runs once when you press | |
| // reset: | |
| void setup() { | |
| // initialize the digital pin as an output. | |
| pinMode(rl11, OUTPUT); | |
| pinMode(rl12, OUTPUT); | |
| pinMode(rl13, OUTPUT); | |
| pinMode(rl14, OUTPUT); | |
| pinMode(rl15, OUTPUT); | |
| pinMode(rl16, OUTPUT); | |
| pinMode(rl17, OUTPUT); | |
| pinMode(rl18, OUTPUT); | |
| pinMode(rl21, OUTPUT); | |
| pinMode(rl22, OUTPUT); | |
| pinMode(rl23, OUTPUT); | |
| pinMode(rl24, OUTPUT); | |
| pinMode(rl25, OUTPUT); | |
| pinMode(rl26, OUTPUT); | |
| pinMode(rl27, OUTPUT); | |
| pinMode(rl28, OUTPUT); | |
| digitalWrite(rl11, LOW); | |
| digitalWrite(rl12, LOW); | |
| digitalWrite(rl13, LOW); | |
| digitalWrite(rl14, LOW); | |
| digitalWrite(rl15, LOW); | |
| digitalWrite(rl16, LOW); | |
| digitalWrite(rl17, LOW); | |
| digitalWrite(rl18, LOW); | |
| digitalWrite(rl21, LOW); | |
| digitalWrite(rl22, LOW); | |
| digitalWrite(rl23, LOW); | |
| digitalWrite(rl24, LOW); | |
| digitalWrite(rl25, LOW); | |
| digitalWrite(rl26, LOW); | |
| digitalWrite(rl27, LOW); | |
| digitalWrite(rl28, LOW); | |
| Serial.begin(9600); | |
| /* Initialize our digital pins... | |
| */ | |
| pinMode(ploadPin, OUTPUT); | |
| pinMode(clockEnablePin, OUTPUT); | |
| pinMode(clockPin, OUTPUT); | |
| pinMode(dataPin, INPUT); | |
| digitalWrite(clockPin, LOW); | |
| digitalWrite(ploadPin, HIGH); | |
| /* Read in and display the pin states at startup. | |
| */ | |
| pinValues = read_shift_regs(); | |
| display_pin_values(); | |
| oldPinValues = pinValues; | |
| } | |
| // the loop routine runs over and over again forever: | |
| void loop() { // wait for a second // wait for a second | |
| /* Read the state of all zones. | |
| */ | |
| pinValues = read_shift_regs(); | |
| /* If there was a chage in state, display which ones changed. | |
| */ | |
| if(pinValues != oldPinValues) | |
| { | |
| Serial.print("*Pin value change detected*\r\n"); | |
| // display_pin_values(); | |
| display_pin_pressed(); | |
| oldPinValues = pinValues; | |
| } | |
| delay(POLL_DELAY_MSEC); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment