Line 3... |
Line 3... |
3 |
|
3 |
|
4 |
This example shows how to log data from three analog sensors |
4 |
This example shows how to log data from three analog sensors |
5 |
to an SD card using the SD library. |
5 |
to an SD card using the SD library. |
6 |
|
6 |
|
7 |
The circuit: |
7 |
The circuit: |
8 |
* analog sensors on analog ins 0, 1, and 2 |
8 |
* analog sensors on analog ins PC0, PC1, and PC2 |
9 |
* SD card attached to SPI bus as follows: |
9 |
* SD card attached to SPI bus as follows: |
10 |
** MOSI - pin 11 |
10 |
** MOSI - PB3 |
11 |
** MISO - pin 12 |
11 |
** MISO - PB4 |
12 |
** CLK - pin 13 |
12 |
** CLK - PB5 |
13 |
** CS - pin 4 |
13 |
** CS - PD4 |
14 |
|
14 |
|
15 |
created 24 Nov 2010 |
15 |
created 24 Nov 2010 |
16 |
updated 2 Dec 2010 |
16 |
updated 2 Dec 2010 |
17 |
by Tom Igoe |
17 |
by Tom Igoe |
18 |
|
18 |
|
19 |
This example code is in the public domain. |
19 |
This example code is in the public domain. |
20 |
|
20 |
|
21 |
*/ |
21 |
*/ |
22 |
|
22 |
|
23 |
#include <SD.h> |
23 |
#include <SD.h> |
24 |
|
- |
|
25 |
// On the Ethernet Shield, CS is pin 4. Note that even if it's not |
- |
|
26 |
// used as the CS pin, the hardware CS pin (10 on most Arduino boards, |
- |
|
27 |
// 53 on the Mega) must be left as an output or the SD library |
- |
|
28 |
// functions will not work. |
- |
|
29 |
const int chipSelect = 4; |
24 |
const int chipSelect = 4; |
30 |
|
25 |
|
31 |
void setup() |
26 |
void setup() |
32 |
{ |
27 |
{ |
33 |
Serial.begin(9600); |
28 |
Serial.begin(9600); |
Line 43... |
Line 38... |
43 |
return; |
38 |
return; |
44 |
} |
39 |
} |
45 |
Serial.println("card initialized."); |
40 |
Serial.println("card initialized."); |
46 |
} |
41 |
} |
47 |
|
42 |
|
- |
|
43 |
int count; |
- |
|
44 |
|
48 |
void loop() |
45 |
void loop() |
49 |
{ |
46 |
{ |
- |
|
47 |
|
50 |
// make a string for assembling the data to log: |
48 |
// make a string for assembling the data to log: |
51 |
String dataString = ""; |
49 |
String dataString = "$"; |
- |
|
50 |
delay(100); |
- |
|
51 |
dataString += String(count); // print number of measurement |
- |
|
52 |
dataString += ","; |
- |
|
53 |
|
52 |
|
54 |
|
53 |
// read three sensors and append to the string: |
55 |
// read three sensors and append to the string: |
54 |
for (int analogPin = 0; analogPin < 3; analogPin++) { |
56 |
for (int analogPin = 0; analogPin < 3; analogPin++) { |
55 |
int sensor = analogRead(analogPin); |
57 |
int sensor = analogRead(analogPin); |
56 |
dataString += String(sensor); |
58 |
dataString += String(sensor); |
Line 67... |
Line 69... |
67 |
if (dataFile) { |
69 |
if (dataFile) { |
68 |
dataFile.println(dataString); |
70 |
dataFile.println(dataString); |
69 |
dataFile.close(); |
71 |
dataFile.close(); |
70 |
// print to the serial port too: |
72 |
// print to the serial port too: |
71 |
Serial.println(dataString); |
73 |
Serial.println(dataString); |
- |
|
74 |
count++; |
72 |
} |
75 |
} |
73 |
// if the file isn't open, pop up an error: |
76 |
// if the file isn't open, pop up an error: |
74 |
else { |
77 |
else { |
75 |
Serial.println("error opening datalog.txt"); |
78 |
Serial.println("error opening datalog.txt"); |
76 |
} |
79 |
} |
77 |
} |
- |
|
78 |
|
80 |
} |
- |
|
81 |
|