//** // Demonstration code for 74HC595 16 bit output
// int RCLKPin = 19;//21; // pin 12 on the 74hc595 latch - nSS
// int SRCLKPin = 23; // pin 11 on the 74hc595 shift register clock - SCK
// int SERPin = 18;//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;
// #define ENCODER_CLK 12
// #define ENCODER_DT 13
// #define ENCODER_BTN 14
// #define ENCODER2_CLK 34
// #define ENCODER2_DT 35
// #define ENCODER2_BTN 25
// int lastClk = HIGH;
// 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(ENCODER_CLK, INPUT);
// pinMode(ENCODER_DT, INPUT);
// pinMode(ENCODER_BTN, INPUT_PULLUP);
// pinMode(ENCODER2_CLK, INPUT);
// pinMode(ENCODER2_DT, INPUT);
// pinMode(ENCODER2_BTN, INPUT_PULLUP);
// pinMode(btn, INPUT_PULLUP);
// attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
// attachInterrupt(digitalPinToInterrupt(ENCODER2_CLK), readEncoder2, FALLING);
// Serial.println("74HC595 Demo 2xchips for 16 bit register.");
// d=1;
// }
// void readEncoder() {
// int dtValue = digitalRead(ENCODER_DT);
// if (dtValue == HIGH) {
// Serial.println("Rotated clockwise ⏩");
// }
// if (dtValue == LOW) {
// Serial.println("Rotated counterclockwise ⏪");
// }
// }
// void readEncoder2() {
// int dtValue = digitalRead(ENCODER2_DT);
// if (dtValue == HIGH) {
// Serial.println("2 Rotated clockwise ⏩");
// }
// if (dtValue == LOW) {
// Serial.println("2 Rotated counterclockwise ⏪");
// }
// }
// void loop()
// {
// if (digitalRead(ENCODER_BTN) == LOW) {
// digitalWrite(LED_BUILTIN, HIGH);
// // Serial.println(digitalRead(ENCODER_BTN));
// Serial.println("encoder press");
// } else {
// digitalWrite(LED_BUILTIN, LOW);
// }
// if (digitalRead(ENCODER2_BTN) == LOW) {
// digitalWrite(LED_BUILTIN, HIGH);
// // Serial.println(digitalRead(ENCODER_BTN));
// Serial.println("encoder2 press");
// } else {
// digitalWrite(LED_BUILTIN, LOW);
// }
// // 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));
// }
// Demonstration code for 74HC595 16 bit output
int RCLKPin = 19;//21; // pin 12 on the 74hc595 latch - nSS
int SRCLKPin = 23; // pin 11 on the 74hc595 shift register clock - SCK
int SERPin = 18;//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;
const int dataPin = 32; /* Q7 */
const int clockPin = 26; /* CP */
const int latchPin = 33; /* PL */
const int numBits = 24; /* 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);
}