Last active
January 1, 2019 14:55
-
-
Save xyzshantaram/584b0f93187c9c228a9269feb2a70d89 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
/* | |
I'm abusing four shift registers [in chains of two each] to get 32 inputs | |
*/ | |
#define NUMREGS 1 | |
#define PINSPERCHAIN 8 // used to write all 1's in loop() | |
const int interrupt[NUMREGS] = {2}; // the two interrupt pins on the arduino uno | |
const int latch[NUMREGS] = {5}; // the latch pins for our shiftreg chains | |
const int data[NUMREGS] = {7}; // the data pins for our shiftreg chains | |
const int clk[NUMREGS] = {11}; // the clock pins for our shiftreg chains | |
String processOutput(boolean hello[NUMREGS][PINSPERCHAIN]); | |
volatile boolean foo[1][8] = { // array to store the returned values from each bit | |
{false, false, false, false, false, false, false, false} | |
}; | |
void setup() { | |
// put your setup code here, to run once: | |
attachInterrupt(digitalPinToInterrupt(interrupt[0]), checkInput, RISING); | |
Serial.begin(9600); | |
Serial.print("Test String\n"); | |
for (int i = 0; i < NUMREGS; i++) { | |
pinMode(latch[i], OUTPUT); | |
pinMode(data[i], OUTPUT); | |
pinMode(clk[i], OUTPUT); | |
} | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
for (int i = 0; i < NUMREGS; i++) { | |
digitalWrite(latch[i], LOW); | |
shiftOut(data[i], clk[i], LSBFIRST, 2 ^ PINSPERCHAIN - 1); | |
digitalWrite(latch[i], HIGH); | |
} | |
} | |
void checkInput() { | |
// when the interrupt fires: | |
for (int i = 0; i < NUMREGS; i++) { // loop through each register chain's pins and write zeroes | |
for (int j = 0; j < PINSPERCHAIN; j++) { | |
writeABit(0, data[i], latch[i], clk[i]); | |
} | |
} | |
for (int i = 0; i < NUMREGS; i++) { | |
// first, write a single 1 to the first register output | |
writeABit(1, data[i], latch[i], clk[i]); | |
// now, check the interrupt pin. It should be low, since we wrote all zeroes. however, if button 0 is pressed, the interrupt pin will be high | |
// [as we wrote a single 1 to the 0th pin.] We store the check in our boolean array. Next, slowly, painfully, shift the bit we wrote high in- | |
// wards one by one and check if the interrupt pin is high [i.e.] the button is pressed. | |
for (int j = 0; j < PINSPERCHAIN; j++) { | |
int temp = digitalRead(interrupt[i]); | |
foo[i][j] = temp == 1 ? true : false; | |
if (j < PINSPERCHAIN - 1) { | |
writeABit(0, data[i], latch[i], clk[i]); | |
} | |
} | |
} | |
delay(10); | |
Serial.println(processOutput(foo)); | |
} | |
void writeABit(int b, int ds, int lat, int clk) { | |
/* digitalWrite(lat, LOW); // hold the latch low while we write data | |
digitalWrite(clk, HIGH); // the register accepts data while clk is high | |
digitalWrite(ds, (b == 1 ? 1: 0)); // write the bit | |
digitalWrite(lat, HIGH); // pull the latch high to send data | |
digitalWrite(clk, LOW); // the register stops receiving data | |
*/ | |
digitalWrite(lat, LOW); | |
digitalWrite(clk, HIGH); | |
digitalWrite(ds, (b == 1 ? 1 : 0)); | |
digitalWrite(lat, HIGH); | |
digitalWrite(clk, LOW); | |
} | |
String processOutput (boolean hello[NUMREGS][PINSPERCHAIN]) { | |
String foo = ""; | |
for (int i = 0; i < NUMREGS; i++) { | |
for (int j = 0; j < PINSPERCHAIN; j++) { | |
boolean tpry = hello[i][j]; | |
if (tpry) { | |
foo += "1"; | |
} | |
else { | |
foo += "0"; | |
} | |
} | |
foo += "\n"; | |
} | |
return foo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment