Hardware Reference
In-Depth Information
To do this, the sketch sets the TinkerKit outputs to digital mode and constantly
monitors the status of the buttons. This sketch will use two TinkerKit outputs:
Out A and Out B. Out A will handle the left-hand side motor, and Out B will
control the right-hand side motor. To go forward, both motors will be activated
at the same time. To turn, only one motor will be activated. The sketch will
look like Listing 21-1.
Listing 21-1: Sketch (fi lename: Chapter21.ino )
1 #include <Esplora.h>
2
3 #define OUTA 3 // Pin TinkerKit Out A is connected to
4 #define OUTB 11 // Pin TinkerKit Out B is connected to
5
6 void setup()
7 {
8 pinMode(OUTA, OUTPUT); // TinkerKit A to output
9 pinMode(OUTB, OUTPUT); // TinkerKit B to output
10 }
11
12 void loop()
13 {
14 boolean outputA = LOW;
15 boolean outputB = LOW;
16
17 if (Esplora.readButton(SWITCH_UP) == LOW)
18 outputA = outputB = HIGH;
19
20 if (Esplora.readButton(SWITCH_LEFT) == LOW)
21 outputB = HIGH;
22
23 if (Esplora.readButton(SWITCH_RIGHT) == LOW)
24 outputA = HIGH;
25
26 digitalWrite(OUTA, outputA);
27 digitalWrite(OUTB, outputB);
28 }
On line 1, the Esplora library is imported, the i rst thing needed for this project.
On lines 3 and 4, there are some define directives, which dei ne the digital
pins used on the TinkerKit outputs because there are no functions available to
write to the TinkerKit pins directly. Because you have to do this the old way,
it requires pinMode() calls in setup() . This is done on line 8 and 9; both pins
are set to OUTPUT .
loop() is declared on line 12, and this is where the buttons will be read and,
if necessary, the outputs will be written to. It starts on line 14 with the creation
of two variables: outputA and outputB . As you can imagine, they will be used to
Search WWH ::




Custom Search