// int latch1Pin = 21;
// int latch2Pin = 18;
// int clockPin = 23;
// int dataPin = 22;
// byte leds1 = 0;
// byte leds2 = 0;
// void setup() {
// Serial.begin(115200);
// pinMode(latch1Pin, OUTPUT);
// pinMode(latch2Pin, OUTPUT);
// pinMode(dataPin, OUTPUT);
// pinMode(clockPin, OUTPUT);
// leds1 = 0;
// leds2 = 0;
// updateShiftRegister();
// }
// void loop() {
// delay(10); // this speeds up the simulation
// leds1 = 0;
// leds2 = 0;
// updateShiftRegister();
// delay(1000);
// for (int i = 0; i < 16; i++)
// {
// if(i < 8)
// {
// leds2 = 0;
// updateShiftRegister();
// bitSet(leds1, i);
// updateShiftRegister();
// Serial.print("led1:");
// for (int j = 7; j >= 0; j--)
// {
// bool b = bitRead(leds1, j);
// Serial.print(b);
// }
// } else
// {
// leds1 = 0;
// updateShiftRegister();
// bitSet(leds2, i-8);
// updateShiftRegister();
// Serial.print("led2:");
// for (int j = 7; j >= 0; j--)
// {
// bool b = bitRead(leds2, j);
// Serial.print(b);
// }
// }
// // updateShiftRegister();
// delay(1000);
// Serial.println(" ");
// }
// // bitSet(leds, 1);
// // updateShiftRegister();
// // for (int i = 0; i < 8; i++)
// // {
// // leds = 0;
// // setStep(i);
// // delay(500);
// // }
// }
// // void setStep(byte step)
// // {
// // bitSet(leds, step);
// // updateShiftRegister();
// // // digitalWrite(latchPin, LOW);
// // shiftOut(dataPin, clockPin, LSBFIRST, leds);
// // digitalWrite(latchPin, HIGH);
// // }
// void updateShiftRegister()
// {
// digitalWrite(latch1Pin, LOW);
// shiftOut(dataPin, clockPin, LSBFIRST, leds1);
// digitalWrite(latch1Pin, HIGH);
// digitalWrite(latch2Pin, LOW);
// shiftOut(dataPin, clockPin, LSBFIRST, leds2);
// digitalWrite(latch2Pin, HIGH);
// }
// Demonstration code for 74HC595 16 bit output
// int RCLKPin = 21; // pin 12 on the 74hc595 latch - nSS
// int SRCLKPin = 23; // pin 11 on the 74hc595 shift register clock - SCK
// int SERPin = 22; // pin 14 on the 74hc595 data - MOSI
// unsigned int d; // Data to be sent to the shift reg.
// int dir =0; // Direction of walking 1.
// char buf[12]; // General purpose buffer.
// int btn = 33;
// void setup() {
// Serial.begin(9600); // start serial port (debug).
// pinMode(RCLKPin, OUTPUT); // Set 595 control PIN sto output.
// pinMode(SRCLKPin, OUTPUT);
// pinMode(SERPin, OUTPUT);
// pinMode(btn, INPUT_PULLUP);
// Serial.println("74HC595 Demo 2xchips for 16 bit register.");
// d=1;
// }
// void loop() {
// delay(10);
// digitalWrite(RCLKPin, LOW);
// shiftOut(SERPin, SRCLKPin, LSBFIRST, (0xff00 & d)>>8);
// shiftOut(SERPin, SRCLKPin, LSBFIRST, 0x00ff & d);
// digitalWrite(RCLKPin, HIGH);
// // Serial.println(itoa(d,buf,16));
// if (!dir) d<<=1; else d>>=1; // Shift
// if (d&0x8000) dir=1; // Set direction.
// if (d&0x0001) dir=0;
// Serial.print("input: ");
// Serial.println(digitalRead(btn));
// }
// int RCLKPin = 21; // pin 12 on the 74hc595 latch - nSS
// int SRCLKPin = 23; // pin 11 on the 74hc595 shift register clock - SCK
// int SERPin = 22; // pin 14 on the 74hc595 data - MOSI
// unsigned int d; // Data to be sent to the shift reg.
// int dir =0; // Direction of walking 1.
// char buf[12]; // General purpose buffer.
// int btn = 33;
// void setup() {
// Serial.begin(9600); // start serial port (debug).
// pinMode(RCLKPin, OUTPUT); // Set 595 control PIN sto output.
// pinMode(SRCLKPin, OUTPUT);
// pinMode(SERPin, OUTPUT);
// pinMode(btn, INPUT_PULLUP);
// Serial.println("74HC595 Demo 2xchips for 16 bit register.");
// d=1;
// }
// void loop() {
// delay(10);
// digitalWrite(RCLKPin, LOW);
// shiftOut(SERPin, SRCLKPin, LSBFIRST, (0xff00 & d)>>8);
// shiftOut(SERPin, SRCLKPin, LSBFIRST, 0x00ff & d);
// digitalWrite(RCLKPin, HIGH);
// // Serial.println(itoa(d,buf,16));
// if (!dir) d<<=1; else d>>=1; // Shift
// if (d&0x8000) dir=1; // Set direction.
// if (d&0x0001) dir=0;
// Serial.print("input: ");
// Serial.println(digitalRead(btn));
// }
// // Define the pins
// const int dataPin = 32; // Q7' from 74HC165
// const int clockPin = 26; // CP from 74HC165 (34)
// const int loadPin = 33; // PL from 74HC165
// void setup() {
// pinMode(dataPin, INPUT_PULLUP);
// pinMode(clockPin, OUTPUT);
// pinMode(loadPin, OUTPUT);
// // Start the serial communication
// Serial.begin(115200);
// }
// void loop() {
// // Read button states
// digitalWrite(loadPin, LOW); // Load the button states into the shift register
// delayMicroseconds(5); // Wait for a short period
// digitalWrite(loadPin, HIGH); // Disable parallel load
// byte buttons = shiftIn(dataPin, clockPin, MSBFIRST); // Read the button states
// // Print the button states
// for (int i = 0; i < 8; i++) {
// Serial.print(bitRead(buttons, 7 - i)); // Print each bit
// Serial.print(" ");
// }
// Serial.println();
// delay(100); // Wait for a second before reading again
// }
// // Define the pins
// const int dataPin = 32; // Q7' from 74HC165
// const int clockPin = 26; // CP from 74HC165 (34)
// const int loadPin = 33; // PL from 74HC165
// void setup() {
// pinMode(dataPin, INPUT_PULLUP);
// pinMode(clockPin, OUTPUT);
// pinMode(loadPin, OUTPUT);
// // Start the serial communication
// Serial.begin(115200);
// }
// void loop() {
// // Read button states
// digitalWrite(loadPin, LOW); // Load the button states into the shift register
// delayMicroseconds(5); // Wait for a short period
// digitalWrite(loadPin, HIGH); // Disable parallel load
// byte buttons = shiftIn(dataPin, clockPin, MSBFIRST); // Read the button states
// // Print the button states
// for (int i = 0; i < 16; i++) {
// Serial.print(bitRead(buttons, 15 - i)); // Print each bit
// Serial.print(" ");
// }
// Serial.println();
// delay(100); // Wait for a second before reading again
// }
// // Define the pins
// const int dataPin = 32; // Q7' from 74HC165
// const int clockPin = 26; // CP from 74HC165 (34)
// const int loadPin = 33; // PL from 74HC165
// void setup() {
// pinMode(dataPin, INPUT_PULLUP);
// pinMode(clockPin, OUTPUT);
// pinMode(loadPin, OUTPUT);
// // Start the serial communication
// Serial.begin(115200);
// }
// void loop() {
// // Read button states
// digitalWrite(loadPin, LOW); // Load the button states into the shift register
// delayMicroseconds(5); // Wait for a short period
// digitalWrite(loadPin, HIGH); // Disable parallel load
// byte buttons = shiftIn(dataPin, clockPin, MSBFIRST); // Read the button states
// // Print the button states
// for (int i = 0; i < 16; i++) {
// Serial.print(bitRead(buttons, 15 - i)); // Print each bit
// Serial.print(" ");
// }
// Serial.println();
// delay(100); // Wait for a second before reading again
// }
const int dataPin = 32; /* Q7 */
const int clockPin = 26; /* CP */
const int latchPin = 33; /* PL */
const int numBits = 16; /* Set to 8 * number of shift registers */
void setup() {
Serial.begin(115200);
pinMode(dataPin, INPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
}
void loop() {
// Step 1: Sample
digitalWrite(latchPin, LOW);
digitalWrite(latchPin, HIGH);
// Step 2: Shift
Serial.print("Bits: ");
for (int i = 0; i < numBits; i++) {
int bit = digitalRead(dataPin);
if (bit == HIGH) {
Serial.print("1");
} else {
Serial.print("0");
}
digitalWrite(clockPin, HIGH); // Shift out the next bit
digitalWrite(clockPin, LOW);
}
Serial.println();
delay(100);
}