-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay.js
138 lines (138 loc) · 4.92 KB
/
relay.js
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
const matrix = require("@matrix-io/matrix-lite");
var methods = {}; //methods object
var relayPin1 = 0;
var relayPin2 = 1;
//initialize the global variables//
var currentTemperature = 20; //store the current temperature
//create the offset temperatures (usually 1 degree above and below the desired temperature)
var temperatureOffset1 = 0;
var temperatureOffset2 = 0;
//create variables that hold the humidity and pressure temperature sensor readings
var humidityTemperature = 0;
var pressureTemperature = 0;
var waitingToggle = false;
var lowestTemperature = 10; //holds the lowest temperature wanted (be mindful of farenheit /celsius conversions)
var highestTemperature = 30; //holds the highest temperature wanted (be mindful of farenheit /celsius conversions)
var list = ['blue'];
matrix.gpio.setFunction(relayPin1, "DIGITAL");
matrix.gpio.setFunction(relayPin2, "DIGITAL");
matrix.gpio.setMode(relayPin1, "output");
matrix.gpio.setMode(relayPin2, "output");
matrix.gpio.setDigital(relayPin1, "ON");
matrix.gpio.setDigital(relayPin2, "ON");
setInterval(function(){
humidityTemperature = matrix.humidity.read().temperature;
pressureTemperature = matrix.pressure.read().temperature;
currentTemperature = (humidityTemperature + pressureTemperature)/2 - 13;
//console.log(currentTemperature);
},50);
methods.sleep = function(milliseconds) {
var start = new Date().getTime();
while ((new Date().getTime() - start) < milliseconds){
}
}
setInterval(function(){
if (!waitingToggle) {
matrix.led.set({});
list = ['blue'];
};
// Creates pulsing LED effect
if (waitingToggle) {
if(list.length < 35){
for (var i = 0; i < 35; ++i) {
// Set individual LED value
matrix.led.set(list);
methods.sleep(10);
list.push('blue');
}
}
}
},50);
methods.startWaiting = function() {
waitingToggle = true;
};
methods.stopWaiting = function() {
waitingToggle = false;
};
methods.makeTemp = function(temp1, temp2){
//set the first and second temperature offsets to a degree greater and less than the desired temperature
temperatureOffset1 = temp1;
temperatureOffset2 = temp2;
if(currentTemperature < temperatureOffset1)
{
//set the first relay on and turn off the other
matrix.gpio.setDigital(relayPin1, "OFF");
matrix.gpio.setDigital(relayPin2, "ON");
//log the current temperature and the offsets
console.log(currentTemperature);
console.log(temperatureOffset1);
console.log(temperatureOffset2);
//wait a second and then call the makeTemp function again to check the temperature
setTimeout(function(){
methods.makeTemp(temperatureOffset1,temperatureOffset2);
}, 1000);
}
else if(currentTemperature > temperatureOffset2)
{
//set the second relay on and turn off the other
matrix.gpio.setDigital(relayPin1, "ON");
matrix.gpio.setDigital(relayPin2, "OFF");
//log the current temperature and the offsets
console.log(currentTemperature);
console.log(temperatureOffset1);
console.log(temperatureOffset2);
//wait a second and then call the makeTemp function again to check the temperature
setTimeout(function(){
methods.makeTemp(temperatureOffset1,temperatureOffset2);
}, 1000);
}
else{
//log the current temperature
console.log(currentTemperature);
//turn off both relays
matrix.gpio.setDigital(relayPin1, "ON");
matrix.gpio.setDigital(relayPin2, "ON");
//after a second check the temperature again for any changes
setTimeout(function(){
methods.makeTemp(temperatureOffset1,temperatureOffset2);
}, 1000);
}
};
methods.currentTemperature = function(){
return currentTemperature;
}
methods.tellTemp = function(){
var list = ["blue"];
for (var i = 0; i < 35; ++i){
if((currentTemperature <= highestTemperature && currentTemperature >= lowestTemperature))
{
if(i <= ((currentTemperature-lowestTemperature)*(35/(highestTemperature-lowestTemperature)))) //represent temperature through leds
{
matrix.led.set(list);
list.push({
r: (255)/(Math.exp(-0.3*i+5)+1) - 1,
g: 0,
b: -255/(Math.exp(-0.3*i +5) +1) +255,
w: 0
});
}
else{
//make the rest of the leds blank
matrix.led.set(list);
list.push({});
}
}
}
if(currentTemperature > highestTemperature)
{
//change all leds to red
matrix.led.set("red");
}
else if(currentTemperature < lowestTemperature)
{
//change all leds to blue
matrix.led.set("blue");
}
};
module.exports = methods;
// Export methods in order to make them avaialble to other files