-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton_n_buzzer.c
46 lines (35 loc) · 1.06 KB
/
button_n_buzzer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**********************************************************************
Program: Button and buzzer
This example program shows the use of a button to
control the buzzer
Hardware: CrowPi (with Raspberry Pi 3B+)
Software: Raspbian system with WiringPi library
Compile: $ gcc -o example button_n_buzzer.c -lwiringPi -Wall
Run with: $ ./example
Jan 2020
***********************************************************************/
#include <stdio.h>
#include <wiringPi.h>
#define BUTTON 37
#define BUZZER 12
int main()
{
// Setup WiringPi and pin modes
wiringPiSetupPhys();
pinMode(BUTTON, INPUT);
pinMode(BUZZER, OUTPUT);
// Print instruction
printf("\nSwitch ON #7 @ LEFT selection switches.\n");
printf("Press the UP button to switch on buzzer.\n");
printf("Press Ctrl-C to EXIT.\n");
// Main loop
while(1) {
if( !digitalRead(BUTTON) ) { // If button is pressed..
digitalWrite(BUZZER, HIGH); // ..switch ON buzzer
}
else { // Otherwise..
digitalWrite(BUZZER, LOW); // ..switch OFF buzzer
}
}
return 0;
}