Schematic to breadboard layout

Hello, do you know much about programming shift registering led’s? I’m having a hard time. Thanks

Sure, its fairly easy. However there are already good guides available (google is your friend!) such as this one which looks to do exactly what you are trying to do with a much better explaination than I could provide and example code:

Peter

I’ve actually been following that guide, I’m just wanting to know how to light just one LED. I can’t quite understand how to do that. I’m wanting to know so I can then have a go at assigning it to a button

Thanks

Set only the bit in the register that corresponds to the LED you want to light (i.e. set a 1 in any single bit in the register) and then send the byte to the shift register. The LED corresponding to the single bit set (or reset if 7 leds go on and one goes off :slight_smile: ) is the one you can control. Setting that particular bit to 1 or 0 turns that one led on or off.

Peter

for (int i = 0; i < 8; i++)
{ This is the part of the code that is controlling the LEDs ain’t it?

Yes or more correctly the value of variable leds is what controls the leds. If you set leds = 1 and then called updateShiftRegister(); (without the for loop) then whatever bit(s) are set in variable leds will cause the led in that position to light.

Peter

/*
Adafruit Arduino - Lesson 4. 8 LEDs and a Shift Register
*/

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;

byte leds = 1;

void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}

void loop()
{
leds = 1;
updateShiftRegister();
delay(500);
for (int i = 0; i < 8; i++)
{
bitSet(leds, 1);
updateShiftRegister();
delay(500);
}
}

void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}

as soon as I attempt to remove the loop “for (int i = 0; i < 8; i++)
{” is says that "updateShiftRegister(); was not declared in this scope. It’s winding me up, it’s probably something I’m missing and simple but I just don’t know

You would need to post the code with the for (int i = 0; i < 8; i++) removed for us to do much. You do also need to remove the { and } if you didn’t. For instance I think this should blink 1 led if it replaced the loop in the above routine.

void loop()
{
leds = 0;
updateShiftRegister();
delay(500);
bitSet(leds, 1);
updateShiftRegister();
delay(500);

}

this initially sets leds to 0, writes it to the shift register waits for a while then sets one bit in leds to 1 to change the state of the led and writes that to the shift register before waiting some more then repeating. Replacing the 1 in bitset with 2, 4, 8, 16, 32, 64, or 128 will change 1 led further along with each number increase.

Peter

Thank you very much, I will have to test that code. First person to explain that to me. Couldn’t get it across to people on the arduino forum. I can assign this to a push button now can’t I?. Then it starts to get hard I then need to make it light red and green depending on which mode it is in but also I think I’ll have to do that through a midi note. Not sure haven’t found much info, found a little on circularlabs forum but no one will reply to me

Once you can get the shift register to light the leds you should do ok. One bit in the led word will light the red led another bit (possibly in a different led variable if it is on a different shift register) will light the green led. Once you can light the red or green led with code you just move that code in to the place where the current state of the push button is determined. There you arrange (via the code you have working for the leds) to turn off the red led and turn on the green led (for instance) depending on the state of the push button and you are away. I think your initial error was likely that you didn’t remove the { and } after the for loop. It they are there with code in them they form a new lexical scope and updateShiftRegister indeed won’t be defined in it thus the error. You can use the #define statement in C to define logical names to the leds (such as Button1RedLed) to set the number that actually needs to go in to the led variable to turn on that led. This makes your code much more readable.

Peter

Thank you I will try the code, I tried to use a couple of libraries to help me make it easier. Like shifty. But it just lit up every LED up, with shifty it allows you to write digital inputs like you would for like a LED or button.

So this is the code I’m running for the mode button. I’m probably best practicing on the mode button since it puts it in play mode and record mode. Once any other button is pressed, I will looose track of the pedals state because it will either stay lit red or green till that pedal is pressed again because I need to figure out how to write it like as if it uses triggers with midi notes. Not hundred percent what I’m on about or how I’ll go about it if I manage it.

Would thi be the correct way of writing it to a push button? If the common cathode LED, red was connected to 1 and green connected to 2?

if (digitalRead(modebtn) == HIGH && toggle8 == 1){
toggle8 = 0;
if(mode == 0){
mode = 1;
midi.write(0x90);
midi.write(0x17);
midi.write((byte)0x00);
leds = 0;
updateShiftRegister();
delay(300);
bitSet(leds, 1);
updateShiftRegister();
delay(300);
}
else if(mode == 1){
mode = 0;
midi.write(0x90);
midi.write(0x30);
midi.write((byte)0x00);
void loop()
{
leds = 0;
updateShiftRegister();
delay(300);
bitSet(leds, 2);
updateShiftRegister();
delay(300);
}

Do I have to wire the blue leg? Because I don’t need it? Thanks

It’s still not working throwing up the same error message

Assuming bit 1 is the red led and bit 2 is the green led and a 1 turns on the led, then this should do what you want.

void setup()
{
leds = 0; /* set the leds initially all off */
pinMode(latchPin, OUTPUT);

if (digitalRead(modebtn) == HIGH && toggle8 == 1){
toggle8 = 0;
if(mode == 0){
mode = 1;
midi.write(0x90);
midi.write(0x17);
midi.write((byte)0x00);
bitClear(leds,2); /* turn off green led /
bitSet(leds, 1); /
turn on red led /
updateShiftRegister();
}
else if(mode == 1){
mode = 0;
midi.write(0x90);
midi.write(0x30);
midi.write((byte)0x00);
bitClear(leds,1) /
turn off the red led /
bitSet(leds, 2); /
turn on the green led */
updateShiftRegister();
}

The variable leds will keep the current state of the leds until the program changes it and calls updateShiftRegister() to change the value so you don’t need the loops or the delays. Pressing the pushbottons will change the state of the leds.

Do I have to wire the blue leg? Because I don’t need it? Thanks

No if you don’t use it you don’t need to connect it.

Peter

You have too many "}"s. At the end of void loop() where there is

}
}

delete the last one and it should work. The { and } need to be balanced and with the extra one it is not and it ends the program before defining updateShiftRegister which is why the error.

Peter

Thank you that helped, also that has sorted the error. But when I flashed it to the arduino it lit every led one by one. Then that was it, nothing is lit. I don’t get it, this is the problem I have been having a lot. I’m pretty sure everything is connected correctly.

/*
Adafruit Arduino - Lesson 4. 8 LEDs and a Shift Register
*/

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;

byte leds = 0;

void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}

void loop()
{
leds = 0;
updateShiftRegister();
delay(500);
bitSet(leds, 1);
updateShiftRegister();
delay(500);

}

void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}

Since the code looks correct I expect you have one or more of the pins from the arduino to the shift register wired wrong. Since the leds all light in sequence they are likely wired correctly as is the power.So verify

arduino pin 4 goes to shift register pin 14
arduino pin 5 goes to shift register pin 12
arduino pin 6 goes to shift register pin 11

Peter

I unlocked the part, and raised to front - right-click on part - and then lowered to back. Seams to be a glitch in FZ where you have to go the opposite way before going to where you want.

EDIT
Opps

I think you got the wrong thread, from the context I think this is the fix for the arduino that won’t move to the front which is another thread :slight_smile:

Peter

Right I have just rewired everything, think it’s working correctly now. I have changed the value 1 to 7 which lights up “Q0” pin. it flashes on and off.

Good, progress is being made :slight_smile: . From the 7 (which translates at the shift register to 0111) the polarity of off is 1 not 0. Thus you want to reverse the bitClear (which in fact turns the led on not off) and the bitSet (which turns it off not on) in the code a couple of posts back and see if you can get more than one led controlled so its on when you think it should be and off when you think it should be.

Peter