-
Notifications
You must be signed in to change notification settings - Fork 0
/
tropo.ino
210 lines (167 loc) · 5.32 KB
/
tropo.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <SoftwareSerial.h>
#include <SD.h>
#include <EEPROM.h>
#include<stdlib.h>
// Liquid Crystal Display
// Define the LCD pins: We'll be using a serial-based LCD display
// which only required +3.3Volts, GND, and a single data line.
// databuff and displaybuff hold the data to be displayed
#define LCDIn 2
#define LCDOut 5
SoftwareSerial mySerialPort(LCDIn, LCDOut);
// Data Buffers for the LCD
char databuff1[16];
char databuff2[16];
char dispbuff[16];
// GAS SENSORS
// Analog input pin that reads the first gas sensor
const int gasPin1 = A5;
// Analog input pin that reads the gas sensor
const int gasPin2 = A4;
// The digital pin that controls the heater of gas sensor 1
const int heaterPin1 = 7;
// The digital pin that controls the heater of gas sensor 2
const int heaterPin2 = 9;
// LED connected to digital pin 13
const int ledPin = 13;
// value read from the sensor A5
int gasVal1 = 0;
// value read from the sensor A4
int gasVal2 = 0;
long warmup = 180000; // enter time for heaters to warmup, in milliseconds.
// 180,000 milliseconds = 3 minutes
long downtime = 360000; // enter delay between readings, in milliseconds.
// 360,000 milli seconds = 6 minutes
//EEPROM records require two bytes to store a 1024 bit value.
//Each gas sensor returns a value from 0-1024, taking 2 bytes.
//To store gas sensor data would require a record index,
//plus two bytes for the first gas sensor, two bytes for the second gas sensor
//For a total of five bytes per record.
// current EEPROM address
int addr =0;
//EEPROM record number
int record = 0;
//EEPROM record length
int reclen = 5;
//switch to tell if an SD card is present
int SDPresent = 1;
void setup()
{
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(heaterPin1, OUTPUT); // sets the digital pins as output
pinMode(heaterPin2, OUTPUT);
pinMode(LCDOut, OUTPUT);
//reset the LCD
mySerialPort.begin(9600);
mySerialPort.write(0xFE);
mySerialPort.write(0x01);
sprintf(databuff1,"Wakeup Test");
sprintf(dispbuff,"%-16s",databuff1);
mySerialPort.print(dispbuff);
// Set up SD card, let us know if SD card is absent
pinMode(10, OUTPUT);
if (!SD.begin(4))
{
SDPresent =0;
sprintf(databuff2,"NO SD CARD!!!");
sprintf(dispbuff,"%-16s",databuff2);
mySerialPort.print(dispbuff);
Serial.println("NO SD CARD!!!");
delay(6000);
}
delay(3333);
}
void loop()
{
long scratch=0; // scratch variable
// set the timer
unsigned long counter = millis();
//turn first heater on
digitalWrite(heaterPin1, HIGH);
// wait 3 minutes for heater to heat up
while(millis() < (counter + warmup))
{
sprintf(databuff1,"Unit1 Activated");
sprintf(dispbuff,"%-16s",databuff1);
mySerialPort.print(dispbuff);
scratch = (int)((counter+warmup - millis())/1000);
sprintf(databuff2,"Countdown: %3d", scratch);
sprintf(dispbuff,"%-16s",databuff2);
mySerialPort.print(dispbuff);
Serial.println(scratch);
}
// read the analog in value:
gasVal1 = analogRead(gasPin1);
sprintf(databuff1,"read unit 1");
sprintf(dispbuff,"%-16s",databuff1);
mySerialPort.print(dispbuff);
// shut off the first heater
digitalWrite(heaterPin1, LOW);
//turn second heater on
digitalWrite(heaterPin2, HIGH);
sprintf(databuff2,"turning on unit2");
sprintf(dispbuff,"%-16s",databuff2);
mySerialPort.print(dispbuff);
// wait 3 minutes for heater to heat up
while(millis() < (counter + warmup + warmup))
{
sprintf(databuff1,"Unit2 Activated");
sprintf(dispbuff,"%-16s",databuff1);
mySerialPort.print(dispbuff);
scratch = (int)((counter+warmup+warmup - millis())/1000);
sprintf(databuff2,"Countdown: %3d", scratch);
sprintf(dispbuff,"%-16s",databuff2);
mySerialPort.print(dispbuff);
Serial.println(scratch);
}
// read the analog in value:
gasVal2 = analogRead(gasPin2);
sprintf(databuff2,"reading unit2");
sprintf(dispbuff,"%-16s",databuff2);
mySerialPort.print(dispbuff);
// shut off the second heater
digitalWrite(heaterPin2, LOW);
//Display on LCD
sprintf(databuff1,"Gas1:%4d",gasVal1);
sprintf(dispbuff,"%-16s",databuff1);
mySerialPort.print(dispbuff);
sprintf(databuff2,"Gas2:%4d",gasVal2);
sprintf(dispbuff,"%-16s",databuff2);
mySerialPort.print(dispbuff);
//write to SD card
if(SDPresent = 1)
{
writeDataToSD(databuff1, databuff2);
}
//Wait downtime and start again
//to make more frequent measurements, change value of downtime
while(millis() < (counter +downtime))
{
}
}
void writeDataToSD(String dataString1, String dataString2)
{
// 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)
{
Serial.println("Hooray, we have a file!");
dataFile.print(millis());
dataFile.print(",");
dataFile.print(dataString1);
dataFile.print(",");
dataFile.println(dataString2);
dataFile.close();
// print to the serial port too:
Serial.print(millis());
Serial.print(",");
Serial.print(dataString1);
Serial.print(",");
Serial.println(dataString2);
//Print to LCD
mySerialPort.print("Datafile written");
}
}