-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolar_arduino.ino
More file actions
131 lines (94 loc) · 3.69 KB
/
Copy pathSolar_arduino.ino
File metadata and controls
131 lines (94 loc) · 3.69 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* SOLAR OVEN CODE
Date:29/3/2020 Update #1: Adding Sensors :completed
Bluetooth Config :completed note: keep delay rate of arduino code less than timer refresh rate of app
SD Card :completed
RTC module connect :completed
*/
/* header files */
#include <SPI.h> //SD card
#include <SD.h> //SD card
#include <Wire.h> //RTC
#include <RTClib.h>
RTC_DS3231 rtc;
DateTime now;
int prim=0,aux1=0,aux2=0,aux3=0; //sensor variables declaration
const int chipSelect = 4; //SD.CS pin on D4
int count=0;
void setup() { // put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
Serial.print("Initializing SD card..."); //SD card Check
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
if (rtc.begin())
Serial.println("RTC connected.");
else if (! rtc.begin())
{
Serial.println("Couldn't find RTC Module");
while (1);
}
if (rtc.lostPower())
{
Serial.println("RTC lost power, lets set the time!");
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//rtc.adjust(DateTime(2020, 1, 28, 21,14 , 0));
}
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop() { // put your main code here, to run repeatedly:
/* Read values from sensors A0 to A3*/
prim=(5.0 * analogRead(A0) *100.0)/1024;
aux1=(5.0 * analogRead(A1) *100.0)/1024;
aux2=(5.0 * analogRead(A2) *100.0)/1024;
aux3=(5.0 * analogRead(A3) *100.0)/1024;
Serial.println(String(prim)+"|"+String(aux1)+"|"+String(aux2)+"|"+String(aux3)+"|"); //sending via bluetooth and serial monitor
//------------------------- bluetooth upload completed ... sd card storage started -----------------------------------------
if(count>=4) //stores valuesa fter 20secs... increase count to suit time ... 1 = 5sec , 4= 20sec
{
String dataString = "";
now = rtc.now();
dataString = String(now.day());
dataString += "-";
dataString += String(now.month());
dataString += "-";
dataString += String(now.year());
dataString += ",";
dataString += String(now.hour());
dataString += ":";
dataString += String(now.minute());
dataString += ":";
dataString += String(now.second());
dataString += ",";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 4; analogPin++) {
int sensor = (5.0 * analogRead(analogPin)*100.0)/1024;
dataString += String(sensor);
if (analogPin < 3) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
count=0;
// --------------------------- SD card save completed ----------------------------------------
}
delay(5000);
count++;
}