-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp36_sensor.ino
87 lines (69 loc) · 2.42 KB
/
tmp36_sensor.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
/****************************************************************
* File: TMP36_001 *
* Author: Shawn Kelley *
* Date: 11-FEB-2013 *
* Desc: Connect TMP36 to arduino via breadboard circuit *
* and write temperature in F / C to serial *
* Update: *
* 11-FEB-2013 *
* - write csv formated txt to file on SD card *
* 12-FEB-2013 *
* - added record_temp function *
* - added 'timer' var for delay value *
* - added removal of existing temps.csv to function *
* 13-FEB-2013 *
* - added datastream to cosm *
* 12-NOV-2014 *
* - removed networking and cosm *
****************************************************************/
/***** libs n stuff *****/
#include <SD.h>
/****** Globals *****/
File tFile;
const int chipSelect = 4;
const int sensorPin = 0;
unsigned long timer = 60000; //milliseconds
/***** setup stuff *****/
void setup()
{
Serial.begin(9600);
//SD card setup stuff
Serial.println("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(4))
{
Serial.println("initialization failed!");
Serial.println("did you remember to insert the SD card, dumbass???");
return;
}
Serial.println("IT IS AAALIVE!!!");
if (SD.exists("temps.csv")) {
SD.remove("temps.csv");
}
}
void loop()
{
int reading = analogRead(sensorPin);
float voltage = (((float)reading / 1024) * 5.0);
float temp_c = (voltage - 0.5) * 100 ;
float temp_f = (temp_c * 9.0 / 5.0) + 32.0;
//current temp
Serial.print("Current TEMP is ");
Serial.print(temp_c); Serial.print("C | ");
Serial.print(temp_f); Serial.println("F ");
record_temp(temp_c, temp_f);
delay(timer);
}
/***** functions *****/
void record_temp(float tempc, float tempf)
{
tFile = SD.open("temps.csv", FILE_WRITE);
if (tFile) {
tFile.print(tempc);
tFile.print(",");
tFile.println(tempf);
tFile.close();
} else {
Serial.println("error opening file");
}
}