-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtSwitch.js
More file actions
164 lines (140 loc) · 3.95 KB
/
tSwitch.js
File metadata and controls
164 lines (140 loc) · 3.95 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
var express = require('express');
var request = require('request');
var Tessel = require("tessel-io");
var five = require("johnny-five");
var board = new five.Board({
io: new Tessel()
});
board.on("ready", () => {
var blueButton = new five.Button("a1");
var redButton = new five.Button("a2");
var greenButton = new five.Button("a3");
var yellowButton = new five.Button("a4");
var sensor = new five.Sensor({
pin: "a5",
threshold: 2
});
var blueLed = new five.Led("b1");
var redLed = new five.Led("b2");
var greenLed = new five.Led("b3");
var yellowLed = new five.Led("b4");
var sensorLed = new five.Led("b5");
var hueURL = "http://192.168.1.3/api/OGIOGrBPRyn1AutXp3tLkZFIbMETdMaUCIBBkCL7";
//request({ url: url, method: 'PUT', json: {foo: "bar", woo: "car"}}, callback)
// Initiate blue light change
blueButton.on("press", () => blueLed.on() && lightOneOn() && blueLight() && lightOneBright());
blueButton.on("release", () => blueLed.off());
// Initiate red light change
redButton.on("press", () => redLed.on() && lightOneOn() && redLight() && lightOneBright());
redButton.on("release", () => redLed.off());
// Initiate green light change
greenButton.on("press", () => greenLed.on() && lightOneOn() && greenLight() && lightOneBright());
greenButton.on("release", () => greenLed.off());
// Initiate yellow light change
yellowButton.on("press", () => yellowLed.on() && lightOneOn() && yellowLight() && lightOneBright());
yellowButton.on("release", () => yellowLed.off());
//potentiometer dimmer
sensor.on("change", () => { dimmer() && hueDim()
//changes light state to on
function lightOneOn(){
request({
url: hueURL + "/lights/1/state",
method: 'PUT',
json: {"on": true}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
);
}
//turns brightness up light up to 210/256
function lightOneBright(){
request({
url: hueURL + "/lights/1/state",
method: 'PUT',
json: {"bri": 210}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
);
}
//defines scale on potentiometer for on board led
function dimmer(){
led.brightness(sensor.scaleTo(0, 255));
}
//defines scale on potentiometer for on light 1
function hueDim(){
request({
url: hueURL + "/lights/1/state",
method: 'PUT',
json: {"bri":sensor.scaleTo(0, 255)}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
);
}
// Puts a green light request
function greenLight(){
request({
url: hueURL + "/lights/1/state",
method: 'PUT',
json: {"hue":25653}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
);
}
// Puts a blue light request
function blueLight(){
request({
url: hueURL + "/lights/1/state",
method: 'PUT',
json: {"hue":44827}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
);
}
// Puts a red light request
function redLight(){
request({
url: hueURL + "/lights/1/state",
method: 'PUT',
json: {"hue":64928}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
);
}
// Puts a yellow light request
function yellowLight(){
request({
url: hueURL + "/lights/1/state",
method: 'PUT',
json: {"hue":13942}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
);
}
});