Using a shift register to control multiple LEDs. Based on the state of my system, the LEDs can be all on, none one, specific LEDs on. Therefore, I need control over individual LEDs via the Shift Register. However, not finding a good example, or conflicting examples of how to do this. Here is the core of the code for this interaction. Am I missing anything or what am I doing wrong?
/* Shift Register */
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPinForShiftRegister = 11;
// Variable to hold the pattern of which LEDs are currently turned on or off
byte shiftLEDS = 0; //used store something like 00000000 as byte holds 8.
byte led1 = '10000000';
byte led2 = '01000000';
byte led34 = '00110000';
void setup() {
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPinForRegister, OUTPUT);
updateShiftRegister();
}
void loop() {
shiftLEDS = 0;
UpdateShiftRegister();
//if some condition met, light up LED #1
shiftLEDS = led1;
UpdateShiftRegister();
//if some condition met, light up LED #3
shiftLEDS = led2;
UpdateShiftRegister();
//if some condition met, light up LED #5 and #6
shiftLEDS = led34;
UpdateShiftRegister();
}
//Update Shift Register for LEDs
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, shiftLEDS);
digitalWrite(latchPin, HIGH);
}
I usually use the SPIShiftRegisterOut
class from the Arduino Helpers library I maintain:
https://tttapa.github.io/Arduino-Helpers/Doxygen/d9/d5f/1_8SPI-Blink_8ino-example.html
You simply define a shift register in your code, and then you can use its pin numbers as arguments for the familiar digitalWrite
function.
The problem in your code that '10000000'
is a character, not an integer. You want 0b10000000
. The compiler should warn you about these kinds of bugs, please go to the preferences in the Arduino IDE and enable all compiler warnings.
Pieter
Thanks, I'll check out your library. I was confused with the '0b' in defining the variable when I first saw it. Feel I read so much it all blurred together.
chrishuff:
I was confused with the '0b' in defining the variable when I first saw it. Feel I read so much it all blurred together.
Normal base-10 integer literals don't have any prefixes, binary (base-2) integer literals have a prefix 0b
, octal (base-8) integer literals have a prefix 0
, and hexadecimal integer literals have a prefix 0x
.
The following variables are all equal to 1610.
int decimal = 16;
int binary = 0b00010000;
int octal = 020;
int hexadecimal = 0x10;
Single quotes are usually only used for single characters, e.g. char c = 'a'
.
Thank you. I program in a lot of languages and sometimes mix up the syntax...but I've never worked directly with bits and bytes and such so that part is new to me.
Got this error when I tried uploading the library. I've uploaded two other libraries without any problems.
LIBRARIES THAT COULD NOT BE IMPORTED:
[mock/Core-Libraries] parse library.properties: library.properties not found
[mock/Libraries/Encoder] parse library.properties: library.properties not found
[mock/Libraries/Adafruit_GFX] parse library.properties: library.properties not found
[mock/Libraries/Adafruit_SSD1306] parse library.properties: library.properties not found
[gtest-wrappers] parse library.properties: library.properties not found
[mock/Core] parse library.properties: library.properties not found
LIBRARIES SUCCESSFULLY IMPORTED:
Arduino Helpers
chrishuff:
Got this error when I tried uploading the library. I've uploaded two other libraries without any problems.
LIBRARIES THAT COULD NOT BE IMPORTED:
[mock/Core-Libraries] parse library.properties: library.properties not found
[mock/Libraries/Encoder] parse library.properties: library.properties not found
[mock/Libraries/Adafruit_GFX] parse library.properties: library.properties not found
[mock/Libraries/Adafruit_SSD1306] parse library.properties: library.properties not found
[gtest-wrappers] parse library.properties: library.properties not found
[mock/Core] parse library.properties: library.properties not found
LIBRARIES SUCCESSFULLY IMPORTED:
Arduino Helpers
What version of the Arduino IDE are you using? The folders it's complaining about are part of the unit tests for the library, not the library itself. As far as I can see, it didn't produce any errors, and I think it should work if you try to compile. Could you post the full message if there was more output?
Edit: I just realized you're using the web editor. I've tried uploading the Arduino-Helpers-master.zip
file, and I got the same warnings. You can safely ignore them, the main library was imported successfully, and the examples seem to compile just fine.
Thanks. When I do my final testing and work, I'll be using the IDE for WIndows on my desktop. Due to pure convenience and availability, I'm doing most of my development on my chromebook. I'll make sure in the future if I have any unusual errors to copy everything over to the windows IDE and try it there. Thanks again.