using Shift register with leds and buttons

Hello I'm having some trouble, I've been surfing the internet reading tutorials and finding information out. I've built a circuit with buttons and now added a 74HC595 shift register and wired it up all correctly. I have added one of these because I have nearly run out of pins I will eventually need to add another because I want to control 6 common cathode led's (I only need the red and green led). I'm wanting to turn a led on when the push button is pressed and the off when pressed again. I can't really seem to find a tutorial online to control just 1 led, a lot of them are matrix codes with multiple led's. I'm not asking for anyone to do this for me, just to help me and guide me the correct way so I can pick things up.
The program I am using which fully works, it has a led already connected but I want to move this led to a pin on the shift register and control it using any button. because I plan to learn how to control one led then program the other buttons and led's to what I need. Sorry I'm quite new to this but I have been trying to learn.

#include <SoftwareSerial.h>
SoftwareSerial midi(0,1); ///RX TX

int clearbtn = 9;
int recordbtn = 5;
int stopbtn = 6;
int track1btn = 10;
int track2btn = 11;
int track3btn = 12;
int track4btn = 13;
int modebtn = 8; 
int undobtn = 7 
int led = 2

int toggle1 = 0;
int toggle2 = 0;
int toggle3 = 0;
int toggle4 = 0;
int toggle5 = 0;
int toggle6 = 0;
int toggle7 = 0;
int toggle8 = 0;
int toggle9 = 0;


int mode = 0;



void setup(){
  pinMode (clearbtn, INPUT_PULLUP);
  pinMode (undobtn, INPUT_PULLUP);
  pinMode (recordbtn, INPUT_PULLUP);
  pinMode (stopbtn, INPUT_PULLUP);
  pinMode (track1btn, INPUT_PULLUP);
  pinMode (track2btn, INPUT_PULLUP);
  pinMode (track3btn, INPUT_PULLUP);
  pinMode (track4btn, INPUT_PULLUP);
  pinMode (modebtn, INPUT_PULLUP);
  pinMode (undobtn, INPUT_PULLUP);
  pinMode (led, OUTPUT);
  digitalWrite(led, HIGH);
  delay(250);
  digitalWrite(led, LOW);
  delay(250);
  digitalWrite(led, HIGH);
  delay(250);
  digitalWrite(led, LOW);
  delay(250);
  digitalWrite(led, HIGH);
  delay(250);
  digitalWrite(led, LOW);
  delay(250);
  
  midi.begin(31250);
}



void loop(){

  if (digitalRead(undobtn) == HIGH && toggle1 == 0){
    toggle1 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x41);
      midi.write(0x01);
      // delay(300);
    }

    if(mode == 1){
      midi.write(0x90);
      midi.write(0x36);
      midi.write(0x01);
      // delay(300);
    }
  } 

  if (digitalRead(undobtn) == LOW && toggle1 == 1){
    toggle1=0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x41);
      midi.write((byte)0x00);  
      delay(300);
    }

    if(mode == 1){
      midi.write(0x90);
      midi.write(0x36);
      midi.write((byte)0x00);  
      delay(300);
    }

  }




  if(digitalRead(clearbtn) == HIGH && toggle2 == 0){
    toggle2 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x01);
      midi.write(0x45);
      //delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x37);
      midi.write(0x45);
      //delay(300);  
    }
  }


  if(digitalRead(clearbtn) == LOW && toggle2 == 1){ 
    toggle2 = 0;

    if(mode == 0){
      midi.write(0x90);
      midi.write(0x01);
      midi.write((byte)0x00);
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x37);
      midi.write((byte)0x00);
      delay(300);
    }

  }



  if(digitalRead(recordbtn) == HIGH && toggle3 == 0){
    toggle3 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x10);
      midi.write(0x45);
      //delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x35);
      midi.write(0x45);
      // delay(300);
    }
  }

  if(digitalRead(recordbtn) == LOW && toggle3 == 1){
   toggle3 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x10);
      midi.write((byte)0x00);
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x35);
      midi.write((byte)0x00);
      delay(300);

    }
  }




  if(digitalRead(stopbtn) == HIGH && toggle4 == 0){
    toggle4 = 1;
    ///Serial.println("mute/stop");  
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x07);
      midi.write(0x45); 
      //delay(300); 
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x34);
      midi.write(0x45); 
      //delay(300); 
    }
  }


  if(digitalRead(stopbtn) == LOW && toggle4 == 1){
    toggle4 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x07);
      midi.write((byte)0x00);
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x34);
      midi.write((byte)0x00);
      delay(300);

    }
  }




  if (digitalRead(track1btn) == HIGH && toggle5 == 0){
    toggle5 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x14);
      midi.write(0x01);
      // delay(300);
    } 
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x33);
      midi.write(0x01);
      //  delay(300);
    }
  } 

  if (digitalRead(track1btn) == LOW && toggle5 == 1){
    toggle5 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x14);
      midi.write((byte)0x00); 
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x33);
      midi.write((byte)0x00); 
      delay(300);
    }
  }

  if (digitalRead(track2btn) == HIGH && toggle6 == 0){
    toggle6 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x15);
      midi.write(0x01);
      // delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x32);
      midi.write(0x01);
      // delay(300);
    }

  } 

  if (digitalRead(track2btn) == LOW && toggle6 == 1){
    toggle6 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x15);
      midi.write((byte)0x00);  
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x32);
      midi.write((byte)0x00);  
      delay(300);
    }
  }


  if (digitalRead(track3btn) == HIGH && toggle7 == 0){
    toggle7 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x16);
      midi.write(0x01);
      //  delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x31);
      midi.write(0x01);
      // delay(300);
    }
  } 

  if (digitalRead(track3btn) == LOW && toggle7 == 1){
    toggle7 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x16);
      midi.write((byte)0x00); 
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x30);
      midi.write((byte)0x00); 
      delay(300);

    }
  }


  if (digitalRead(track4btn) == HIGH && toggle9 == 0){
    toggle9 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x40);
      midi.write(0x01);
      // delay(300);
    } 
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x66);
      midi.write(0x01);
      //  delay(300);
    }
  } 

  if (digitalRead(track4btn) == LOW && toggle9 == 1){
    toggle9 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x40);
      midi.write((byte)0x00); 
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x66);
      midi.write((byte)0x00); 
      delay(300);
    }
  }


  if (digitalRead(modebtn) == HIGH && toggle8 == 0){
    toggle8 = 1; 
    if(mode == 0){


      midi.write(0x90);
      midi.write(0x17);
      midi.write(0x01);
      // delay(300);
    }

    if(mode == 1){


      midi.write(0x90);
      midi.write(0x30);
      midi.write(0x01);
      //  delay(300);
    }


  } 

  if (digitalRead(modebtn) == LOW && toggle8 == 1){
    toggle8 = 0;
    if(mode == 0){
      mode = 1;
      midi.write(0x90);
      midi.write(0x17);
      midi.write((byte)0x00);  
      digitalWrite(led, HIGH);
      delay(300);
    }
    else if(mode == 1){
      mode = 0;
      midi.write(0x90);
      midi.write(0x30);
      midi.write((byte)0x00);  
      digitalWrite(led, LOW);
      delay(300);}

  }
}

at the moment I have my shift register connected to the following

int DS_pin = 4;
int STCP_pin = 3;
int SHCP_pin = 2;

Try to use the shift register in some short simple sketch so you learn how to use it. Then integrate it in this long one. I didn't manage to find attempt to use it in the code so it is hard to tell you what is wrong.

I've built a few circuits with led's but that's lighting them all up a lot of the tutorials showed. The code I posted is what I'm running. All I want to add now is 6 LED's. Just to turn on and off when a button is pressed.

The point Smajdalf is making is that you haven't shown us your best attempt at incorporating the shift register. Post that code, even if it does not work, describe what it does do (if anything) and we will help.

Hints: check the Learning-->Reference page in the menu at the top of the forum page and have a look at shiftOut() and bitWrite().

On this Arsuino page, you will find some code that does just that: Put HIGH or LOW only one of the HC595 pins

Jacques

I have built a circuit and connected just one led on one pin of the shift register. at the moment it is flashing because it was a code to test if all my parts are wired up correctly.
I did have a go at changing my current code to turn just one pin on, but I know each pin has a value I just want to know how to implement that in the code because I see lots of tutorial turning all pins on like a matrix. I need to understand how to write it just for one pin. like Here

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

sorry I'm a nuisance, I honestly have been trying to learn and pick things up by reading tutorials and watching youtube videos

I did have a go at changing my current code to turn just one pin on

Post that code.

You seen to have all the pieces of the jigsaw... I can't quite figure out what is stopping you from putting the final piece in place...

I am wondering if you know what 'bitSet()' and 'bitClear()' actualy does to the variable leds.

The Adafruit site that you mentionned says:
The loop function pauses for half a second and then begins to count from 0 to 7 using the 'for' loop and the variable 'i'. Each time, it uses the Arduino function 'bitSet' to set the bit that controls that LED in the variable 'leds'. It then also calls 'updateShiftRegister' so that the leds update to reflect what is in the variable 'leds'.

I agree with PaulRB, you are almost there.

Hint : If you want to fire just one pin, then you do not need the 'for' loop.

I don't know what they mean I have an idea. Been reading up on a lot of tutorials. Just needed someone to just explain a few things and point me in the right direction. I'm new to all this you see, I've been spending hours trying to read things and take it all in but sometimes you can read it and read it and still not understand it. Which has been my case a little. I'm using push buttons which send midi notes. So could I still use the midi notes and turn on a single LED by a push of the button.

Is this code correct to turn just 1 pin on so it lights only 1 LED? Appreciate your comments and help

Sorry can't post this as code because I'm on my phone.
" void loop()
{
leds = 0;
updateShiftRegister();
delay(500);
bitSet(leds, 1);
updateShiftRegister();
delay(500);"

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

Hey look! I'm on my phone but I managed to post code. Amazing!

You got it jovid,

Consider that the variable 'leds' contains a bank of 8 values that can be either on (1) or off (0). In the way that 'bitSet()' is sees 'leds', their positions are numbered, from left to right: 7, 6, 5, 4, 3, 2, 1 and 0. To know more about this, check here. It is complete, and it will help you in future projects because it contains other concepts that can be used later.

'leds = 0' makes all the 8 bits of 'leds' at zero. (All the LEDs OFF)

The leftmost bit is called MSB (Most Significant bit. It is worth 128). The rightmost bit is called LSB (Least Significant Bit. It is worth 1). The code at Adafruit that you mentionned uses:
shiftOut(dataPin, clockPin, LSBFIRST, leds). shiftOut sends all the 8 bits to the 74HC595.

LSBFIRST means that the Least Significant Bit is sent first to the 74HC595. Experiment with LSBFIRST and MSBFIRST to see your code light up the LED attached to pin Q1 or the one attached to pin Q6.

#include <SoftwareSerial.h>
SoftwareSerial midi(0,1); ///RX TX

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int modebtn = 7; 
byte leds = 0;

int toggle8 = 0;

int mode = 0;

void setup(){
leds = 0; /* set the leds initially all off */
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  pinMode (modebtn, INPUT_PULLUP);
  midi.begin(31250);
}

void loop(){
  if (digitalRead(modebtn) == HIGH && toggle8 == 0){
}
  toggle8 = 1; 
  if(mode == 0){


  midi.write(0x90);
  midi.write(0x17);
  midi.write(0x01);
  // delay(300);
  }

    if(mode == 1){


      midi.write(0x90);
      midi.write(0x30);
      midi.write(0x01);
      //  delay(300);
    }


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

  }
}

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

I'm trying to assign the shift register to my program to make the led turn green when pressed then red when pressed again. But I'm having some bother, it does switch colour but it messes up. Tried removing the "{" above "toggle8 = 1;" because I don't think it should be there. and also tried to remove the multiple "{" further down towards the bottom but arduino throws up an error of "updateShiftRegister was not declared in this scope". Is there something I'm missing or doing wrong?

You need the same amount of { as }. The brackets around toggle=1 are probably wrong. Try to autoformat the source to make it better readable.

#include <SoftwareSerial.h>
SoftwareSerial midi(0, 1); ///RX TX

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int modebtn = 7;
byte leds = 0;

int toggle8 = 0;

int mode = 0;

void setup() {
  leds = 0; /* set the leds initially all off */
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode (modebtn, INPUT_PULLUP);
  midi.begin(31250);
}

void loop() {
  if (digitalRead(modebtn) == HIGH && toggle8 == 0) {
    toggle8 = 1;
    if (mode == 0) {


      midi.write(0x90);
      midi.write(0x17);
      midi.write(0x01);
      // delay(300);
    }

    if (mode == 1) {


      midi.write(0x90);
      midi.write(0x30);
      midi.write(0x01);
      //  delay(300);
    }


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

      else if (mode == 1) {
        mode = 0;
        midi.write(0x90);
        midi.write(0x30);
        midi.write((byte)0x00);
        bitClear(leds, 7); /* turn off the red led */
        bitSet(leds, 6); /* turn on the green led */
        updateShiftRegister();
      }

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

tried removing the {'s and adding where I think they need to be but it is still showing the same error

Hi,

You need more discipline when you code. Computers are machines and use a syntax that has to be strictly adhered to.

As Smajdalf mentioned, you need to have the same amount of { as }. But moreover, they need to be placed correctly.

The usual way of placing brackets is the following: When starting a block (Something between brackets) Place the opening bracket at the end of the first line, indent the code inside the bracket, place the ending bracket alone one it’s line, at the same indentation than the start of the block.

Here is an example :

void myCode(int someParameter) {
  for (i = 1 ; i<8 ; i++) {
    analogWrite(13, someParameter)
  }
}

Looking at the closing brackets, you know instantly to which block it belongs.

Rearange your code, and you will find the missing closing brackets. (The Arduino IDE is very helping in this task: when you select a bracket, it highlights it's matching counterpart. Use this help.)

Moreover, know that updateShiftRegister() is a function that is used by your sketch. It is like a little helper that simplifies the code inside the loop() portion of your sketch, but is independent of the loop(). Think of it as giving a name to a piece of code that can be used over and over in the loop, without re-writing the same code each time. Therefore, it has to reside outside of the loop. Make sure that the closing bracket of the loop() is before the code for updateShiftRegister().