Professional Documents
Culture Documents
ARDUINO
WORKSHOP
A
HANDS-ON INTRODUCTION
W I T H 65 PROJECTS
JOHN BOXALL
The Hardware
The following hardware is required for this project:
One breadboard
Two push buttons (S1 and S2)
Two 10 kW resistors (R1 and R2)
Various connecting wires
Arduino and USB cable
The Schematic
The circuit schematic is shown in Figure8-30.
3V3 5V
Power
RST
Vin
D12
AREF
IO REF
Arduino
Digital Input/Output
N/C
A0
A1
A3
A4
D11
PWM
D10
PWM
D9
PWM
D8
D7
D6
PWM
D5
PWM
S1
D4
D3
Analog Input
A2
D13
S2
PWM
D2
D1
D0
TX
RX
SCL
A5
GND
SDA
R1
10k
R2
10k
The Sketch
Enter and upload this sketch:
// Project 31 Creating a Stopwatch
unsigned long start, finished, elapsed;
void setup()
{
Serial.begin(9600);
u pinMode(2, INPUT); // the start button
pinMode(3, INPUT); // the stop button
Serial.println("Press 1 for Start/reset, 2 for elapsed time");
}
void displayResult()
{
float h, m, s, ms;
unsigned long over;
elapsed = finished - start;
h
over
m
over
s
ms
=
=
=
=
=
=
int(elapsed / 3600000);
elapsed % 3600000;
int(over / 60000);
over % 60000;
int(over / 1000);
over % 1000;
Arduino Workshop
2013, John Boxall
if (digitalRead(3) == HIGH)
{
finished = millis();
delay(200); // for debounce
displayResult();
}
Arduino Workshop
2013, John Boxall