Hi all,
I am experimenting with 74HC595. What I am trying to accomplish in this step is to replace 8 output pins with a shift register connected t 3 pins as usual.
I made a scetch just to see the project work with 8 input switches affecting 8 input pins and 8 outputs, each connected to a dummy diode to see it working. The code was as follows:
//Declaring input pins
byte inPin0 = A0; // pushbutton connected to analog pin A0
byte inPin1 = A1; // pushbutton connected to analog pin A1
byte inPin2 = A2; // pushbutton connected to analog pin A2
byte inPin3 = A3; // pushbutton connected to analog pin A3
byte inPin4 = A4; // pushbutton connected to analog pin A4
byte inPin5 = A5; // pushbutton connected to analog pin A5
byte inPin6 = 2; // pushbutton connected to digital pin 2
byte inPin7 = 3; // pushbutton connected to digital pin 3
//Declaring output pins
byte outPin0 = 6; // LED connected to digital pin 6
byte outPin1 = 7; // LED connected to digital pin 7
byte outPin2 = 8; // LED connected to digital pin 8
byte outPin3 = 9; // LED connected to digital pin 9
byte outPin4 = 10; // LED connected to digital pin 10
byte outPin5 = 11; // LED connected to digital pin 11
byte outPin6 = 12; // LED connected to digital pin 12
byte outPin7 = 13; // internal LED connected to digital pin 6
//Declaring variables for storing the value of each output pin
byte val0 = 0; // variable to store the read value of outPin0
byte val1 = 0; // variable to store the read value of outPin1
byte val2 = 0; // variable to store the read value of outPin2
byte val3 = 0; // variable to store the read value of outPin3
byte val4 = 0; // variable to store the read value of outPin4
byte val5 = 0; // variable to store the read value of outPin5
byte val6 = 0; // variable to store the read value of outPin6
byte val7 = 0; // variable to store the read value of outPin7
void setup() {
// Declare input variables:
pinMode(inPin0, INPUT); // sets the analog pin A0 as input
pinMode(inPin1, INPUT); // sets the analog pin A1 as input
pinMode(inPin2, INPUT); // sets the analog pin A2 as input
pinMode(inPin3, INPUT); // sets the analog pin A3 as input
pinMode(inPin4, INPUT); // sets the analog pin A4 as input
pinMode(inPin5, INPUT); // sets the analog pin A5 as input
pinMode(inPin6, INPUT); // sets the digital pin 3 as input
pinMode(inPin7, INPUT); // sets the digital pin 4 as input
// Declare output variables:
pinMode(outPin0, OUTPUT); // sets the digital pin 6 as output
pinMode(outPin1, OUTPUT); // sets the digital pin 7 as output
pinMode(outPin2, OUTPUT); // sets the digital pin 8 as output
pinMode(outPin3, OUTPUT); // sets the digital pin 9 as output
pinMode(outPin4, OUTPUT); // sets the digital pin 10 as output
pinMode(outPin5, OUTPUT); // sets the digital pin 11 as output
pinMode(outPin6, OUTPUT); // sets the digital pin 12 as output
pinMode(outPin7, OUTPUT); // sets the digital pin 13 as output
}
void loop() {
// Read input pin value and set that value on output pin:
val0 = digitalRead(inPin0); // read the input pin 0
val1 = digitalRead(inPin1); // read the input pin 1
val2 = digitalRead(inPin2); // read the input pin 2
val3 = digitalRead(inPin3); // read the input pin 3
val4 = digitalRead(inPin4); // read the input pin 4
val5 = digitalRead(inPin5); // read the input pin 5
val6 = digitalRead(inPin6); // read the input pin 6
val7 = digitalRead(inPin7); // read the input pin 7
//Set input pin value to output pin
digitalWrite(outPin0, val0); // sets connecting the LED to the button's value
digitalWrite(outPin1, val1); // sets connecting the LED to the button's value
digitalWrite(outPin2, val2); // sets connecting the LED to the button's value
digitalWrite(outPin3, val3); // sets connecting the LED to the button's value
digitalWrite(outPin4, val4); // sets connecting the LED to the button's value
digitalWrite(outPin5, val5); // sets connecting the LED to the button's value
digitalWrite(outPin6, val6); // sets connecting the LED to the button's value
digitalWrite(outPin7, val7); // sets connecting the LED to the button's value
}
It worked as intended, pushing any button or several at once lighted up corresponding diodes.
So now I am trying to learn how to replace output pins with a shift register. What I am intending to do is to make a value for the shift register according to which buttons are pressed. If button 0 is pressed the values shall be B00000001, if button 1 B00000010, if button 2 and 1 B00000110. I think that is the syntax.
So my question is, how do I make a value from val0-7 so send in to the shift register?
Code I'm working with:
//Declaring input pins
byte inPin0 = A0; // pushbutton connected to analog pin A0
byte inPin1 = A1; // pushbutton connected to analog pin A1
byte inPin2 = A2; // pushbutton connected to analog pin A2
byte inPin3 = A3; // pushbutton connected to analog pin A3
byte inPin4 = A4; // pushbutton connected to analog pin A4
byte inPin5 = A5; // pushbutton connected to analog pin A5
byte inPin6 = 2; // pushbutton connected to digital pin 2
byte inPin7 = 3; // pushbutton connected to digital pin 3
//Declaring output pins
int latchPin = 6; //Pin connected to ST_CP(pin 12) of 74HC595
int clockPin = 7; //Pin connected to SH_CP(pin 11) of 74HC595
int dataPin = 8; //Pin connected to DS(pin 14) of 74HC595
//Declaring variables for storing the value of each output pin
byte val0 = 0; // variable to store the read value of outPin0
byte val1 = 0; // variable to store the read value of outPin1
byte val2 = 0; // variable to store the read value of outPin2
byte val3 = 0; // variable to store the read value of outPin3
byte val4 = 0; // variable to store the read value of outPin4
byte val5 = 0; // variable to store the read value of outPin5
byte val6 = 0; // variable to store the read value of outPin6
byte val7 = 0; // variable to store the read value of outPin7
void setup() {
// Declare input variables:
pinMode(inPin0, INPUT); // sets the analog pin A0 as input
pinMode(inPin1, INPUT); // sets the analog pin A1 as input
pinMode(inPin2, INPUT); // sets the analog pin A2 as input
pinMode(inPin3, INPUT); // sets the analog pin A3 as input
pinMode(inPin4, INPUT); // sets the analog pin A4 as input
pinMode(inPin5, INPUT); // sets the analog pin A5 as input
pinMode(inPin6, INPUT); // sets the digital pin 3 as input
pinMode(inPin7, INPUT); // sets the digital pin 4 as input
// Declare output pins to 74HC595:
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
// Read input pin value and set that value on output pin:
val0 = digitalRead(inPin0); // read the input pin 0
val1 = digitalRead(inPin1); // read the input pin 1
val2 = digitalRead(inPin2); // read the input pin 2
val3 = digitalRead(inPin3); // read the input pin 3
val4 = digitalRead(inPin4); // read the input pin 4
val5 = digitalRead(inPin5); // read the input pin 5
val6 = digitalRead(inPin6); // read the input pin 6
val7 = digitalRead(inPin7); // read the input pin 7
//value to send shall be in the form of B00000001 with the 1's corresponding to the pressed buttons
//Set input to 74HC595
{
digitalWrite(latchPin, LOW); //Pull latch LOW to send data
shiftOut(dataPin, clockPin, MSBFIRST, valueToSend); //Send the data
digitalWrite(latchPin, HIGH); // Pull latch HIGH to stop sending data
delay(200);
}
}
Best regards
Staffan