งานปฏิบัติ 9
wiring diagram🔻
Code 1🔻
#include "LedControl.h"
#include "Keypad.h"
#define C4 262
#define D4 294
#define E4 330
#define F4 349
#define G4 392
#define A4 440
#define B4 494
#define C5 523
char keys[4][4]={
{7,8,9,0},
{4,5,6,0},
{1,2,3,0},
{0,0,0,0}};
byte rowPins[] = {7,6,5,4};
byte colPins[] = {3,2,1,0};
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,4,4);
int melody[] = {C4,D4,E4,F4,G4,A4,B4,C5};
int dl = 300;
int buzzerpin = 11;
LedControl lc=LedControl(8,10,9,1);
// Pin 8->DIN, 10->CLK, 9->CS(LOAD), 1 = No.of devices
void setup()
{
lc.shutdown(0,false);
lc.setIntensity(0,5);
lc.clearDisplay(0);
pinMode(buzzerpin,OUTPUT);
digitalWrite(buzzerpin,HIGH);
}
void loop()
{
char key = keypad.getKey();
if ((key != NO_KEY) && (key < 9))
{
lc.setDigit(0,0,key,false);
tone(buzzerpin, melody[key-1],dl);
delay(dl);
digitalWrite(buzzerpin,HIGH);
}
}
Code 2🔻
#include "LedControl.h"
#include "Keypad.h"
#define A3 220
#define B3 247
#define C4 262
#define D4 294
#define E4 330
#define F4 349
#define G4 392
#define A4 440
#define ST 0
int melody[] = {G4,G4,G4,G4,G4,G4,E4,D4,E4,G4,C4,G4,E4,
D4,E4,C4,D4,C4,A3,C4,C4,C4,A3,C4,ST,C4,A3,C4,C4,C4,A3,
C4,ST,G4,A4,G4,E4,D4,C4,ST};
float beats[] ={0.5,0.5,0.5,0.5,1.5,0.5,0.5,0.5,0.5,0.5,
1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1,1,1,1};
int buzzerpin = 11;
int timestop = 70;
char keys[4][4]={
{'7','8','9','0'},
{'4','5','6','0'},
{'1','2','3','0'},
{'0','0','0','0'}};
byte rowPins[] = {7,6,5,4};
byte colPins[] = {3,2,1,0};
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,4,4);
LedControl lc=LedControl(8,10,9,1);
// Pin 8->DIN, 10->CLK, 9->CS(LOAD), 1 = No.of devices
int m=1;
int num[2]; //num[0] to num[1]
char number;
int pw;
void setup()
{
lc.shutdown(0,false);
lc.setIntensity(0,5);
lc.clearDisplay(0);
pinMode(buzzerpin,OUTPUT);
digitalWrite(buzzerpin,HIGH);
randomSeed(analogRead(0));
pw = random(100);
}
void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
if (m==1)
lc.clearDisplay(0);
lc.setChar(0,m,key,false);
num[m]=key;
m = m-1;
if (m < 0)
{
delay(500);
number = ((num[1]-48)*10)+(num[0]-48);
if (number == pw)
{
lc.setChar(0,7,'0',false);
lc.setChar(0,6,'H',false);
int dl = 350;
int numnote;
numnote = sizeof(melody)/2;
for (int i=0;i<numnote;i++)
{
if (melody[i]>0)
tone(buzzerpin, melody[i],dl*beats[i]);
delay(dl*beats[i]);
digitalWrite(buzzerpin,HIGH);
delay(timestop);
}
}
else if (number < pw)
{
lc.setChar(0,7,'P',false);
lc.setChar(0,6,'P',false);
}
else if (number > pw)
{
lc.setChar(0,7,'L',false);
lc.setChar(0,6,'L',false);
}
m = 1;
}
}
}
ความคิดเห็น
แสดงความคิดเห็น