-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTempHumSensor.ino
More file actions
137 lines (107 loc) · 2.78 KB
/
TempHumSensor.ino
File metadata and controls
137 lines (107 loc) · 2.78 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
#include "DHT.h"
#include "Math.h"
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <AccelStepper.h>
#define STEPS 600
#define DHTTYPE DHT22
#define DHTPIN 5
#define SPEED 60.0
#define HOME -100
#define CALLIBRATION_MODE false
#define ACTION_MS 2000
#define FACTOR 4.4
DHT dht(DHTPIN, DHTTYPE);
int lastOutputTemp=0;
int lastOutputHumd=0;
Adafruit_MotorShield afms = Adafruit_MotorShield();
Adafruit_StepperMotor *tempMotor = afms.getStepper(600, 1);
Adafruit_StepperMotor *humdMotor = afms.getStepper(600, 2);
void hForwardstep1() {
humdMotor->onestep(FORWARD, SINGLE);
}
void hBackwardstep1() {
humdMotor->onestep(BACKWARD, SINGLE);
}
void tForwardstep1() {
tempMotor->onestep(FORWARD, SINGLE);
}
void tBackwardstep1() {
tempMotor->onestep(BACKWARD, SINGLE);
}
AccelStepper hStepper(hForwardstep1, hBackwardstep1);
AccelStepper tStepper(tForwardstep1, tBackwardstep1);
bool initialize = true;
void setup() {
Serial.begin(9600);
afms.begin();
dht.begin();
tStepper.setMaxSpeed(SPEED);
tStepper.setAcceleration(SPEED);
hStepper.setMaxSpeed(SPEED);
hStepper.setAcceleration(SPEED);
// Move to an initial position
tStepper.move(HOME);
hStepper.move(HOME);
}
long lastTempCheck = 0;
void moveDials(float temp, float humidity) {
int toMoveF = roundf(humidity*FACTOR);
int toMoveH = roundf(temp*FACTOR);
tStepper.moveTo(toMoveF);
hStepper.moveTo(toMoveH);
Serial.print("Temperature: ");
Serial.println(toMoveF);
Serial.println(temp);
Serial.print("Humidity: ");
Serial.println(toMoveH);
Serial.println(humidity);
}
void checkTempFromSensorsAndSetDials() {
if( (millis() - lastTempCheck) > 2000) {
float h = dht.readHumidity();
float f = dht.readTemperature(true);
lastTempCheck = millis();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
} else {
moveDials(f,h);
}
}
}
int callibrationVal = 0;
void callibrate() {
moveDials(callibrationVal, callibrationVal);
if(callibrationVal < 100) {
callibrationVal += 10;
} else {
callibrationVal = 0;
}
}
void performAction() {
if(CALLIBRATION_MODE) {
callibrate();
} else {
checkTempFromSensorsAndSetDials();
}
}
void loop() {
if(hStepper.distanceToGo() != 0 || tStepper.distanceToGo() != 0) {
// If we're currently moving, carry on
} else {
if(initialize) {
// If we were initializing, we've found '0'
initialize = false;
tStepper.setCurrentPosition(0);
hStepper.setCurrentPosition(0);
}
if( (millis() - lastTempCheck) > ACTION_MS) {
performAction();
lastTempCheck = millis();
}
}
tStepper.run();
hStepper.run();
}