Hello there. Im working on a project where im using 74hc165 shif registers to command outputs (LEDs) with the use of 74hc595 shift registers.
I managed to cascade a pair of both shift registers and they work fine, but in my application Ill have to shift some bits through shift registers.
In this case I have button 1 through 12 and I have LEDS 1 through 12.
The way im doing it right now is by sending two bytes of data, one byte for the first 8 buttons/leds and the second byte for the next 4 buttons/leds.
Since the ShiftIn and ShiftOut commands allow me to shift just one byte of data per shif register,
This is what I want to do : given an instruction (for example if this or that button is pushed) shift my 16 bits n number of positions to the right. The bit shifting part is not a problem but the fact that the last bit of the first shift register would not shift right TO THE NEXT SHIFT REGISTER. And then when i start pressing the buttons of the second shift register they would be shifted n times to the right as if they were totally indepent.
I have no idea how to fix this, Ill be working with as many as 8 shift registers and I need to be able to, when having for example this button pushed ill start lighting my leds from the 4th position and on or if i push that other button ill start commanding my ouputs from the 10th position of my leds and on.
Im communicating 2 Arduinos by radio modules, so this is the code of the transmitter.
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
// Define Connections to 74HC165
// PL pin 1
int load = 7;
// CE pin 15
int clockEnablePin = 4;
// Q7 pin 9
int dataIn = 5;
// CP pin 2
int clockIn = 6;
//buttonpin
const int buttonpin = 44;
int buttonstate=0;
//radio settings
RF24 radio(8, 9); // CE, CSN
const byte address[][6] = {"pipe1"};
void setup() {
// Setup Serial Monitor
Serial.begin(9600);
// 74HC165 pins
pinMode(load, OUTPUT);
pinMode(clockEnablePin, OUTPUT);
pinMode(clockIn, OUTPUT);
pinMode(dataIn, INPUT);
pinMode(buttonpin, INPUT);
// partie radio
radio.begin(); // Initialize the nRF24L01 Radio
radio.setChannel(108); // Above most WiFi frequencies
radio.setDataRate(RF24_250KBPS); // Fast enough.. Better range
radio.setPALevel(RF24_PA_LOW);// radio.setPALevel(RF24_PA_MAX);
radio.openWritingPipe(address);
radio.stopListening();
}
void loop() {
// Read Switches
// Write pulse to load pin
digitalWrite(load, LOW);
delayMicroseconds(5);
digitalWrite(load, HIGH);
delayMicroseconds(5);
// Get data from 74HC165
digitalWrite(clockIn, HIGH);
digitalWrite(clockEnablePin, LOW);
byte incoming = shiftIn(dataIn, clockIn, LSBFIRST);
byte incoming2= shiftIn(dataIn, clockIn, LSBFIRST);
digitalWrite(clockEnablePin, HIGH);
// Print to serial monitor
Serial.print("Pin States(1-8):\r\n");
Serial.println(incoming, BIN);
Serial.print("Pin States(9-12):\r\n");
Serial.println(incoming2, BIN);
//bit shift
int test= int (incoming);
test= test/2;
incoming=byte (test);
int test2= int (incoming2);
test2= test2/2;
incoming2=byte (test2);
//send via radio
radio.write(&incoming, sizeof(incoming));
radio.write(&incoming2, sizeof(incoming2));
delay(150);
}
And this is the code of the receiving Arduino.
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
// Define Connections to 74HC595
// ST_CP pin 12
const int latchPin = 10;
// SH_CP pin 11
const int clockPin = 11;
// DS pin 14
const int dataPin = 12;
// radio settings
RF24 radio(7, 8); // CE, CSN
const byte address[][6] = {"pipe1"};
void setup() {
Serial.begin(9600);
// 74HC595 pins
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// partie radio
radio.begin(); // Initialize the nRF24L01 Radio
radio.setChannel(108); // Above most WiFi frequencies
radio.setDataRate(RF24_250KBPS); // Fast enough.. Better range
radio.setPALevel(RF24_PA_LOW);// radio.setPALevel(RF24_PA_MAX);
radio.openReadingPipe(1,address);
radio.startListening();
}
void loop() {
//receive byte
delay(5);
if ( radio.available()) {
byte incoming=0;
byte incoming2=0;
while (radio.available()) {
radio.read(&incoming, sizeof(incoming));
radio.read(&incoming2, sizeof(incoming2));
Serial.print("Pin States(1-8):\r\n");
Serial.println(incoming, BIN);
Serial.print("Pin States(9-12):\r\n");
Serial.println(incoming2, BIN);
}
// Write to LEDs
// ST_CP LOW to keep LEDs from changing while reading serial data
digitalWrite(latchPin, LOW);
// Shift out the bits
shiftOut(dataPin, clockPin, LSBFIRST, incoming2);
shiftOut(dataPin, clockPin, LSBFIRST, incoming);
// ST_CP HIGH change LEDs
digitalWrite(latchPin, HIGH);
delay(150);
}
}
