Shiftin and Shiftout help?

Hi there,

I have recently being messing around with shiftin and shiftout examples, but i was wondering how do you take the digiatal input readings (using 8 switches) from a 4021 (shiftin) to the arduio and shiftout (with a 595) to 8 leds?

I know for the shiftout i can get rid of the set binary values because i am wanting to output the values from the shiftin switches on the 4021. I am just not sure how to combine the 2 bits of code to be able to read each individual switch and shiftin and then shiftout to each individual LED.

The shiftin example code i have being using is:

int latchPin = 8;
int dataPin = 9;
int clockPin = 7;


void setup() {
  Serial.begin(9600);  // start serial
  pinMode(latchPin, OUTPUT); // setup pin modes
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, INPUT);
}

void loop()
{
  // Pulse the latch pin
  digitalWrite(latchPin, HIGH);
  delayMicroseconds(20);
  digitalWrite(latchPin, LOW);

  
  for (int i=0; i<8; i++)
  {
    
    // read data
    delay(5);
    int temp = digitalRead(dataPin);
    Serial.print(temp);

    // trigger next input (LOW -> HIGH)
    digitalWrite(clockPin, HIGH);
    delayMicroseconds(2);
    digitalWrite(clockPin, LOW);
    delayMicroseconds(2);
  }

  Serial.println();
  delay(50);
}

shiftout example code:

int data = 11; // DS
int clock = 12; // SHCP
int latch = 10; // STCP

void setup()
{
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(10, OUTPUT);
}

int k1 = B00100100;
int k2 = B01001001;
int k3 = B10010010;

void loop()
{
  digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, k1);
  digitalWrite(latch, HIGH);
  delay(100);

  digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, k2);
  digitalWrite(latch, HIGH);
  delay(100);

  digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, k3);
  digitalWrite(latch, HIGH);
  delay(100);
}

You just want to mimic the inputs to the outputs eh?

That should be a single shiftIn() to a variable followed by a single shoutOut() of that variable.

Probably a total 5 lines of active code, or have I missed something?.


Rob

The ShiftIn tutorial has example code and circuits http://www.arduino.cc/en/Tutorial/ShiftIn and the ShiftOut tutorial has example code and circuit diagrams http://arduino.cc/en/Tutorial/ShiftOut

Basically, once the ShiftIn has been carried out each bit of an 8 bit variable will hold a value corresponding to whether the associated button/switch was closed. That variable can be shifted out and the corresponding LED will be turned on. Is that what you are trying to do ?

yeah basically shifting in the 8bit switches and shifting out to 8bit Leds. I am just trying to understand how both work together and to minimise wires for a project later on eg. instead of having 8 wires for each individual bit i would have 3 for clock, latch and data etc. if that makes sense.

The principle really is simple. Read an 8 bit byte from 1 shift register and output it to a second one.

Like this switchVar1 = shiftIn(dataPin1, clockPin1);After that the variable switchVar1 holds the state of the buttons. ThenshiftOut(dataPin2, clockPin2, MSBFIRST, switchVar1);

The principle really is simple. Read an 8 bit byte from 1 shift register and output it to a second one.

If the logical "sense" of the switch and LED are the same, eg. switch is LOW when pushed, LED on if LOW.

I still don't really get this. There is that many examples of code out there they are all different and confusing as hell. Its ok saying use this tutorial, i have probably seen them all but still can't get any further with simple stuff. There seems to be more on shifting out data to led examples but not so much on shifting in using hardware.

for example, there is this reference to shiftin http://arduino.cc/en/Reference/ShiftIn then there is the 4021 tutorial for shiftin http://www.arduino.cc/en/Tutorial/ShiftIn

If shifting in is suppose to be as easy as the one in the reference, why is the tutorial code one big mess with having to include the shiftIn function at the bottom of the code? i know you need it to use the shiftin function its just messy and confusing

why is the tutorial code one big mess with having to include the shiftIn function at the bottom of the code?

I have no idea why that example has it's own shiftIn() function, I would suggest that it's yet one more example of crap Arduino documentation.

The guts of the program should be as simple as this.

digitalWrite(4021latchPin, HIGH);
delayMicroseconds(20);
digitalWrite(4021latchPin,LOW);
myVar = shiftIn(4021dataPin, 4021clockPin, LSBFIRST);

digitalWrite(595latchPin, LOW);
shiftOut(595dataPin, 595clockPin, LSBFIRST, myVar );  
digitalWrite(595latchPin, HIGH);

Rob

I would suggest that it's yet one more example of crap Arduino documentation.

Or, maybe it was written before shiftIn() was incorporated into the Arduino core function set.

apologies for stressing out ha. It is kinda crazy when arduino has a massive online community yet i still find it difficult to progress when tutorials work, yet its hard to relate it and modify to what you need.

I will give it a try in a bit and see how i get on.

cheers guys!

Quite possibly, but that’s the documentation in place on the official web site, still crap, just for a different reason.

Yeah I know it’s a voluntary and community thing, nobody is getting paid to write documentation. But AFAIK there is nobody looking after this stuff and they will not delegate to people who may be willing to do so (if indeed anyone is :)). The “cap on the clock line” is a classic example, we’ve been on about that for years, it could be fixed in 5 minutes.

How many tutorials use Serial.flush() in the old way? How many tutorials use a constant of 5000 to get an analogue value when on a Due it should be 3300? And now I gather we’re talking about a new analog API.

This will get worse as the libraries evolve and the board types increase.


Rob

This will get worse as the libraries evolve and the board types increase.

Or until they open the documentation up for a wider community to edit. SurferTim has been successful in getting some documentation corrected. But, I think he’s bribing someone. :slight_smile:

He must be :)

digitalWrite(4021latchPin, HIGH);
delayMicroseconds(20);
digitalWrite(4021latchPin,LOW);
myVar = shiftIn(4021dataPin, 4021clockPin, LSBFIRST);

digitalWrite(595latchPin, LOW); shiftOut(595dataPin, 595clockPin, LSBFIRST, myVar );  digitalWrite(595latchPin, HIGH);

I still can't seem to get this working. What setup/example were you going off? this http://www.arduino.cc/en/Tutorial/ShiftIn or the ones i posted?

I know that the ide doesn't like numbers before characters eg. 4021latchPin so i changed that to latchPin4021 etc

numbers before characters

Oops, my bad, that's not valid with C.

I wasn't going off anything except the top of my head, can you post your current code?


Rob

kh602: I still don't really get this. There is that many examples of code out there they are all different and confusing as hell. Its ok saying use this tutorial, i have probably seen them all but still can't get any further with simple stuff. There seems to be more on shifting out data to led examples but not so much on shifting in using hardware.

for example, there is this reference to shiftin http://arduino.cc/en/Reference/ShiftIn then there is the 4021 tutorial for shiftin http://www.arduino.cc/en/Tutorial/ShiftIn

If shifting in is suppose to be as easy as the one in the reference, why is the tutorial code one big mess with having to include the shiftIn function at the bottom of the code? i know you need it to use the shiftin function its just messy and confusing

I wondered the exact same thing....

Shiftin/shiftout is all you really need, you could forgo and simply use digital write to do the shifting yourself (but it's slower unless you mess with the bit registers directly)

The example needs updating for shiftin.

my code is

int latch4021 = 8;
int data4021 = 6;
int clock4021 = 7;

int data595 = 12; 
int clock595 = 11; 
int latch959 = 10;

byte myVar = 0;

void setup() {

  pinMode(latch4021, OUTPUT);
  pinMode(data4021, INPUT);
  pinMode(clock4021, OUTPUT);
  
  pinMode(latch959, OUTPUT);
  pinMode(data595, OUTPUT);
  pinMode(clock595, OUTPUT);
  }

void loop() {  
  
  digitalWrite(latch4021, HIGH);
  delayMicroseconds(20);
  digitalWrite(latch4021,LOW);
  myVar = shiftIn(data4021, clock4021, LSBFIRST);

  digitalWrite(latch959, LOW);
  shiftOut(data595, clock595, LSBFIRST, myVar );  
  digitalWrite(latch959, HIGH);

}

It works toggling leds with mom tactile switches however, the 8th par input (pin 1 of the 4021 to switch) doesnt seem to be toggling Q0 (pin 15 output on 595) led on/off. All the other switches with leds turn off/on but not this one switch and led. I have tied the tac switches high so the leds are always on for now until it is pushed then it is off.

another question i have, providing i can sort that other switch and led out is, is there away of making the leds latch on and off rather than momentary?

This was just posted on the developer's forum

Using the 74HC165, shiftIn sets the clock pin high before reading from the output pin, and this makes it skip the first input pin, and instead read pins 2-8, and a value from the serial data input.

I don't have time right now to check on the 4021 signal polarities but maybe that's at the heart of your problem.


Rob