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);
}
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();
}
#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
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