Press the button, led ON, press again led OFF
*/
int led
=
5
;
// The D5 pin,driving LED
int button
=
A0
;
// The A0,read the button,Here used a analog pin as digital pin.
void setup
()
{
pinMode
(
led
,
OUTPUT
)
;
// initialize the LED pin as an output.
pinMode
(
button
,
INPUT_PULLUP
)
;
// initialize the BUTTON pin as an input.
}
void loop
()
{
if
(
digitalRead
(
button
)
==
LOW
){
delay
(
200
)
;
// wait for 200 microsecond,Avoid pressing the button and read many times in this very short time
digitalWrite
(
led
,
HIGH
)
;
// turn the LED on (HIGH is the voltage level)
while
(
1
){
if
(
digitalRead
(
button
)
==
LOW
){
delay
(
200
)
;
digitalWrite
(
led
,
LOW
)
;
// turn the LED off (LOW is the voltage level)
break
;
//End of the while loop,Back to the main loop
}}
}}
Part3. Vibration sensor control passive buzzer
/*
PART3 Vibration sensors CONTROL Passive buzzer
Knock on the table, the buzzer will ring
*/
int vibration
=
A0
;
// The A0 pin,read Vibration sensors
int buzzer
=
6
;
// The D6 pin,driving the Passive buzzer,the pin must PWM pin(3 5 6 9 10 11 on UNO)
void setup
()
{
pinMode
(
vibration
,
INPUT_PULLUP
)
;
// initialize the vibration pin as an input.
pinMode
(
buzzer
,
OUTPUT
)
;
// initialize the buzzer pin as an output.
}
void loop
()
{
if
(
digitalRead
(
vibration
)
==
HIGH
){
analogWrite
(
buzzer
,
200
)
;
//driver Passive buzzer must PWM,so analogWrite,200 is PWM value,max 1024
delay
(
1000
)
;
//wait for 1000 microsecond
analogWrite
(
buzzer
,
0
)
;
//turn off the buzzer
}
}
Summary of Contents for Arduino Starter Kit
Page 8: ......