Arduino with shift register. Need help with the code!!

So the idea is to control multiple LEDs using a shift register 74HC595. I have connected all the wires and got the code even to work.

However, what i am currently struggling with is controlling each LED with the Serial port.

In my code i can control each LED using the loop function. But, not through the Serial port.

I have tried, Serial.read() but this allows me to light only one LED at a time. Not multiple ones.

Any help is appreciated.

LED_SR.ino (1.93 KB)

Where are you using Serial.read()?

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

// name of SR pin -> Arduino pin # connected to SR pin
#define SER    9  // data in
#define SRCLK  11  // shift register clock
#define SRCLR  13  // clear shift register
#define RCLK   10  // storage register (sometimes called the latch pin)
#define OE     12  // enable output

#define TOTAL_SHIFT_PINS  8
int c;
int b;
//int index;
char a[7];
void setup() {
  Serial.begin(9600); 
  pinMode(SER,OUTPUT);
  pinMode(SRCLK,OUTPUT);
  pinMode(SRCLR,OUTPUT);
  pinMode(RCLK,OUTPUT);
  pinMode(OE,OUTPUT);
  clearShiftRegisters();
  turnOutputsOn();
}

void loop()
{
 if (Serial.available() > 0){
  c = Serial.read() - 48;
  lightOnlyOnePin(c);
 }
 //lightOnlyOnePin(3);
 //lightOnlyOnePin(1);
}

void lightOnlyOnePin(int i) {
  int j;
  for(j=0;j<TOTAL_SHIFT_PINS;++j) {
    int data = (i==j? HIGH : LOW);
    shiftDataIn(data);
  }
  copyShiftToStorage();
}


// This doesn't change the value stored in the storage registers.
void turnOutputsOn() {
  digitalWrite(OE,LOW);
}

// This doesn't change the value stored in the storage registers.
void turnOutputsOff() {
  digitalWrite(OE,HIGH);
}

// clear the shift registers without affecting the storage registers.
void clearShiftRegisters() {
  digitalWrite(SRCLR,LOW);
  digitalWrite(SRCLR,HIGH);
}

// All the data in the shift registers moves over 1 unit and the new data goes in at shift register 0.
// The data that was in the shift register 7 goes to the next register (if any).
void shiftDataIn(int data) {
  digitalWrite(SER,data);
  digitalWrite(SRCLK,HIGH);
  digitalWrite(SRCLK,LOW);
}

// copy the 8 shift registers into the shift registers,
// which changes the output voltage of the pins.
void copyShiftToStorage() {
  digitalWrite(RCLK,HIGH);
  digitalWrite(RCLK,LOW);
}

@larryd

Thanks for the reply.
So, i added the serial.read() in the loop function. It lights up the LEDs one at a time. What i would like it do is to get the previous LED to stay lit as i light the next one.
It only does it when i pass the function lightOnlyOnePin(), multiple times in the loop function. But unfortunately this doesn’t happen when i include the function within the serial.available(), if loop.

Always, always, always, always, always, always show us a schematic!

Is this your circuit?


"What i would like it do is to get the previous LED to stay lit as i light the next one."

So you want the following results?

                                       76543210
You send ASCII '0'  you get LEDs       00000001
If you send '7' you want               10000001
If you send '3' you want               10001001

etc.

If so, how are you going to turn off the LEDs?


See the line of code with <------<<<<

//Version 1

//name of SR pin -> Arduino pin # connected to SR pin
//                     74HC595
#define SER    9   //    14    data in
#define SRCLK  11  //    11    shift register clock
#define SRCLR  13  //    10    clear shift register
#define RCLK   10  //    12    storage register (sometimes called the latch pin)
#define OE     12  //    13    enable output

#define TOTAL_SHIFT_PINS  8

byte rxCharacter;
byte ourByte;

//************************************************************************
void setup()
{
  Serial.begin(9600);
  pinMode(SER, OUTPUT);

  pinMode(SRCLK, OUTPUT);
  digitalWrite(SRCLK, LOW);

  pinMode(SRCLR, OUTPUT);
  digitalWrite(SRCLR, HIGH);

  pinMode(RCLK, OUTPUT);
  digitalWrite(RCLK, LOW);

  pinMode(OE, OUTPUT);
  digitalWrite(OE, HIGH);

  clearShiftRegisters();

  turnOutputsOn();

} //END of setup()

//************************************************************************
void loop()
{
  //*******************************
  if (Serial.available() > 0)
  {
    rxCharacter = Serial.read() - 48;

    //only allow numbers 0to7?
    if (rxCharacter >= 0 && rxCharacter < 8)
    {
      lightOnlyOnePin(rxCharacter);
    }

  }

  //*******************************


} //END of loop()


//************************************************************************
void lightOnlyOnePin(byte rxData)
{
  //If you want one bit at a time, uncomment the next line of code <------<<<<
  //ourByte = 0;
  
  //the requested bit is set to a 1 (HIGH)
  bitSet(ourByte, rxData);

  //send the byte to the 74HCT595 shift register
  shiftDataIn(ourByte);

  //transfer the 75HCT595 shift register to the LEDs
  copyShiftToStorage();

} //END of lightOnlyOnePin(byte rxData)

//************************************************************************
//all 8 bits in our byte are sent to the shift register
void shiftDataIn(byte data)
{
  byte thisBit = 0;

  //send the 8 bits to the 74HCT595 shift register
  for (byte x = 0; x < TOTAL_SHIFT_PINS; x++)
  {
    //get the value of this bit
    thisBit = bitRead(data, x);

    digitalWrite(SER, thisBit);
    digitalWrite(SRCLK, HIGH);
    digitalWrite(SRCLK, LOW);
  }

} //END of shiftDataIn(int data)

//************************************************************************
//This doesn't change the value stored in the storage registers.
void turnOutputsOn()
{
  digitalWrite(OE, LOW);

} //END of turnOutputsOn()

//************************************************************************
//This doesn't change the value stored in the storage registers.
void turnOutputsOff()
{
  digitalWrite(OE, HIGH);

} //END of turnOutputsOff()

//************************************************************************
//clear the shift registers without affecting the storage registers.
void clearShiftRegisters()
{
  digitalWrite(SRCLR, LOW);
  digitalWrite(SRCLR, HIGH);

} //END of clearShiftRegisters()

//************************************************************************
// copy the 8 shift registers into the shift registers,
// which changes the output voltage of the pins.
void copyShiftToStorage()
{
  digitalWrite(RCLK, HIGH);
  digitalWrite(RCLK, LOW);

} //END of copyShiftToStorage()

//************************************************************************