Bit By Bit, Turn on 16 LEDs using 2x 74HC595 Shift Registers

Hello experts,

I am rather new to the Arduino so any help is much appreciated. I am sure there are other forum questions about this but i have been searching without any luck.

I am trying to turn on 16 LEDs individually using 2 74HC595 shift registers using the serial monitor. So the flow would be:
Serial monitor prompts user for which LED (0-15) to turn on
Input gets analyzed and single LED turned on
Repeat this process until a "x" character is entered to clear the data registers.

My issue is that when i reach ch = 8 (or the Q0 bit of the second register, i do not get the LED to turn on). Additionally, when i enter a value of '10', I get two entries - ch =1 and ch = 0.

Thoughts on how to make this work???

The code that I initally referenced is here...

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

byte leds1 = 0;
byte leds2 = 0;
 
void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  updateShiftRegister();
  Serial.begin(9600);
  while (! Serial); 
  Serial.println("Enter LED Number 0 to 15 or 'x' to clear");
}
 
void loop() 
{
  if (Serial.available())
  {
    int ch = Serial.read();
    Serial.print("ch = ");
    Serial.println(ch);
    if (ch >= 48 && ch <= 56) // betweeon 0 and 7 in integer
    {
      int led = ch - '0';
      
      bitSet(leds1, led);
      updateShiftRegister();
      Serial.print("Turned on LED ");
      Serial.println(led);
    }    
    if (ch >= 57 && ch <= 64) // betweeon 8 and 16 in integer
    {
      int led = ch - '0';
      
      bitSet(leds2, led);
      updateShiftRegister();
      Serial.print("Turned on LED ");
      Serial.println(led);
    }
    if (ch == 'x')
    {
      leds1 = 0;
      updateShiftRegister();
      Serial.println("Cleared");
    }
  }
}
 
void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds2);
   shiftOut(dataPin, clockPin, MSBFIRST, leds1);
   digitalWrite(latchPin, HIGH);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Hello experts,

I am rather new to the Arduino so any help is much appreciated. I am sure there are other forum questions about this but i have been searching without any luck.

I am trying to turn on 16 LEDs individually using 2 74HC595 shift registers using the serial monitor. So the flow would be:
Serial monitor prompts user for which LED (0-15) to turn on
Input gets analyzed and single LED turned on
Repeat this process until a "x" character is entered to clear the data registers.

My issue is that when i reach ch = 8 (or the Q0 bit of the second register, i do not get the LED to turn on). Additionally, when i enter a value of '10', I get two entries - ch =1 and ch = 0.

Thoughts on how to make this work???

The code that I initally referenced is here...

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

byte leds1 = 0;
byte leds2 = 0;

void setup() 
{
 pinMode(latchPin, OUTPUT);
 pinMode(dataPin, OUTPUT);  
 pinMode(clockPin, OUTPUT);
 updateShiftRegister();
 Serial.begin(9600);
 while (! Serial); 
 Serial.println("Enter LED Number 0 to 15 or 'x' to clear");
}

void loop() 
{
 if (Serial.available())
 {
   int ch = Serial.read();
   Serial.print("ch = ");
   Serial.println(ch);
   if (ch >= 48 && ch <= 56) // betweeon 0 and 7 in integer
   {
     int led = ch - '0';
     
     bitSet(leds1, led);
     updateShiftRegister();
     Serial.print("Turned on LED ");
     Serial.println(led);
   }    
   if (ch >= 57 && ch <= 64) // betweeon 8 and 16 in integer
   {
     int led = ch - '0';
     
     bitSet(leds2, led);
     updateShiftRegister();
     Serial.print("Turned on LED ");
     Serial.println(led);
   }
   if (ch == 'x')
   {
     leds1 = 0;
     updateShiftRegister();
     Serial.println("Cleared");
   }
 }
}

void updateShiftRegister()
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, leds2);
  shiftOut(dataPin, clockPin, MSBFIRST, leds1);
  digitalWrite(latchPin, HIGH);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

When you send "10" from serial monitor, 2 characters are sent, '1' and '0'. Receiving 10 requires 2 reads. The serial input basics tutorial shows how to receive multi digit numbers.

Give this a try:

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

unsigned int leds;

void setup() 
{
    pinMode(latchPin, OUTPUT);
    pinMode(dataPin, OUTPUT);  
    pinMode(clockPin, OUTPUT);
    updateShiftRegister();
    Serial.begin(9600);
    while(!Serial); 
    Serial.println("Enter LED Number 0 to 15 or 'x' to clear");
    leds = 0;
}

#define BUFF_SIZE   5
void loop() 
{
    int
        ch;
    int
        idx;
    char
        *ptr,
        chIncoming[BUFF_SIZE];

    idx = 0;
    do
    {
        if( Serial.available() > 0 )
        {
            ch = Serial.read();
            if( ch >= '0' && ch <= '9' || ch == 'x' )
            {
                chIncoming[idx] = (char)ch;
                idx++;
                if( idx > BUFF_SIZE-1 )
                    idx = BUFF_SIZE-1;
            }//if
            
        }//if
                    
    }while( ch != 0xa );
    
    chIncoming[idx] = 0;    //null termination

    if( strchr( chIncoming, (int)'x' ) )
    {
        leds = 0;
        updateShiftRegister();
        Serial.println("Cleared");
        
    }//if
    else
    {
        ch = (unsigned int)strtol( chIncoming, &ptr, 10 );
        if( ch >=0 && ch<=15 )
        {
            Serial.print("Adding LED "); Serial.println(ch);
            leds |= (1<<ch);
            updateShiftRegister();
            
            Serial.print("Bit Pattern: 0b");
            for( int i=0; i<16; i++ )    
                Serial.print( (leds & (1<<(15-i)))?"1":"0" );
            Serial.println();
                
        }//if
                
    }//else
    
}//loop

void updateShiftRegister()
{
    digitalWrite( latchPin, LOW );
    shiftOut( dataPin, clockPin, MSBFIRST, ((leds >> 8) & 0xff) );
    shiftOut( dataPin, clockPin, MSBFIRST, (leds & 0xff) );
    digitalWrite( latchPin, HIGH );
    
}//updateShiftRegister

As for LEDs in the 2nd register not showing up, double-check your wiring, connections and power.

Cross posted in Project Guidance. Don't cross post. Cross posting wastes member's time.

Threads merged.