Instructions

25/06/2015 DFRduinoBeginnerKitForArduinoV3SKU:DFR0100RobotWiki
http://www.dfrobot.com/wiki/index.php/DFRduino_Beginner_Kit_For_Arduino_V3_SKU:DFR0100 17/23
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
47
*/
intbuttonPin=2;//buttonpin‐‐Digital2
intrelayPin=3;//relaypin‐‐Digital3
intrelayState=HIGH;
intbuttonState;
intlastButtonState=LOW;
longlastDebounceTime=0;
longdebounceDelay=50;
voidsetup(){
pinMode(buttonPin,INPUT);
pinMode(relayPin,OUTPUT);
digitalWrite(relayPin,relayState);
}
voidloop(){
//readthestateoftheswitchintoalocalvariable:
intreading=digitalRead(buttonPin);

//checktoseeifyoujustpressedthebutton
//(i.e.theinputwentfromLOWtoHIGH),andyou'vewaited
//longenoughsincethelastpresstoignoreanynoise:
//Iftheswitchchanged,duetonoiseorpressing:
if(reading!=lastButtonState){
lastDebounceTime=millis();
}

if((millis()‐lastDebounceTime)>debounceDelay){
//whateverthereadingisat,it'sbeenthereforlonger
//thanthedebouncedelay,sotakeitastheactualcurrentstate:
//ifthebuttonstatehaschanged:
if(reading!=buttonState){
buttonState=reading;

//onlytoggletheRelayifthenewbuttonstateisHIGH
if(buttonState==HIGH){
relayState=!relayState;
}
}
}
//settherelay: