-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolarLog.ino
More file actions
114 lines (87 loc) · 2.01 KB
/
SolarLog.ino
File metadata and controls
114 lines (87 loc) · 2.01 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <SD.h>
#include <stdlib.h>
const int CHIP_SELECT_PIN = 10;
const int STATUS_LED = 9;
const bool DEBUG = true;
int counter;
float CAPTURE_INTERVAL; //minutes
File dataFile;
char *filename = "data_0.csv";
bool error = false;
void setup()
{
pinMode(STATUS_LED, OUTPUT);
digitalWrite(STATUS_LED, LOW);
if (DEBUG)
{
CAPTURE_INTERVAL = 0.02;
Serial.begin(9600);
}
else
CAPTURE_INTERVAL = 10.0;
debug("Initializing SD card");
pinMode(CHIP_SELECT_PIN, OUTPUT);
//init SD card
if (!SD.begin(CHIP_SELECT_PIN))
{
debug("Card failed, or not present");
while(1){
//stop further execution
blinkLED();
};
}
// ensure unique sequential name
int nameIndex = 0;
while(SD.exists(filename)){
nameIndex++;
String name = "data_" + String(nameIndex) + ".csv";
name.toCharArray(filename, name.length()+1);
}
debug(filename);
debug("Card initialized.");
log("Time,Voltage");
// blink 3 times to see it started
blinkLED();
blinkLED();
blinkLED();
}
void debug(String msg)
{
if (DEBUG)
Serial.println(msg);
}
void log(String msg)
{
// open data file
dataFile = SD.open(filename, FILE_WRITE);
if (!dataFile)
{
debug("Error opening data file");
while(1){
//stop further execution
blinkLED();
}
}
dataFile.println(msg);
dataFile.close();
debug(msg);
}
void blinkLED()
{
digitalWrite(STATUS_LED, HIGH);
delay(100);
digitalWrite(STATUS_LED, LOW);
delay(100);
}
void loop()
{
// read voltage and multiply by voltage divider factor
float voltage= analogRead(A0) * (5.0 / 1023.0) * 1.22;
// convert float voltage to a string
char voltage_s[5];
dtostrf(voltage, 4, 2, voltage_s);
String entry = String(counter) + "," + voltage_s;
log(entry);
counter++;
delay(CAPTURE_INTERVAL * 60000);
}