Stap 3: Codeing
Hieronder wordt de code gebruikt om het alarm uitvoert, Ive instellen zodat het de melodie van uiteindelijk bagatelliseert
int rcvpin = A0;
int buzzpin = 9;
VOID Setup
{
pinMode(rcvpin,INPUT);
pinMode(buzzpin,OUTPUT);
Buzz(200);
Buzz(200);
Buzz(200);
delay(2000);
Serial.begin(9600);
}
void loop
{
int ldrval=analogRead(rcvpin);
Serial.println(ldrval);
Als (ldrval < = 800)
{
Buzz(50);
playmelody();
}
}
#define C_note 261
#define C_sharp 277
#define D_note 293
#define D_sharp 311
#define E_note 329
#define F_note 349
#define F_sharp 369
#define G_note 391
#define G_sharp 415
#define A_note 440
#define A_sharp 466
#define B_note 246
int melody2 [] = {F_sharp, 16, E_note, 16, F_sharp, 64, B_note, 64,
G_note, 16, F_sharp, 16, G_note, 32, F_sharp, 32, E_note, 64,
G_note, 16, F_sharp, 16, G_note, 64, B_note, 64,
E_note, 16, D_note, 16, E_note, 32, D_note, 32,
C_note, 32, E_note, 32, D_note, 64, F_sharp, 16,
E_note, 16, F_sharp, 64, B_note, 64,
0, 0};
int melodie [] = {C_sharp, B_note, C_sharp, F_sharp, D_note, C_sharp, D_note, C_sharp, B_note, D_note,
C_sharp, D_note, F_sharp, B_note, A_note, B_note, A_note, G_sharp, B_note, A_note,
G_sharp, A_note, B_note, A_note, B_note, C_sharp, B_note, A_note, G_sharp, F_note,
D_note, C_sharp, C_sharp, C_sharp, D_note, C_sharp, B_note, C_sharp};
ongeldig playmelody() {}
int i = 0;
terwijl (melody2 [i] > 0) {}
Toon (buzzpin, melody2[i]);
delay(melody2[i+1]*10);
i = i + 2;
}
noTone(buzzpin);
}
VOID buzz (unsigned char keer)
{
analogWrite(buzzpin,170);
delay(time);
analogWrite(buzzpin,0);
delay(time);
}