-
Notifications
You must be signed in to change notification settings - Fork 0
/
hacklab-ArduinoComic-Page-12.html
68 lines (68 loc) · 1.79 KB
/
hacklab-ArduinoComic-Page-12.html
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-type"/>
<meta content="en-us" http-equiv="Content-Language"/>
<title>
/hacklab-ArduinoComic-Page-12
</title>
</head>
<body>
PAGE 12: Digital input: setting up a switch to turn LED on and off
<br/>
Panel 45:
<br/>
Drawing: hand holding switch
<br/>
(In text panel): Next we will add a switch, a digital input, so we can turn the LED off and on.
<br/>
<br/>
Panel 46:
<br/>
Drawing: Detail of switch attached to board along with LED and resistors
<br/>
(In text panel): Connect one end of a momentary switch to pin 4 on the
<br/>
Arduino, with a 10k resistor connected to ground attached to the same end. Attach the other end to power. We will leave the LED attached to the same pin.
<br/>
<br/>
<br/>
Panel 47:
<br/>
Drawing: code for switch script written on white shape
<br/>
void setup() {
<br/>
pinMode(2, OUTPUT);
<br/>
pinMode(4, INPUT);
<br/>
}
<br/>
<br/>
void loop() {
<br/>
if(digitalRead(4)){
<br/>
digitalWrite(2, HIGH);
<br/>
}else{
<br/>
digitalWrite(2, LOW);
<br/>
}
<br/>
}
<br/>
(In text panels): Next we’ll write the code. In setup, we declare pin 2 an output and pin 4 an input. In loop, we use an if statement, if we read pin 4 as high, we set the LED pin to high, otherwise we set the LED pin to low, turning it off.
<br/>
<br/>
Panel 48:
<br/>
Drawing: split panel, finger pushing switch with LED lit, finger lets up and LED is off
<br/>
(In text panel) The LED lights when the switch is held down.
<br/>
<br/>
</body>
</html>