74HC595, LEDs, Button, And Sound

halo guys, i have a project with Arduino Mega, 74HC595, 58 LEDs, 26 Buttons and sound(MP3 player).

the system maybe simple to you guys,

you push button number 1 so LED number 1 and sound will be trigger.

if you pushs the button number 2, the LED number 2 will be trigger to On and LED number 1 will go OFF.

and if you push button number 3 the LED number 1 and 2 will be trigger to ON.

the sound(Mp3 player) and button have already working properly, the problem is, I can't control the LEDs.

and I've been assembly the button into matrix 6x5 (LEDs total are 26).

I'm using the 74HC595 to control the LEDs. and i don't understand how to program the LEDs

i will give you the syntax i made before, i hope you guys can help me.

-Sorry for the bad English

#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>


int r1=A1;
int r2=A2;
int r3=A3;
int r4=A4;
int r5=A5;
int r6=A6;
int c1=A8;
int c2=A9;
int c3=A10;
int c4=A11;
int c5=A12;
int colm1;
int colm2;
int colm3;
int colm4;
int colm5;

//------------------ Init 74HC595 ------------------------------------------
int clockPin = 12; //IC Pin 11, Yellow Jumper
int dataPin = 11; //IC Pin 14, Blue Jumper
int latchPin = 8; //IC Pin 12, Green Jumper


byte patterns[30] = {
B00000001,};
byte patterns2[30] ={
  B00000010,
};

int index = 0;
int count = sizeof(patterns) / 2;
int count1 = sizeof(patterns2)/2;

//------------------------- VOID -------------------------------------------------
void setup() {
 
  pinMode(r1,OUTPUT);
  pinMode(r2,OUTPUT);
  pinMode(r3,OUTPUT);
  pinMode(r4,OUTPUT);
  pinMode(r5,OUTPUT);
  pinMode(r6,OUTPUT);
  pinMode(c1,INPUT);
  pinMode(c2,INPUT);
  pinMode(c3,INPUT);
  pinMode(c4,INPUT);
  pinMode(c5,INPUT);
  digitalWrite(c1,HIGH);
  digitalWrite(c2,HIGH);
  digitalWrite(c3,HIGH);
  digitalWrite(c4,HIGH);
  digitalWrite(c5,HIGH);
  Serial.begin(9600);  

pinMode(latchPin, OUTPUT); 
pinMode(clockPin, OUTPUT); 
pinMode(dataPin, OUTPUT);


mp3_set_serial (Serial);
delay(1);
mp3_set_volume(20);}

void loop() {
/*digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, patterns[index * 2]);
digitalWrite(latchPin, HIGH);
delay(patterns[(index * 2) + 1]);
//index++;
if (index >= count){
index = 0;*/



  digitalWrite(r1,LOW);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  digitalWrite(r5,HIGH);
  digitalWrite(r6,HIGH);
  colm1=digitalRead(c1);
  colm2=digitalRead(c2);
  colm3=digitalRead(c3);
  colm4=digitalRead(c4);
  colm5=digitalRead(c5);
  if(colm1==LOW){
  {Serial.println("A1");
  digitalWrite (latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, patterns[index*2]); 
  digitalWrite(latchPin, HIGH); 
  delay(patterns[(index*2)+1]);
   (index >= count);{ index =0;
     //delay(200);
     mp3_play(15);
    //digitalWrite(clear, LOW);
    delay(10000);
     }}}
  else 
   // digitalWrite(latchPin, HIGH); 
  
  {
   if(colm2==LOW)
   {Serial.println("A2");
   digitalWrite(latchPin, HIGH);
   shiftOut(dataPin, clockPin, MSBFIRST, patterns2[index*2]);
   digitalWrite(latchPin, LOW);
   delay(patterns2[(index*2)+1]);
   (index>=count1);{index = 0;
   //mp3_play(16);
    //delay(10000);
    }}
   else
   {
   if(colm3==LOW)
   {Serial.println("A3");
     delay(200);}
   else
   {
   if(colm4==LOW)
   {Serial.println("A4");
      delay(200);}
    else 
    {
    if(colm5==LOW)
    {Serial.println("A5");
    delay(200);}
   }}}

  digitalWrite(r1,HIGH);
  digitalWrite(r2,LOW);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  digitalWrite(r5,HIGH);
  digitalWrite(r6,HIGH);
  colm1=digitalRead(c1);
  colm2=digitalRead(c2);
  colm3=digitalRead(c3);
  colm4=digitalRead(c4);
  colm5=digitalRead(c5);
  if(colm1==LOW)
  {Serial.println("A6");
    delay(200);}
  else
  {
   if(colm2==LOW)
   {Serial.println("A7");
    delay(200);}
   else
   {
   if(colm3==LOW)
   {Serial.println("A8");
      delay(200);}
   else
   {
   if(colm4==LOW)
   {Serial.println("A9");
       delay(200);}
   else
   {
    if(colm5==LOW)
    {Serial.println("A10");
    delay(200);}
   }}}

  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,LOW);
  digitalWrite(r4,HIGH);
  digitalWrite(r5,HIGH);
  digitalWrite(r6,HIGH);
  colm1=digitalRead(c1);
  colm2=digitalRead(c2);
  colm3=digitalRead(c3);
  colm4=digitalRead(c4);
  colm5=digitalRead(c5);
  if(colm1==LOW)
  {Serial.println("A11");
     delay(200);}
  else
  {
   if(colm2==LOW)
   {Serial.println("A12");
       delay(200);}
   else
   {
   if(colm3==LOW)
   {Serial.println("A13");
        delay(200);}
   else
   {
   if(colm4==LOW)
   {Serial.println("A14");
        delay(200);}
        else
        {
          if(colm5==LOW)
          {Serial.println("A15");
          delay(200);}
   }}}
  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,LOW);
  digitalWrite(r5,HIGH);
  digitalWrite(r6,HIGH);
  colm1=digitalRead(c1);
  colm2=digitalRead(c2);
  colm3=digitalRead(c3);
  colm4=digitalRead(c4);
  colm5=digitalRead(c5);
  if(colm1==LOW)
  {Serial.println("A16");
      delay(200);}
  else
  {
   if(colm2==LOW)
   {Serial.println("A17");
        delay(200);}
   else
   {
   if(colm3==LOW)
   {Serial.println("A18");
      delay(200);}
   else
   {
   if(colm4==LOW)
   {Serial.println("A19");
       delay(200);}
      else
      {
        if(colm5==LOW)
        {Serial.println("A20");
        delay(200);}
      }}}
  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  digitalWrite(r5,LOW);
  digitalWrite(r6,HIGH);
  colm1=digitalRead(c1);
  colm2=digitalRead(c2);
  colm3=digitalRead(c3);
  colm4=digitalRead(c4);
  colm5=digitalRead(c5);
      if(colm1==LOW)
  {Serial.println("21");
      delay(200);}
  else
  {
   if(colm2==LOW)
   {Serial.println("22");
        delay(200);}
   else
   {
   if(colm3==LOW)
   {Serial.println("23");
      delay(200);}
   else
   {
   if(colm4==LOW)
   {Serial.println("24");
       delay(200);}
      else
      {
        if(colm5==LOW)
        {Serial.println("25");
        delay(200);}
      }}}

  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  digitalWrite(r5,HIGH);
  digitalWrite(r6,LOW);
  colm1=digitalRead(c1);
  colm2=digitalRead(c2);
  colm3=digitalRead(c3);
  colm4=digitalRead(c4);
  colm5=digitalRead(c5);
  
  if(colm1==LOW)
  {Serial.println("26");
      delay(200);}
  else
  {
   if(colm2==LOW)
   {Serial.println("27");
        delay(200);}
   else
   {
   if(colm3==LOW)
   {Serial.println("28");
      delay(200);}
   else
   {
   if(colm4==LOW)
   {Serial.println("29");
       delay(200);}
      else
      {
        if(colm5==LOW)
        {Serial.println("30");
        delay(200);}
}
      }     }}}}}}}}

cobaSoreIni_contoh_Forum.ino (6.07 KB)

We need you to post a schematic. Ii is not obvious how all these switches and leds are connected to the Arduino and shift register.

I suggest that you make your code more readable first. Your loop() is far too big. And something like below indicates a wrong approach

                  }
                }
              }
            }
          }
        }
      }
    }
  }

You seem to have a matrix for your buttons. Maybe the below will help in the cleanup

const byte rowPins[] = {A1, A2, A3, A4, A5, A6};
const byte columnPins[] = {A8, A9, A10, A11, A12};
...
...
void setup()
{
  // set the row pins as output
  for (int rowcnt = 0; rowcnt < sizeof(rowPins); rowcnt++)
  {
    pinMode(rowPins[rowcnt], OUTPUT);
  }

  // set the column pins as input
  for (int colcnt = 0; colcnt < sizeof(columnPins); colcnt++)
  {
    pinMode(columnPins[colcnt], INPUT);
  }
}

And write a function to read the buttons

/*
  read which button is pressed in the matrix
  returns:
    0 if no button is pressed else a number representing the button
 */
byte readButtons()
{
  // reset all row pins to high
  for (int rowcnt = 0; rowcnt < sizeof(rowPins); rowcnt++)
  {
    digitalWrite(rowPins[rowcnt], HIGH);
  }

  // loop through all row pins
  for (int rowcnt = 0; rowcnt < sizeof(rowPins); rowcnt++)
  {
    // set one low
    digitalWrite(rowPins[rowcnt], LOW);

    // check the columns
    for (int colcnt = 0; colcnt < sizeof(columnPins); colcnt++)
    {
      if (digitalRead(columnPins[colcnt]) == LOW)
      {
        // return a number representiing the button that was pressed
        return (rowcnt * sizeof(rowPins)) + colcnt + 1;
      }
    }
    // set back to HIGH before using next row pin
    digitalWrite(rowPins[rowcnt], HIGH);
  }

  // indicate no button was pressed
  return 0;
}

This function should return 1..5 or 7..11 or 13..17 or 10..23, 25..29 or 31..35 if a button was pressed and 0 if no button is pressed.

I don't have a Mega so can't test.

You can write a small test sketch to test

[code]
const byte rowPins[] = {A1, A2, A3, A4, A5, A6};
const byte columnPins[] = {A8, A9, A10, A11, A12};
void setup()
{
  // setup serial; adjust baudrate to your needs
  Serial.begin(115200);

  // set the row pins as output
  for (int rowcnt = 0; rowcnt < sizeof(rowPins); rowcnt++)
  {
    pinMode(rowPins[rowcnt], OUTPUT);
  }

  // set the column pins as input
  for (int colcnt = 0; colcnt < sizeof(columnPins); colcnt++)
  {
    pinMode(columnPins[colcnt], INPUT);
  }
}

/*
  read which button is pressed in the matrix
  returns:
    0 if no button is pressed else a number representing the button
 */
byte readButtons()
{
  // reset all row pins to high
  for (int rowcnt = 0; rowcnt < sizeof(rowPins); rowcnt++)
  {
    digitalWrite(rowPins[rowcnt], HIGH);
  }

  // loop through all row pins
  for (int rowcnt = 0; rowcnt < sizeof(rowPins); rowcnt++)
  {
    // set one low
    digitalWrite(rowPins[rowcnt], LOW);

    // check the columns
    for (int colcnt = 0; colcnt < sizeof(columnPins); colcnt++)
    {
      if (digitalRead(columnPins[colcnt]) == LOW)
      {
        // return a number representiing the button that was pressed
        return (rowcnt * sizeof(rowPins)) + colcnt + 1;
      }
    }
    // set back to HIGH before using next row pin
    digitalWrite(rowPins[rowcnt], HIGH);
  }

  // indicate no button was pressed
  return 0;
}

void loop()
{
  byte button = readButtons();
  if(button != 0)
  {
    Serial.println(button);
  }
}

And next, as PaulRB says, a schematic will be useful. You can provide a photo/scan of a handdrawn one if needed.

PaulRB:
We need you to post a schematic. Ii is not obvious how all these switches and leds are connected to the Arduino and shift register.

sterretje:
I suggest that you make your code more readable first. Your loop() is far too big. And something like below indicates a wrong approach

                  }

}
             }
           }
         }
       }
     }
   }
 }




You seem to have a matrix for your buttons. Maybe the below will help in the cleanup



const byte rowPins[] = {A1, A2, A3, A4, A5, A6};
const byte columnPins[] = {A8, A9, A10, A11, A12};
...
...
void setup()
{
 // set the row pins as output
 for (int rowcnt = 0; rowcnt < sizeof(rowPins); rowcnt++)
 {
   pinMode(rowPins[rowcnt], OUTPUT);
 }

// set the column pins as input
 for (int colcnt = 0; colcnt < sizeof(columnPins); colcnt++)
 {
   pinMode(columnPins[colcnt], INPUT);
 }
}




And write a function to read the buttons


/*
 read which button is pressed in the matrix
 returns:
   0 if no button is pressed else a number representing the button
*/
byte readButtons()
{
 // reset all row pins to high
 for (int rowcnt = 0; rowcnt < sizeof(rowPins); rowcnt++)
 {
   digitalWrite(rowPins[rowcnt], HIGH);
 }

// loop through all row pins
 for (int rowcnt = 0; rowcnt < sizeof(rowPins); rowcnt++)
 {
   // set one low
   digitalWrite(rowPins[rowcnt], LOW);

// check the columns
   for (int colcnt = 0; colcnt < sizeof(columnPins); colcnt++)
   {
     if (digitalRead(columnPins[colcnt]) == LOW)
     {
       // return a number representiing the button that was pressed
       return (rowcnt * sizeof(rowPins)) + colcnt + 1;
     }
   }
   // set back to HIGH before using next row pin
   digitalWrite(rowPins[rowcnt], HIGH);
 }

// indicate no button was pressed
 return 0;
}



This function should return 1..5 or 7..11 or 13..17 or 10..23, 25..29 or 31..35 if a button was pressed and 0 if no button is pressed.

I don't have a Mega so can't test.

You can write a small test sketch to test


[code]
const byte rowPins[] = {A1, A2, A3, A4, A5, A6};
const byte columnPins[] = {A8, A9, A10, A11, A12};
void setup()
{
 // setup serial; adjust baudrate to your needs
 Serial.begin(115200);

// set the row pins as output
 for (int rowcnt = 0; rowcnt < sizeof(rowPins); rowcnt++)
 {
   pinMode(rowPins[rowcnt], OUTPUT);
 }

// set the column pins as input
 for (int colcnt = 0; colcnt < sizeof(columnPins); colcnt++)
 {
   pinMode(columnPins[colcnt], INPUT);
 }
}

/*
 read which button is pressed in the matrix
 returns:
   0 if no button is pressed else a number representing the button
*/
byte readButtons()
{
 // reset all row pins to high
 for (int rowcnt = 0; rowcnt < sizeof(rowPins); rowcnt++)
 {
   digitalWrite(rowPins[rowcnt], HIGH);
 }

// loop through all row pins
 for (int rowcnt = 0; rowcnt < sizeof(rowPins); rowcnt++)
 {
   // set one low
   digitalWrite(rowPins[rowcnt], LOW);

// check the columns
   for (int colcnt = 0; colcnt < sizeof(columnPins); colcnt++)
   {
     if (digitalRead(columnPins[colcnt]) == LOW)
     {
       // return a number representiing the button that was pressed
       return (rowcnt * sizeof(rowPins)) + colcnt + 1;
     }
   }
   // set back to HIGH before using next row pin
   digitalWrite(rowPins[rowcnt], HIGH);
 }

// indicate no button was pressed
 return 0;
}

void loop()
{
 byte button = readButtons();
 if(button != 0)
 {
   Serial.println(button);
 }
}




And next, as PaulRB says, a schematic will be useful. You can provide a photo/scan of a handdrawn one if needed.

[/code]
actually the button is working properly, and the schematic is a matrix, I will attach the schematic later, or 

maybe hand drawn. the problem is, how can I control the 74HC595 like I control LEDs using button in the 

Arduino IDE example

I suspect that your current code is working properly. But it's close to impossible to maintain that code. Your loop() is around 300 lines of code which is too much.

Years ago, the rule of thumb was that a function should fit on a sheet of A4 paper (dot matrix printer) when printed.

sterretje:
I suspect that your current code is working properly. But it's close to impossible to maintain that code. Your loop() is around 300 lines of code which is too much.

Years ago, the rule of thumb was that a function should fit on a sheet of A4 paper (dot matrix printer) when printed.

yes it is no error in my code, but also, i can't control the LEDs using 74HC595 with button, the Audio is

working normally, when I press the button, the sound comes out, but the LEDs not working.

is there anyway to program the 74HC595 so it could work like button and leds without 74HC595

Please define "the LEDs not working". Does it do something but not what you expected; does it do nothing at all?

And show your diagram; most bets are off if we don't know how it's all connected. Also double and triple check your wiring.

You can use below code to test a single 74HC595; after adjusting the pin numbers it should do something

//74HC595
const int latchPin = 4;
const int clockPin = 3;
const int dataPin = 2;

void setup() {

  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
}

byte ledpattern[] =
{
  B00000001,
  B00000011,
  B00000111,
  B00001111,
  B00011110,
  B00111100,
  B01111000,
  B11110000,
  B11100000,
  B11000000,
  B10000000,
};


void loop() {

  static int index = 0;
  static int direction = 1;
  registerWrite(ledpattern[index]);
  delay(25);


  index += direction;
  if (index == 0 || index == sizeof(ledpattern) - 1)
    direction *= -1;
}

void registerWrite(byte pattern) {
  // inverted logic because LEDs are wired from Vcc to pins
  pattern ^= 0xFF;
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, pattern);
  digitalWrite(latchPin, HIGH);
}