Shift register output to serial monitor

Hi,
I have some code below that I would like to utilise to read parallel bits in (upto 64 bits) and send them to the serial monitor. My issue is, I would like to put a comma separating each bit i.e 0,1,0,1,0,1,0,0. Currently it outputs 00000000. This will allow me to look at the bits individually in Excel. I've attached the ShiftIn.h library too if anyone has any ideas, thanks!

#include <ShiftIn.h>

// Init ShiftIn instance with one chip.
// The number in brackets defines the number of daisy-chained 74HC165 chips
// So if you are using two chips, you would write: ShiftIn<2> shift;
ShiftIn<1> shift;

void setup() {
  Serial.begin(9600);
  // declare pins: pLoadPin, clockEnablePin, dataPin, clockPin
  shift.begin(7, 4, 5, 6);
}

void displayValues() {
  for(int i = 0; i < shift.getDataWidth(); i++)
    Serial.print( shift.state(i) ); // get state of button i
  Serial.println();
}

void loop() {
  if(shift.update()) // read in all values. returns true if any button has changed
    displayValues();
  delay(1000);
}

ShiftIn.h (4.22 KB)

How about adding a Serial.print(",") after each Serial.print( shift.state(i) )?

Please note that you need you will need { and } for the body of the for-loop.

I think you just need to change this

void displayValues() {
  for(int i = 0; i < shift.getDataWidth(); i++)
    Serial.print( shift.state(i) ); // get state of button i
  Serial.println();
}

to this

void displayValues() {
  for(int i = 0; i < shift.getDataWidth(); i++) {
      Serial.print( shift.state(i) ); // get state of button i
      Serial.print(','); // comma
   }
  Serial.println();
}

...R

Thanks for the quick replies all,

I did put an additional serial print as suggested but it's not quite where I wanted it. It now puts the comma at the end of the bit i.e 00000000,

I think it may need to be comma separated inside the .h?

Show your new code.

#include <ShiftIn.h>

// Init ShiftIn instance with one chip.
// The number in brackets defines the number of daisy-chained 74HC165 chips
// So if you are using two chips, you would write: ShiftIn<2> shift;
ShiftIn<1> shift;

void setup() {
  Serial.begin(9600);
  // declare pins: pLoadPin, clockEnablePin, dataPin, clockPin
  shift.begin(7, 4, 5, 6);
}

void displayValues() {
  for(int i = 0; i < shift.getDataWidth(); i++)
    Serial.print( shift.state(i) ); // get state of button i
    Serial.print(',');//comma
  Serial.println();
}

void loop() {
  if(shift.update()) // read in all values. returns true if any button has changed
    displayValues();
  delay(1000);
}

You did not listen to what I said about { and } and what Robin demonstrated.

Format your code using auto-format and see where that new line is placed. It's not part of the for-loop as can be seen below.

void displayValues() {
  for (int i = 0; i < shift.getDataWidth(); i++)
    Serial.print( shift.state(i) ); // get state of button i
  Serial.print(',');//comma
  Serial.println();
}

Just did Tool>Autoformat (if that's what you meant?) and little has changed as below:

#include <ShiftIn.h>

// Init ShiftIn instance with one chip.
// The number in brackets defines the number of daisy-chained 74HC165 chips
// So if you are using two chips, you would write: ShiftIn<2> shift;
ShiftIn<1> shift;

void setup() {
  Serial.begin(9600);
  // declare pins: pLoadPin, clockEnablePin, dataPin, clockPin
  shift.begin(7, 4, 5, 6);
}

void displayValues() {
  for (int i = 0; i < shift.getDataWidth(); i++)
    Serial.print( shift.state(i) ); // get state of button i
  Serial.print(',');//comma
  Serial.println();
}

void loop() {
  if (shift.update()) // read in all values. returns true if any button has changed
    displayValues();
  delay(1000);
}
void displayValues() {
  for (int i = 0; i < shift.getDataWidth(); i++)
    Serial.print( shift.state(i) ); // get state of button i
  Serial.print(',');//comma
  Serial.println();
}

Note the indentation of

   Serial.print( shift.state(i) ); // get state of button i

Without { and } round the code block for the for loop, as previously advised, the for loop will run, print all of the states and only then will the comma be printed, as you have discovered

Thanks all,

The comma's are appearing as they should now!

Putting in the { and } sorted it like you all said. Just to clarify, it was previously going around the FOR loop until completed at the indentation and then finishing the loop and adding the comma last? With the help of the Auto Format feature and curly brackets, the indentation is equal so we can see the FOR loop is going through the Serial.print(','); as below?

void displayValues() {
  for (int i = 0; i < shift.getDataWidth(); i++) {
    Serial.print( shift.state(i) ); // get state of button i
    Serial.print(','); // comma
  }
  Serial.println();
}

Just to clarify, it was previously going around the FOR loop until completed at the indentation and then finishing the loop and adding the comma last?

Not quite. Without { and } to delimit the code block the statement after the for loop initialiser statement will be executed regardless of its indentation. C/C++ takes no notice of indentation or even blank lines

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.