Hardware Reference
In-Depth Information
As with most shields, the I/O lines remain accessible. You can plug in the
cables straight on the Ethernet shield, and they will work in exactly the same way.
Use the code in Listing 12-1 to write the sketch.
Listing 12-1: Sketch (fi lename: Chapter12.ino )
1 #include <SD.h>
2 #include <SPI.h>
3 const int chipSelect = 4; // Change this as required
4
5 int light;
6 int lightPin = A3;
7 unsigned int iteration = 1;
8
9
10 void setup()
11 {
12 Serial.begin(9600);
13
14 Serial.print("Initializing SD card...");
15 // Chip Select pin needs to be set to output for the SD library
16 pinMode(10, OUTPUT);
17
18 // Attempt to initialize SD library
19 if (!SD.begin(chipSelect)) {
20 Serial.println("Card failed, or not present");
21 // don't do anything more:
22 return;
23 }
24 Serial.println("Card initialized.");
25 }
26
27 void loop()
28 {
29 // Get a light level reading
30 light = analogRead(lightPin);
31
32 // Open the SD data file
33 File dataFile = SD.open("light.txt", FILE_WRITE);
34
35 // Has the file been opened?
36 if (dataFile)
37 {
38 // Create a formatted string
39 String dataString = "";
40 dataString += String(iteration);
41 dataString += ",";
42 dataString += String(light);
43 dataString += ",";
44
Search WWH ::




Custom Search