1
2#include <SPI.h>
3#include <SD.h>
4
5
6 Enable button at pin 6
7const int Enable = 6;
8int Data_Counter = 0;
9
10
11 Input buttons at pin 5, 4, 3
12const int Input_1 = 5;
13const int Input_2 = 4;
14const
15 int Input_3 = 3;
16
17
18Sd2Card
19 card;
20SdVolume volume;
21SdFile root;
22
23const int chipSelect = 10;
24
25
26 Create a file to store the data
27File myFile;
28
29void setup() {
30
31 serial communications and wait for port to open:
32 Serial.begin(9600);
33 while
34 (!Serial) {
35 ;
36 only
37 }
38
39
40 pinMode(Enable, INPUT);
41 pinMode(Input_1,
42 INPUT);
43 pinMode(Input_2, INPUT);
44 pinMode(Input_3, INPUT);
45
46
47 setup for the SD card
48 Serial.println("Initializing SD card...");
49
50
51 if(!SD.begin(chipSelect)) {
52 Serial.println("initialization failed!");
53
54 return;
55 }
56 Serial.println("initialization done.");
57 Serial.println();
58
59
60
61 myFile = SD.open("DATA.csv", FILE_WRITE);
62
63
64 file opened ok, write to it:
65 if (myFile) {
66 Serial.println("File opened
67 ok");
68 Serial.println("Waiting for inputs");
69 Serial.println();
70
71
72 myFile.println("Entry,Input_1,Input_2,Input_3");
73
74 }
75 myFile.close();
76}
77
78void loop(void) {
79 if (digitalRead(Enable)
80 == 1) {
81
82 Serial.print("Reading
83 data entry ");
84 Serial.print(Data_Counter);
85 Serial.println(":");
86
87 Serial.print("Input_1 = ");
88 Serial.println(digitalRead(Input_1));
89
90 Serial.print("Input_2 = ");
91 Serial.println(digitalRead(Input_2));
92
93 Serial.print("Input_3 = ");
94 Serial.println(digitalRead(Input_3));
95
96 Serial.println(" ");
97
98 myFile = SD.open("DATA.csv",
99 FILE_WRITE);
100 if (myFile) {
101 myFile.print(Data_Counter);
102 myFile.print(",");
103
104 myFile.print(digitalRead(Input_1));
105 myFile.print(",");
106 myFile.print(digitalRead(Input_2));
107
108 myFile.print(",");
109 myFile.print(digitalRead(Input_3));
110 myFile.println(",");
111
112 }
113 myFile.close();
114 Serial.println("Data written to SD");
115
116 Serial.println();
117 Data_Counter++;
118 delay(5000);
119
120 }
121}