ShiftIn simple explanation or program ? help !!

hello guys, i was working on the shiftin shift register here on this site but i couldn't get really what's going on with codes :S so can anyone please explain to me or has a simple program that can be understood ?

i need please something similar to the shift out like this code :

// Shift Register 1 Digit Display

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
byte leds = 0;

int potPin = A0;

void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  //_|
  // leds = B11111100; 

  // u
  //leds = B11111000; 
  
  // 0
  //leds = B11000000; 
  
  // 1
  //leds = B11011011;
  
  // 2
  //leds = B01100001;
  
  // 3
  //leds = B01001001;
  
  // 4
  //leds = B01011010;
  
  // 5
  //leds = B01001100;
  
  // 6
  //leds = B01000100;
  
  // 7
  //leds = B11011001;
  
  // 8
  //leds = B01000000;
  
  // 9
  //leds = B01001000;
  
  // H
  //leds = B01010010;
  
  updateShiftRegister();
  
}

void updateShiftRegister(){
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}

shiftIn is a function. It takes 2 arguments, both of them integers. The first argument tells the function which pin number to look for data on and the second tells it which pin to use to clock data in from the shift register. The function is defined as returning a byte. This byte will contain the 8 data bits that have been shifted in.

  switchVar1 = shiftIn(dataPin, clockPin);

This line of code calls the shiftIn function and on completion the byte read in will be in the switchVar1 variable.

There is a built in shiftIn function see http://arduino.cc/en/Reference/ShiftIn that you can call directly without having to worry about how it works or do you want more details of how the one in your code works ?

UKHeliBob: shiftIn is a function. It takes 2 arguments, both of them integers. The first argument tells the function which pin number to look for data on and the second tells it which pin to use to clock data in from the shift register. The function is defined as returning a byte. This byte will contain the 8 data bits that have been shifted in.

  switchVar1 = shiftIn(dataPin, clockPin);

This line of code calls the shiftIn function and on completion the byte read in will be in the switchVar1 variable.

There is a built in shiftIn function see http://arduino.cc/en/Reference/ShiftIn that you can call directly without having to worry about how it works or do you want more details of how the one in your code works ?

yes i know the function but i never used it, i tried the shiftout and works fine and i understood how to write codes for it but this one is more complicated, so let say byte ... = 10000010, and i need to say if switchvar1 == B10000010 , can i simply do it this way ? or i must convert it

how can i program this function is a simple way ?

byte shiftIn(int myDataPin, int myClockPin) { 
  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;

  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);
//we will be holding the clock pin high 8 times (0,..,7) at the
//end of each time through the for loop

//at the begining of each loop when we set the clock low, it will
//be doing the necessary low to high drop to cause the shift
//register's DataPin to change state based on the value
//of the next bit in its serial information flow.
//The register transmits the information about the pins from pin 7 to pin 0
//so that is why our function counts down
  for (i=7; i>=0; i--)
  {
    digitalWrite(myClockPin, 0);
    delayMicroseconds(0.2);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      //set the bit to 0 no matter what
      myDataIn = myDataIn | (1 << i);
    }
    else {
      //turn it off -- only necessary for debuging
     //print statement since myDataIn starts as 0
      pinState = 0;
    }

    //Debuging print statements
    //Serial.print(pinState);
    //Serial.print("     ");
    //Serial.println (dataIn, BIN);

    digitalWrite(myClockPin, 1);

  }
  //debuging print statements whitespace
  //Serial.println();
  //Serial.println(myDataIn, BIN);
  return myDataIn;
}
so let say byte ... = 10000010,

No byte can ever equal 10000010 so, no, you cannot do it that way.

This works though

byte data = 130;
void setup() 
{
  Serial.begin(115200);
  if (data == B10000010)
  {
    Serial.println("match");
  }
}

void loop() 
{}

how can i program this function is a simple way ?

Use the Arduino shiftIn function instead (http://arduino.cc/en/Reference/ShiftIn) as I prevoiusly suggested ?

UKHeliBob:

how can i program this function is a simple way ?

Use the Arduino shiftIn function instead (http://arduino.cc/en/Reference/ShiftIn) as I prevoiusly suggested ?

no it's not about the function it's about how to make a program for a shift in register

UKHeliBob: so let say byte ... = 10000010,

No byte can ever equal 10000010 so, no, you cannot do it that way.

why not ?! i used it before in the shift out : leds = B00000001

firashelou: no it's not about the function it's about how to make a program for a shift in register

Why not simply look at the shiftIn source code then, if all you are trying to do is replicate it?

firashelou: why not ?! i used it before in the shift out : leds = B00000001

10000010 is not equal to B10000010. The former is assumed to be in decimal if not preceded by a 0 (Ocatal), 0x (Hex), 0b (Binary), or B (Arduino-specific binary).

firashelou:

UKHeliBob:

how can i program this function is a simple way ?

Use the Arduino shiftIn function instead (http://arduino.cc/en/Reference/ShiftIn) as I prevoiusly suggested ?

no it's not about the function it's about how to make a program for a shift in register

It sounds from what you have said that you have got an example program working for shiftOut(). Is that correct ?

Have a look at this page http://www.arduino.cc/en/Tutorial/ShiftIn It includes example programs to shift in data from the shift register.

UKHeliBob:

firashelou:

UKHeliBob:

how can i program this function is a simple way ?

Use the Arduino shiftIn function instead (http://arduino.cc/en/Reference/ShiftIn) as I prevoiusly suggested ?

no it’s not about the function it’s about how to make a program for a shift in register

It sounds from what you have said that you have got an example program working for shiftOut(). Is that correct ?

Have a look at this page http://www.arduino.cc/en/Tutorial/ShiftIn It includes example programs to shift in data from the shift register.

for shiftOut() i did the tutorial here on this site and i made my own program which i put it up in a comment

for the shiftIn() examples i did read those examples and that’s why i started this topic because i didn’t understood how it works :frowning: (i mean the program i didn’t understand the program)

what does this if statement arguments mean? :

if (switchVar1 & (1 << n) ){
      //print the value of the array location
      Serial.println(note2sing[n]);
    }

and this :

myDataIn = myDataIn | (1 << i);

i am trying to make 3 push button light 3 LEDs
here is my program, i make it work, but the Leds are taking time to be on :S

but didn’t get what’s the purpose of thedelays (0.2) (20) … ?

//define where your pins are
int latchPin = 4;
int dataPin = 6;
int clockPin = 2;

int ledRed = 10;
int ledYellow = 9;
int ledGreen = 8;

//Define variables to hold the data 
//for shift register.
//starting with a non-zero numbers can help
//troubleshoot
byte switchVar1 = B00000000;  //01001000

//define an array that corresponds to values for each 
//of the shift register's pins

void setup() {
  //start serial
  Serial.begin(9600);

  //define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);
  
  pinMode(ledRed, OUTPUT); 
  pinMode(ledYellow, OUTPUT); 
  pinMode(ledGreen, OUTPUT); 

}

void loop() {

  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  digitalWrite(latchPin,1);
  //set it to 1 to collect parallel data, wait
  //delayMicroseconds(20);
  //set it to 0 to transmit data serially  
  digitalWrite(latchPin,0);

  //while the shift register is in serial mode
  //collect each shift register into a byte
  //the register attached to the chip comes in first 
  switchVar1 = shiftIn(dataPin, clockPin);

  //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  


  //This for-loop steps through the byte
  //bit by bit which holds the shift register data 
  //and if it was high (1) then it prints
  //the corresponding location in the array
 

//This following is a away to examine the whole
//byte at once and create combinations
//of settings.

//By passing the switchVar1 variable to
//a "switch" statement and comparing it against
//a set nemerical value (written in binary)
//you can create special cases

  switch (switchVar1) {
  case B00000001:
    digitalWrite(ledRed, HIGH);
    break;
  case B00000010:
    digitalWrite(ledYellow, HIGH);
    break;
  case B00000100:
    digitalWrite(ledGreen, HIGH);
    break;
  default: 
    // if nothing else matches, do the default
    Serial.println("LEDs off"); 
  }

//white space
Serial.println("-------------------");
//delay so all these print satements can keep up. 
//delay(500);

}

//------------------------------------------------end main loop

////// ----------------------------------------shiftIn function
///// just needs the location of the data pin and the clock pin
///// it returns a byte with each bit in the byte corresponding
///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0

byte shiftIn(int myDataPin, int myClockPin) { 
  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;

  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);
//we will be holding the clock pin high 8 times (0,..,7) at the
//end of each time through the for loop

//at the begining of each loop when we set the clock low, it will
//be doing the necessary low to high drop to cause the shift
//register's DataPin to change state based on the value
//of the next bit in its serial information flow.
//The register transmits the information about the pins from pin 7 to pin 0
//so that is why our function counts down
  for (i=7; i>=0; i--)
  {
    digitalWrite(myClockPin, 0);
    //delayMicroseconds(0.2);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      //set the bit to 0 no matter what
      myDataIn = myDataIn | (1 << i);
    }
    else {
      //turn it off -- only necessary for debuging
     //print statement since myDataIn starts as 0
      pinState = 0;
    }

    //Debuging print statements
    //Serial.print(pinState);
    //Serial.print("     ");
    //Serial.println (dataIn, BIN);

    digitalWrite(myClockPin, 1);

  }
  //debuging print statements whitespace
  //Serial.println();
  //Serial.println(myDataIn, BIN);
  return myDataIn;
}

hi can anyone please tell me what does this line means ?

myDataIn | (1 << i);

The | is the bit wise logic or symbol and the << is shift to the left
So this line ORs or adds a one into the myData variable in the bit position given by the value in i

What Grumpy_Mike said, except that if what you've shown is the complete statement, then it does nothing because the result is not saved anywhere.

Pete

Grumpy_Mike:
The | is the bit wise logic or symbol and the << is shift to the left
So this line ORs or adds a one into the myData variable in the bit position given by the value in i

ahhh ok so what i understand is that i must get rid of this line if i need to make 3 buttons light up 3 LEDs right ?

firashelou: ahhh ok so what i understand is that i must get rid of this line if i need to make 3 buttons light up 3 LEDs right ?

Maybe, maybe not. Without context (Code), its impossible to say.

Arrch:

firashelou:
ahhh ok so what i understand is that i must get rid of this line if i need to make 3 buttons light up 3 LEDs right ?

Maybe, maybe not. Without context (Code), its impossible to say.

here is the full code :

//define where your pins are
int latchPin = 4;
int dataPin = 6;
int clockPin = 2;

int ledRed = 10;
int ledYellow = 9;
int ledGreen = 8;

//Define variables to hold the data 
//for shift register.
//starting with a non-zero numbers can help
//troubleshoot
byte switchVar1 = B00000000;  //01001000

//define an array that corresponds to values for each 
//of the shift register's pins

void setup() {
  //start serial
  Serial.begin(9600);

  //define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);
  
  pinMode(ledRed, OUTPUT); 
  pinMode(ledYellow, OUTPUT); 
  pinMode(ledGreen, OUTPUT); 

}

void loop() {

  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  digitalWrite(latchPin,1);
  //set it to 1 to collect parallel data, wait
  //delayMicroseconds(20);
  //set it to 0 to transmit data serially  
  digitalWrite(latchPin,0);

  //while the shift register is in serial mode
  //collect each shift register into a byte
  //the register attached to the chip comes in first 
  switchVar1 = shiftIn(dataPin, clockPin, MSBFIRST);

  //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  


  //This for-loop steps through the byte
  //bit by bit which holds the shift register data 
  //and if it was high (1) then it prints
  //the corresponding location in the array
 

//This following is a away to examine the whole
//byte at once and create combinations
//of settings.

//By passing the switchVar1 variable to
//a "switch" statement and comparing it against
//a set nemerical value (written in binary)
//you can create special cases

  switch (switchVar1) {
  case B10000000:
    digitalWrite(ledRed, HIGH);
    Serial.println("on"); 
    break;
  case B01000000:
    digitalWrite(ledYellow, HIGH);
    break;
  case B00100000:
    digitalWrite(ledGreen, HIGH);
    break;
  default: 
    // if nothing else matches, do the default
    Serial.println("LEDs off"); 
  }

//white space
Serial.println("-------------------");
//delay so all these print satements can keep up. 
//delay(500);

}

//------------------------------------------------end main loop

////// ----------------------------------------shiftIn function
///// just needs the location of the data pin and the clock pin
///// it returns a byte with each bit in the byte corresponding
///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0

byte shiftIn(int myDataPin, int myClockPin) { 
  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;

  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);
//we will be holding the clock pin high 8 times (0,..,7) at the
//end of each time through the for loop

//at the begining of each loop when we set the clock low, it will
//be doing the necessary low to high drop to cause the shift
//register's DataPin to change state based on the value
//of the next bit in its serial information flow.
//The register transmits the information about the pins from pin 7 to pin 0
//so that is why our function counts down
  for (i=7; i>=0; i--)
  {
    digitalWrite(myClockPin, 0);
    //delayMicroseconds(0.2);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      //set the bit to 0 no matter what
      myDataIn = myDataIn | (1 << i);
    }
    else {
      //turn it off -- only necessary for debuging
     //print statement since myDataIn starts as 0
      pinState = 0;
    }

    //Debuging print statements
    //Serial.print(pinState);
    //Serial.print("     ");
    //Serial.println (dataIn, BIN);

    digitalWrite(myClockPin, 1);

  }
  //debuging print statements whitespace
  //Serial.println();
  //Serial.println(myDataIn, BIN);
  return myDataIn;
}

Getting rid of that line of code will destroy the shiftIn function, why do you think you need to get rid of it to do what you want? How is your hardware hooked up? Why did you create a new thread, instead of just asking the question in your other thread?

Arrch: Getting rid of that line of code will destroy the shiftIn function, why do you think you need to get rid of it to do what you want? How is your hardware hooked up? Why did you create a new thread, instead of just asking the question in your other thread?

because i didn't get any reply but the thing is this line of code was so annoying and frustrating that made me create a new thread :S