I have a project which requires me to read if the input is "HIGH". I have 15 inputs (switches), and each input has its own output (LED). I am using a Shift Register to control its inputs and outputs.
My current code looks like this (4 inputs and 4 outputs, it works though...):
#include "SPI.h"
#include "MFRC522.h"
#include "rgb_lcd.h"
#include <Wire.h>
#define SS_PIN 17
#define RST_PIN 7
int counter = 0;
int lock = 100;
MFRC522 rfid(SS_PIN, RST_PIN);
rgb_lcd lcd;
MFRC522::MIFARE_Key key;
// Define Pins
int SHIFTIN_LATCH = 22;
int SHIFTIN_DATA = 23;
int SHIFTIN_CLOCK = 21;
int numberToDisplay = 0;
#define SHIFTOUT_DATA 18
#define SHIFTOUT_LATCH 19
#define SHIFTOUT_CLOCK 20
//Define variables to hold the data
//for each shift register.
//starting with non-zero numbers can help
//troubleshoot
byte keySlotID[15] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
byte keyInsert[15] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
byte keyReg[10] = {
0,0,0,0,0,0,0,0,0,0}; // amt of reg.
void setup() {
//start serial
Serial.begin(9600);
//define pin modes
pinMode(SHIFTIN_LATCH, OUTPUT);
pinMode(SHIFTIN_CLOCK, OUTPUT);
pinMode(SHIFTIN_DATA, INPUT);
pinMode(SHIFTOUT_DATA, OUTPUT);
pinMode(SHIFTOUT_LATCH, OUTPUT);
pinMode(SHIFTOUT_CLOCK, OUTPUT);
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
lcd.begin(16, 2);
}
void KeyID(){
// Key slot indicator.
keySlotID[0] = (keyReg[0] & 0x1E) >> 1;
keySlotID[1] = (((keyReg[1] & 0x03) << 3) | ((keyReg[0] & 0xE0) >> 5)) >> 1;
keySlotID[2] = (keyReg[1] & 0x78) >> 3;
keySlotID[3] = (keyReg[2] & 0x0F);
// Key inserted indicator.
keyInsert[0] = (keyReg[0] & 0x01);
keyInsert[1] = (keyReg[0] & 0x20) >> 5;
keyInsert[2] = (keyReg[1] & 0x04) >> 2; // X
keyInsert[3] = (keyReg[1] & 0x80) >> 7; // X
}
void registerWrite(byte data1, byte data2) {
digitalWrite(SHIFTOUT_LATCH, LOW);
delay(10);
shiftOut(SHIFTOUT_DATA, SHIFTOUT_CLOCK, MSBFIRST, data2);
shiftOut(SHIFTOUT_DATA, SHIFTOUT_CLOCK, MSBFIRST, data1);
digitalWrite(SHIFTOUT_LATCH, HIGH);
delay(10);
}
void LedIFs() {
if (keyInsert[0] == 0 && keyInsert[1] == 0 && keyInsert[2] == 0 && keyInsert[3] == 0) {
registerWrite(0xFF,0);
}
else if (keyInsert[0] == 0 && keyInsert[1] == 0 && keyInsert[2] == 0 && keyInsert[3] == 1) {
registerWrite(0x40,0);
}
else if (keyInsert[0] == 0 && keyInsert[1] == 0 && keyInsert[2] == 1 && keyInsert[3] == 0) {
registerWrite(0x10,0);
}
else if (keyInsert[0] == 0 && keyInsert[1] == 0 && keyInsert[2] == 1 && keyInsert[3] == 1) {
registerWrite(0x50,0);
}
else if (keyInsert[0] == 0 && keyInsert[1] == 1 && keyInsert[2] == 0 && keyInsert[3] == 0) {
registerWrite(0x04,0);
}
else if (keyInsert[0] == 0 && keyInsert[1] == 1 && keyInsert[2] == 0 && keyInsert[3] == 1) {
registerWrite(0x44,0);
}
else if (keyInsert[0] == 0 && keyInsert[1] == 1 && keyInsert[2] == 1 && keyInsert[3] == 0) {
registerWrite(0x14,0);
}
else if (keyInsert[0] == 0 && keyInsert[1] == 1 && keyInsert[2] == 1 && keyInsert[3] == 1) {
registerWrite(0x54,0);
}
else if (keyInsert[0] == 1 && keyInsert[1] == 0 && keyInsert[2] == 0 && keyInsert[3] == 0) {
registerWrite(0x01,0);
}
else if (keyInsert[0] == 1 && keyInsert[1] == 0 && keyInsert[2] == 0 && keyInsert[3] == 1) {
registerWrite(0x41,0);
}
else if (keyInsert[0] == 1 && keyInsert[1] == 0 && keyInsert[2] == 1 && keyInsert[3] == 0) {
registerWrite(0x11,0);
}
else if (keyInsert[0] == 1 && keyInsert[1] == 0 && keyInsert[2] == 1 && keyInsert[3] == 1) {
registerWrite(0x51,0);
}
else if (keyInsert[0] == 1 && keyInsert[1] == 1 && keyInsert[2] == 0 && keyInsert[3] == 0) {
registerWrite(0x05,0);
}
else if (keyInsert[0] == 1 && keyInsert[1] == 1 && keyInsert[2] == 0 && keyInsert[3] == 1) {
registerWrite(0x45,0);
}
else if (keyInsert[0] == 1 && keyInsert[1] == 1 && keyInsert[2] == 1 && keyInsert[3] == 0) {
registerWrite(0x15,0);
}
else if (keyInsert[0] == 1 && keyInsert[1] == 1 && keyInsert[2] == 1 && keyInsert[3] == 1) {
registerWrite(0x55,0);
}
}
void loop() {
//Pulse the latch pin:
//set it to 1 to collect parallel data
digitalWrite(SHIFTIN_LATCH,1);
//set it to 1 to collect parallel data, wait
delay(1);
//set it to 0 to transmit data serially
digitalWrite(SHIFTIN_LATCH,0);
//while the shift register is in serial mode
//collect each shift register into a byte
//the register attached to the chip comes in first
keyReg[0] = ~shiftIn(SHIFTIN_DATA, SHIFTIN_CLOCK);
keyReg[1] = ~shiftIn(SHIFTIN_DATA, SHIFTIN_CLOCK);
keyReg[2] = ~shiftIn(SHIFTIN_DATA, SHIFTIN_CLOCK);
LedIFs();
KeyID();
}
The problem I'm facing is that it would be too long for me to write down 15 KeyInsert
s. That would mean I have to write about or close to 200,000 lines of code...