Published

Piezo Buzzer Piano 4x4 Keypad LCD

Each buzzer pressed on the keypad plays a note and displays on the LCD the button's character.

BeginnerShowcase (no instructions)1 hour1,880
Piezo Buzzer Piano 4x4 Keypad LCD

Story

Read more

Schematics

Capture.PNG

Code

keypadecranspeaker.ino

C/C++
#include <LiquidCrystal.h>
#include <Keypad.h>
 
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;

const byte row=4;
const byte col=4;
int i=1;
int speakerPin=13;

LiquidCrystal lcd(12,11,A0,A1,A2,A3);

char butoane[row][col]=
{
{'1', '2', '3', 'A'}, 
{'4', '5', '6', 'B'}, 
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rowPins[row]={9,8,7,6};
byte colPins[col]={5,4,3,2};

Keypad myKeypad= Keypad(makeKeymap(butoane), rowPins, colPins,row, col);

int tones[] = { 31, 104, 330, 1432, 880, 2093, 1014, 956 }; //freq
int Cur_tone = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
    lcd.begin(16, 2);
      lcd.setCursor(0,0);
      pinMode(speakerPin,OUTPUT);
      pinMode(butoane[1][1],INPUT);
       pinMode(butoane[1][2],INPUT);
        pinMode(butoane[1][3],INPUT);
         pinMode(butoane[1][4],INPUT);
         pinMode(butoane[2][1],INPUT);
       pinMode(butoane[2][2],INPUT);
        pinMode(butoane[2][3],INPUT);
         pinMode(butoane[2][4],INPUT);
            pinMode(butoane[3][1],INPUT);
       pinMode(butoane[3][2],INPUT);
        pinMode(butoane[3][3],INPUT);
         pinMode(butoane[3][4],INPUT);
            pinMode(butoane[4][1],INPUT);
       pinMode(butoane[4][2],INPUT);
        pinMode(butoane[4][3],INPUT);
         pinMode(butoane[4][4],INPUT);
         
}
void beep(int note, int duration, int Cur_tone)
{
  tone(speakerPin, note, duration);
      delayMicroseconds(Cur_tone);
       digitalWrite(speakerPin, LOW);
    delayMicroseconds(Cur_tone);

}


void loop() 
{
  char key=myKeypad.getKey();

  if(key)
  {digitalWrite(speakerPin, HIGH);
    delayMicroseconds(Cur_tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(Cur_tone);}
   

  if (key)
  {
     lcd.print(key);
  
  
  i++;
  if (key=='1')
  
  {
    Cur_tone = tones[0];
     beep(a, 500,Cur_tone);

    
 
  }
   
  if (key=='2')
  
  {
  
    Cur_tone = tones[1];
     beep(a, 500,Cur_tone);

  }
   

 if (key=='3')
  
  {
  
    Cur_tone = tones[2];
     beep(a, 500,Cur_tone);

  }


 if (key=='A')
  
  {
    
Cur_tone = tones[3]; beep(f, 350,Cur_tone); }

  if (key=='4')
  
  {   Cur_tone = tones[4];
   beep(cH, 150,Cur_tone);
  }

if (key=='5')
  
  {
    
   Cur_tone = tones[5];
   
  beep(a, 500,Cur_tone);  

  }

 if (key=='6')
  
  {
    
    Cur_tone = tones[6];
      beep(f, 350,Cur_tone);
  }

 if (key=='B')
  
  {
    
   Cur_tone = tones[7];
   beep(cH, 150,Cur_tone);
  } 
  if (key=='4')
  
  {
    Cur_tone = tones[0];
       beep(a, 650,Cur_tone);

    
 
  }
    if (key=='5')
  
  {
    Cur_tone = tones[1];
    
  beep(eH, 500,Cur_tone);


    
 
  }
   if (key=='6')
  
  {
    Cur_tone = tones[2];
       beep(eH, 500,Cur_tone);

    
 
  }
     if (key=='C')
  
  {
    Cur_tone = tones[2];
       beep(eH, 500,Cur_tone);}

    
 
   

     if (key=='7')
  
  {
    Cur_tone = tones[2];
       beep(fH, 350,Cur_tone);}

    
 
   

    if (key=='8')
  
  {
    Cur_tone = tones[2];
       beep(cH, 150,Cur_tone);

    
 
    
   }
    if (key=='9')
  
  {
    Cur_tone = tones[2];
       beep(gS, 500,Cur_tone);}
  
   
  if (key=='*')
  
  {
    Cur_tone = tones[4];
     beep(f, 350,Cur_tone);

    
 
  }
   if (key=='0')
  
  {
    Cur_tone = tones[5];
     beep(cH, 150,Cur_tone);

    
 
  }
  if (key=='#')
  
  {
    Cur_tone = tones[6];
     beep(a, 650,Cur_tone);

    
 
  }
  if (key=='D')
  
  {
    Cur_tone = tones[7];
     beep(aH, 500,Cur_tone);

    

  }
 

    
  }
  else 
  {
    digitalWrite(speakerPin, LOW);
  }
  if (i==17){lcd.setCursor(0,1);}
  if (i==34){lcd.clear(); i=0;}
}

Credits

Comments

Please log in or sign up to comment.