I think this is the right place to ask this. Also I've had a search of the internet and these forums but I don't seem to have come up with anything that helps which suggests that either it can't be done (which I think is very unlikely) or I've made a really simple mistake that I just can't seem to spot.
When I upload the sketch to the arduino and open the serial monitor all I get is full 0s as the input from the 595 except occasionally it will read all 1s then randomly drop different bytes until it reaches all 0s again
I've taken some photos of how I actually put it together and linked them below (you may notice that the buttons appear to be connected to gnd and the shift register but its actually +5v)
Hard to tell from the photos but have you put a signal to all the ic inputs, especially the reset? If they are not connected to 5v or gnd you can get strange effects from the floating inputs.
Just realized you are trying to use the 595 as an input device. The example uses a different chip. The 595 is for output only. You should get into the habit of reading the datasheets for the devices you use as these will spell out the capabilities.
The 74HC595 is a serial in parallel out shift register.
It can not be used with the shift in tutorial because that needs a parallel in serial out shift register.
thanks mike, so you can't use the 595 for inputs? or i need to do it in a different way? if it is possible do you know of any tutorials that could help me?
Yes the 74hc595 you can use only as a serial to parallel expander. For inputs its good to use the I2c-Bus called also TwoWireBus
and the PCF8574. I think you will find some application and hints in the Internet. On this IC you can use the pins as well as inputs and outputs.
kitte
Check it out. download the datasheet and try to understand it.
And I did use the chip 4021 and do a test code. I was using a telephone keypad.
Here the test code.
//**************************************************************//
// Name : shiftIn Example 1.1 //
// Author : Carlyn Maw //
// Date : 25 Jan, 2007 //
// Version : 1.0 //
// Notes : Code for using a CD4021B Shift Register //
// : //
//****************************************************************
// Modify code from the original
// Use only shiftIn () function
/*
--------------
1 2 3 <-- Col 1
4 5 6 <-- Col 2
7 8 9 <-- Col 3
* 0 # <-- Col 4
-------------
/\ /\ /\
| | |
Row 1 Row 2 Row 3
Telephone keypad pinouts
col 1, col 2, col 3, col 4, row 1, row 2, row3, GND
4021 connection :
col 1 : pin 7
col 2 : pin 6
col 3 : pin 5
col 4 : pin 4
row 1 : pin 13
row 2 : pin 14
row 3 : pin 15
pin 1 of the 4021 - PI-8 held HIGH with a 1 k pull-out resistor.
All keypad pins are with a 1 K pull-up resistor.
Also Serial In - pin 11 of the 4021 held HIGH with a 1 k pull-up resistor
Therefore : 1CCCCRRR
Button = 1 = 0b10111011 = 0xBB = 187
Button = 2 = 0b10111101 = 0xBD = 189
Button = 3 = 0b10111110 = 0xBE = 190
Button = 4 = 0b11011011 = 0xDB = 219
Button = 5 = 0b11011101 = 0xDD = 221
Button = 6 = 0b11011110 = 0xDE = 222
Button = 7 = 0b11101011 = 0xEB = 235
Button = 8 = 0b11101101 = 0xED = 237
Button = 9 = 0b11101110 = 0xEE = 238
Button = * = 0b11110011 = 0xF3 = 243
Button = 0 = 0b11110101 = 0xF5 = 245
Button = # = 0b11110110 = 0xF6 = 246
Button = None press = 0b11111111 = 0xFF = 255
*/
//define where your pins are
int latchPin = 10;
int dataPin = 12;
int clockPin = 11;
//Define variables to hold the data
//for shift register.
//starting with a non-zero numbers can help
//troubleshoot
byte switchVar;
void setup() {
//start serial
Serial.begin(9600);
//define pin modes
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
switchVar = 0b11111111;
}
void loop() {
//Pulse the latch pin:
//set it to 1 to collect parallel data
digitalWrite(latchPin,HIGH);
//set it to 1 to collect parallel data, wait
delayMicroseconds(20);
//set it to 0 to transmit data serially
digitalWrite(latchPin,LOW);
//while the shift register is in serial mode
//collect each shift register into a byte
//the register attached to the chip comes in first
switchVar = shiftIn(dataPin, clockPin,LSBFIRST);
//Print out the results.
//leading 0's at the top of the byte
//(7, 6, 5, etc) will be dropped before
//the first pin that has a high input
//reading
Serial.println(switchVar, BIN);
// or
Serial.println(switchVar, DEC);
//white space
Serial.println("-------------------");
//delay so all these print satements can keep up.
delay(1000);
}