I'm trying out a different use of a 74hc595. My idea was to use two buttons inputs for 1 and 0 and a third pin that would write all 0's and make use of the QH pin to input the data entered into the Arduino. This is the first stage of what I hope will be a binary calculator.
My problem right now is how to store the input from the 595 in a way that will allow math to be performed. The idea is that in the future I will have two 595's and a matrix of buttons to allow AND, OR, XOR, ADD, SUB, etc functions to be performed on the two values.
I've tried atoi and several other methods to make my array into a byte but I'm stuck and could use some help. I'm not even sure storing it in an array makes sense. This is my first project and post to the forum so I hope I haven't made any mistakes.
Here is my current breadboard:
Button functions from left to right:
- Shiftout a 0 bit[1]
- Shiftout a 1 bit[1]
- Shiftout 8x 0 bit and use QH pin to read value entered by user into the arduino[1]
// // ****************************************************
// binary input with 2 buttons and a third read button
// with 1 x 74HC595 shift register
// ****************************************************
// by Philip Prescott-Decie
//*****************************************************
// Arduino to IC 74HC595
int latchPin = 12; //IC Pin 12 latch-signal
int clockPin = 11; //IC Pin 11 serial CLK
int dataPin = 13; //IC Pin 13 serial DATA input
int input = 3; //IC Pin 3 Input QH (output pin normally sent to second register is used to read in data)
int button0 = 2; // pin to connect [0] button
int button1 = 4; // pin to connect [1] button
int button2 = 5; // pin to connect [PRINTOUT] button
int buttonState0 = 0; // current state of the [1] button
int lastButtonState0 = 0; // previous state of the [1] button
int buttonState1 = 0; // current state of the [0] button
int lastButtonState1 = 0; // previous state of the [0] button
int buttonState2 = 0; // current state of the [0] button
int lastButtonState2 = 0; // previous state of the [0] button
byte pinState = 0b00000000; // store binary
int binary[] = {0, 0, 0, 0, 0, 0, 0, 0}; // Array used to store Data from IC - Perhaps possible to change to byte?
void setup()
{
// Handshake-Connections
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(button0, INPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
Serial.begin(9200);
}
void loop()
{
// read the pushbutton input pin:
buttonState0 = digitalRead(button0);
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
if (buttonState0 != lastButtonState0) {
// check for state change
if (buttonState0 == HIGH) {
// if the current state is HIGH then the button went from off to on:
Serial.println("Button 0 on");
digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
digitalWrite(clockPin, LOW);
digitalWrite(dataPin, LOW); // write a zero
digitalWrite(clockPin, HIGH);
digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that it
delay(250); //add a delay to avoid double entries
}
} else {
if (buttonState1 != lastButtonState1) {
// check for state change
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button went from off to on:
Serial.println("Button 1 on");
digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
digitalWrite(clockPin, LOW);
digitalWrite(dataPin, HIGH); // write a one
digitalWrite(clockPin, HIGH);
digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that it
delay(250); //add a delay to avoid double entries
}
} else {
if (buttonState2 != lastButtonState2) {
// check for state change
if (buttonState2 == HIGH) {
// if the current state is HIGH then the button went from off to on:
Serial.println("Button 2 on");
for (int i = 0; i < 8; i++){
binary[i]=digitalRead(input);
Serial.println(binary[i]);
digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
digitalWrite(clockPin, LOW);
digitalWrite(dataPin, LOW); // write a zero
digitalWrite(clockPin, HIGH);
digitalWrite(latchPin, HIGH); //return the latch pin high to signal chip that you are done transmitting
delay(1);
}
}
}
// Delay a little bit to avoid bouncing
delay(100);
}
}
}