Hardware Reference
In-Depth Information
Sketch
The code that will be used for this sketch is presented in Listing 18-1.
Listing 18-1: Sketch (fi lename: Chapter18.ino)
1 #include <SD.h>
2 #include <SPI.h>
3 #include <Audio.h>
4
5 const int buttonPin = 2; // The pushbutton pin
6 const int sensorPin = A5; // The analog input pin
7
8 void setup()
9 {
10 // Debug output at 9600 baud
11 Serial.begin(9600);
12
13 // Set up SD-card. Check your board for the pin to use
14 if (!SD.begin(4))
15 {
16 Serial.println("SD initialization failed!");
17 return;
18 }
19
20 // Configure high-speed SPI transfers
21 SPI.setClockDivider(4);
22
23 // 44100Khz mono files, 100 mSec of prebuffering.
24 Audio.begin(44100, 100);
25
26 // Configure pins
27 pinMode(buttonPin, INPUT);
28 pinMode(sensorPin, INPUT);
29 }
30
31 void loop()
32 {
33 // Wait for a button to be pressed
34
35 if (digitalRead(buttonPin))
36 {
37 // read the value from the sensor:
38 int sensorValue = analogRead(sensorPin);
39
40 Serial.print("Sensor reading: ");
41 Serial.print(sensorValue, DEC);
42
43 // Convert the temperature (3.3V on the Due)
44 int tempC = ( 3.3 * analogRead(sensorPin) * 100.0) / 1024.0;
45 Serial.print(" Temperature: ");
 
Search WWH ::




Custom Search