-
Notifications
You must be signed in to change notification settings - Fork 0
/
MethaneArduino.ino
91 lines (70 loc) · 1.7 KB
/
MethaneArduino.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <SPI.h>
#include <SD.h>
/*
* SD card attached as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
*
*/
File myFile;
int times;
String incomingByte;
int input = 0;
int nocard = 0;
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
nocard = 1;
return;
}
else{
Serial.println("initialization done.");
nocard = 0;
}
myFile = SD.open("sensor.txt", FILE_WRITE);
// Open the file for writing!
if (myFile) {
myFile.println("time,sensor");
//myFile.close();
Serial.println("First line done!");
} else {
// if the file didn't open, print an error:
Serial.println("error opening .txt");
}
times = 0;
}
void loop() {
int analogSensor = analogRead(0);
//Serial.print("Pin A0: ");
Serial.print(times);
Serial.print(": ");
if (nocard==1){
Serial.print(analogSensor);
Serial.println(" -> No SD-card !!!");
}
else{
Serial.println(analogSensor);
}
myFile.print(times);
myFile.print(",");
myFile.println(analogSensor);
incomingByte = (Serial.read());
//input = int(incomingByte);
//Serial.println (incomingByte);
if (incomingByte == "113"){//"type q for quit - exit/finish the writing to the file
myFile.close();
Serial.println("Writing compleated...");
}
else if (incomingByte == "100"){// "type d for delete
myFile.close();
SD.remove("test.txt");
SD.remove("sensor.txt");
Serial.println("File removed...");
}
delay(1000);
times=times+1;
}