Hello All,
Based on the code below lighting each individual LED works fine...I am trying to light more than one at the same time and I am having trouble doing so. What do I need to change in the code to below to accomplish this? Thanks in advance!
int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
byte leds = 0;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
updateShiftRegister();
Serial.begin(9600);
while (! Serial); // Wait untilSerial is ready - Leonardo
Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
}
void loop() {
if (Serial.available()) {
char ch = Serial.read();
if (ch >= '0' && ch <= '7') {
int led = ch - '0';
bitSet(leds, led);
updateShiftRegister();
Serial.print("Turned on LED ");
Serial.println(led);
}
if (ch == 'x') {
leds = 0;
updateShiftRegister();
Serial.println("Cleared");
}
if (ch == 'a') {
/* ENTER CODE HERE */
updateShiftRegister();
Serial.println("Block One");
}
}
}
void updateShiftRegister() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
See this bloc:
if (ch >= '0' && ch <= '7') {
int led = ch - '0';
bitSet(leds, led);
Add more leds you like to light up:
bitSet(leds, 1);
bitSet(leds, 5);
updateShiftRegister();
Serial.print("Turned on LED ");
Serial.println(led);
}
That's great! Now if LEDs 7 and 5 are on....I want to turn off 6 and 4 in the same if statement...however the bitSet does not allow 3 arguments just 2...how do I accomplish this? Thanks.
EDIT: Nevermind...found it...bitClear(leds, whichever LED) sorry...new to this Shift Register programming.
Thanks I will take a look at that tutorial. I've hooked up another shift register and using my same code i need to control the LEDs connected to the second shift register independent of the first.
This library, I think, is the most popular to drive shift registers:
http://www.elcojacobs.com/shiftpwm/
Had to search the internet to find the library to download...anyways to simplify my question as follow:
Register 1: Light up LED 1
Register 2: Light up LED 4
and so on. I can light up individual LEDs on the first register not the second. As of right now register 2 mimicks register 1. Thanks for the help!
Post how you connect IC, and post your updated code
Here is my code below:
int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
byte leds = 0;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
updateShiftRegister();
Serial.begin(9600);
while (! Serial); // Wait untilSerial is ready
Serial.println("ENTER LED NUMBER 0 TO 7 OR 'x' TO CLEAR");
}
void loop() {
if (Serial.available()) {
char ch = Serial.read();
if (ch >= '0' && ch <= '7') {
int led = ch - '0';
bitSet(leds, led);
updateShiftRegister();
Serial.print("Turned on LED ");
Serial.println(led);
}
if (ch == 'x') {
leds = 0;
updateShiftRegister();
Serial.println("ALL OFF");
}
// ALL CLEAR
if (ch == 'c') {
bitClear(leds, 7);
bitClear(leds, 6);
bitSet(leds, 5);
bitClear(leds, 4);
bitClear(leds, 3);
bitSet(leds, 2);
bitClear(leds, 1);
bitClear(leds, 0);
updateShiftRegister();
Serial.println("ALL CLEAR");
}
// ALL APPROACH
if (ch == 'a') {
bitClear(leds, 7);
bitSet(leds, 6);
bitClear(leds, 5);
bitClear(leds, 4);
bitSet(leds, 3);
bitClear(leds, 2);
bitClear(leds, 1);
bitSet(leds, 0);
updateShiftRegister();
Serial.println("ALL APPROACH");
}
// ALL STOP
if (ch == 's') {
bitSet(leds, 7);
bitClear(leds, 6);
bitClear(leds, 5);
bitSet(leds, 4);
bitClear(leds, 3);
bitClear(leds, 2);
bitSet(leds, 1);
bitClear(leds, 0);
updateShiftRegister();
Serial.println("ALL STOP");
}
}
}
void updateShiftRegister() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
I would assume that all I would have to do is make double code for Register #2? Or am I way off base?
Here is a connection diagram, pay attention that 2 registers are in series:
Yes...my Arduino/Shift register setup is exactly how it looks in the diagram...in fact I used the Shift Out tutorial to set everything up and used the test code to make sure things worked...and it does. Just trying to figure out how to light the LEDs on shift 2 individually like I can with shift 1 based on the code provided. Thanks Magician.
YOu can use bitSet and bitClear if you want, or you can learn how to use the C bit-fiddling operators << >> >>> ~ & ^ | .