-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFull_Functionality.ino
More file actions
348 lines (307 loc) · 7.92 KB
/
Full_Functionality.ino
File metadata and controls
348 lines (307 loc) · 7.92 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/***
File: Full_Functionality.ino
Author: Rahim Aziz
Date: May 21, 2024
Description: Arduino script for reading and writing data to EEPROM, including temperature and humidity readings from a DHT11 sensor.
***/
// ===============================
// Libraries
// ===============================
#include <EEPROM.h>
#include <DHT11.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <Narcoleptic.h>
// ===============================
// Global Variables
// ===============================
struct DHT11DataObject {
int temp;
int humd;
int index;
};
#define LED 13
#define DEBUGMODE 1
// Create an instance of the DHT11 class.
// - For Arduino: Connect the sensor to Digital I/O Pin 2.
DHT11 dht11(2);
int writeTempAddress = 0; //Location we want the data to be put.
int writeAddress = 0; //Address for general EEPROM writing
// ===============================
// Setup Function
// ===============================
void setup() {
Serial.begin(9600);
// Uncomment the line below to set a custom delay between sensor readings (in milliseconds).
// dht11.setDelay(500); // Set this to the desired delay. Default is 500ms.
// Low power mode setup
pinMode(LED,OUTPUT);
digitalWrite(LED,LOW);
// slow clock to divide by 256
clock_prescale_set (clock_div_1);
// turn off brown-out enable in software
MCUCR = bit (BODS) | bit (BODSE);
MCUCR = bit (BODS);
disableAllPeriph();
// Another tweaks to lower the power consumption
ADCSRA &= ~(1<<ADEN); //Disable ADC
ACSR = (1<<ACD); //Disable the analog comparator
// ATmega48/88/168/328
DIDR0 = B00111111; //Disable digital input buffers on all ADC0-ADC5 pins
DIDR1 = (1<<AIN1D)|(1<<AIN0D); //Disable digital input buffer on AIN1/0
if (DEBUGMODE)
{
Narcoleptic.enableSerial();
}
}
// ===============================
// Main Loop
// ===============================
void loop() {
if(DEBUGMODE)
{
debugMode();
}
else
{
Narcoleptic.delay(10000); //uncalibrated delay for 10s
Narcoleptic.enableSerial();
readTEMP();
//writeTEMP();
Serial.flush();
Narcoleptic.disableSerial();
}
}
/**
* Reads the data of size type struct DHT11DataObject from the EEPROM at the users desired address
*
* @param 'N/A'
* @return N/A
* @throws N/A
*
*/
void getEEPROM()
{
Serial.flush();
int readAddress = 0;
Serial.println("Enter desired read address: ");
while (!Serial.available()); // Wait until data is available
readAddress = Serial.parseInt();
Serial.println("Getting EEPROM address: " + String(readAddress));
// Check if the entered address is within the valid range
if (readAddress >= 0 && readAddress < EEPROM.length()) {
// Read the data from EEPROM at the specified address
DHT11DataObject customReadVar;
EEPROM.get(readAddress, customReadVar);
Serial.println("Temp: " + String(customReadVar.temp));
Serial.println("Humid: " + String(customReadVar.humd));
Serial.println("Index: " + String(customReadVar.index));
} else {
Serial.println("Invalid EEPROM address. Please enter a value between 0 and 1023.");
}
}
/**
* Write the data of size type struct DHT11DataObject to the EEPROM at the users desired address
*
* @param 'N/A'
* @return N/A
* @throws N/A
*
*/
void putEEPROM()
{
Serial.flush();
Serial.println("Enter EEPROM address (0-1023) to write: ");
while (!Serial.available()); // Wait until data is available
writeAddress = Serial.parseInt(); // Read the integer from Serial input
// Check if the entered address is within the valid range
if (writeAddress >= 0 && writeAddress < EEPROM.length()) {
DHT11DataObject customVar = {
21,
33,
1
};
Serial.println("Putting data in EEPROM address: " + String(writeAddress));
EEPROM.put(writeAddress, customVar);
writeAddress += sizeof(DHT11DataObject);
} else {
Serial.println("Invalid EEPROM address. Please enter a value between 0 and 1023.");
}
}
/**
* Resets all of the data in the EEPROM to 0
*
* @param 'N/A'
* @return N/A
* @throws N/A
*
*/
void clearEEPROM()
{
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 0);
}
}
/**
* Reads environmental data from the DHT11 and stores it in the EEPROM
*
* @param 'N/A'
* @return N/A
* @throws N/A
*
*/
void writeTEMP()
{
int temperature = 0;
int humidity = 0;
// Attempt to read the temperature and humidity values from the DHT11 sensor.
int result = dht11.readTemperatureHumidity(temperature, humidity);
// Check the results of the readings.
if (result == 0) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C\tHumidity: ");
Serial.print(humidity);
Serial.println(" %");
DHT11DataObject tempReading = {temperature, humidity, writeTempAddress};
// Check if the entered address is within the valid range
if (writeTempAddress >= 0 && writeTempAddress < EEPROM.length()) {
Serial.println("Writing temperature data to address: " + String(writeTempAddress));
EEPROM.put(writeTempAddress, tempReading);
writeTempAddress += sizeof(DHT11DataObject);
}
else
{
Serial.println("EEPROM out of range!");
}
}
else
{
Serial.println(DHT11::getErrorString(result));
}
}
/**
* Reads environmental data from the DHT11
*
* @param 'N/A'
* @return N/A
* @throws N/A
*
*/
void readTEMP()
{
int temperature = 0;
int humidity = 0;
// Attempt to read the temperature and humidity values from the DHT11 sensor.
int result = dht11.readTemperatureHumidity(temperature, humidity);
// Check the results of the readings.
if (result == 0) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C\tHumidity: ");
Serial.print(humidity);
Serial.println(" %");
} else {
Serial.println(DHT11::getErrorString(result));
}
}
/**
* Utilizes the Narcoleptic library to disable all peripherals
*
* @param 'N/A'
* @return N/A
* @throws N/A
*
*/
void disableAllPeriph()
{
Narcoleptic.disableWire();
Narcoleptic.disableTimer1();
Narcoleptic.disableTimer2();
Narcoleptic.disableTimer3();
Narcoleptic.disableTimer4();
Narcoleptic.disableSerial();
Narcoleptic.disableADC();
Narcoleptic.disableSPI();
Narcoleptic.disableTouch();
}
/**
* Utilizes the Narcoleptic library to enable all peripherals
*
* @param 'N/A'
* @return N/A
* @throws N/A
*
*/
void enableAllPeriph()
{
Narcoleptic.enableWire();
Narcoleptic.enableTimer1();
Narcoleptic.enableTimer2();
Narcoleptic.enableTimer3();
Narcoleptic.enableTimer4();
Narcoleptic.enableSerial();
Narcoleptic.enableADC();
Narcoleptic.enableSPI();
Narcoleptic.enableTouch();
}
void readAll()
{
Serial.println("Temperature,Humidity,Index");
for (int address = 0; address < EEPROM.length(); address += sizeof(DHT11DataObject)) {
DHT11DataObject data;
EEPROM.get(address, data);
// Check if the data is valid (non-zero)
if (data.temp != 0 || data.humd != 0 || data.index != 0) {
Serial.print(data.temp);
Serial.print(",");
Serial.print(data.humd);
Serial.print(",");
Serial.println(data.index);
} else {
// Stop reading if we encounter all zeros (assuming it's unused EEPROM)
break;
}
}
Serial.println("EEPROM data export complete");
}
/**
* Takes a keyboard input and runs the appropriate function.
* This function is primarily used for debugging
*
* @param 'N/A'
* @return N/A
* @throws N/A
*
*/
void debugMode()
{
if (Serial.available() > 0)
{
switch (Serial.read())
{
case 'w':
putEEPROM();
break;
case 'r':
getEEPROM();
break;
case 'c':
clearEEPROM();
break;
case 'a':
readTEMP();
break;
case 't':
writeTEMP();
break;
case 'q':
readAll();
break;
default:
Serial.println("Not valid input!");
break;
}
}
}