-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultrasonic-proximity-leds.js
144 lines (117 loc) · 3.41 KB
/
ultrasonic-proximity-leds.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
139
140
141
142
143
var five = require("johnny-five");
var firebase = require('firebase');
var board = new five.Board();
board.on("ready", function() {
// Set up variables
// RGB LED
var rgb = new five.Led.RGB([5,6,4]);
/*** LEDS RGB ***/
//var leds = new five.Leds([3, 5, 6]);
//var led = new five.Led(6);
/*** Proximity Sensors ***/
var proximity = new five.Proximity({
controller: "HCSR04",
pin: 2,
freq: "700"
});
/*** Firebase Setup Config ***/
firebase.initializeApp({
apiKey: "AIzaSyCnCCmWUR9vVDSMxmlcbuEOKGV_jekKEQw",
authDomain: "iot-arduino-827cb.firebaseapp.com",
databaseURL: "https://iot-arduino-827cb.firebaseio.com",
projectId: "iot-arduino-827cb",
storageBucket: "iot-arduino-827cb.appspot.com",
messagingSenderId: "971124207623",
//provider: "anonymous",
//uid: "41a455a7-388c-4c17-912f-07c5899d4b2a"
});
// RGB LED
var ref = firebase.database().ref('color');
var nref = firebase.database().ref('person');
// Firebase RGB LEDS array references
var ledRed = firebase.database().ref('leds/red');
var ledGreen = firebase.database().ref('leds/green');
var ledBlue = firebase.database().ref('leds/blue');
// LEDS
// Pulse all leds in the object.
//led.on();
//led.pulse();
//leds.pulse();
//leds.off();
ledGreen.on('value', function(snapshot) {
if(snapshot.val() == 1) {
//leds[0].on();
} else
if (snapshot.val() == 0){
//leds[0].off();
}
});
ledBlue.on('value', function(snapshot) {
if(snapshot.val() == 1) {
//leds[1].on();
} else
if (snapshot.val() == 0){
//leds[1].off();
}
});
ledRed.on('value', function(snapshot) {
if(snapshot.val() == 1) {
//leds[2].on();
} else
if (snapshot.val() == 0){
//leds[2].off();
}
});
//ledRed.set('off');
//ledGreen.set('off');
//ledBlue.set('off');
//var servo = new five.Servo(2);
//Add servo to REPL (optional)
//this.repl.inject({
//servo: servo,
//range: [ 0, 180 ]
//});
//servo.to(90, 500, 10);
// Set horn to 45°
//servo.min();
//servo.sweep();
var ledRef = ref;
ledRef.on('value', function(snapshot) {
if(snapshot.val() == 'red') {
rgb.color("FF0000");
} else if (snapshot.val() =='blue'){
rgb.color("FFFF00");
}
});
proximity.on("data", function() {
var cmtr = this.cm;
var inch = this.in;
var excm = Math.floor(cmtr);
var exin = Math.floor(inch);
console.log("Proximity: ");
console.log(" cm : ", cmtr + ' ('+excm+')');
console.log(" in : ", inch + ' ('+exin+')');
console.log("-----------------");
});
proximity.on("change", function() {
var cmtr = this.cm;
var inch = this.in;
var exct = Math.floor(cmtr);
if (exct < 20) {
ref.set('red');
//rgb.color("FF0000");
setTimeout(function() {
nref.push(
{
'type':'leds',
'message':'Hi, someone turn the led to red!',
'timestamp':firebase.database.ServerValue.TIMESTAMP
});
},800);
} else {
ref.set('blue');
rgb.color("FFFF00");
}
//console.log(exct +" The obstruction has moved.");
});
});