-
Notifications
You must be signed in to change notification settings - Fork 0
/
J5TennisBall.js
158 lines (131 loc) · 4.56 KB
/
J5TennisBall.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* jha 10/26/2013
Objectives
- Get comfortable with javascript. time to move past CPR (copy, paste, refactor *tm*)
I am using visual studio over a samba mount to the 'pi :-)
- Control a 120VAC LED traffice light via relays for close quarters parking assistance in
garage using ping ultransonic sensor.
- Garage door opener
previously wrote windows phone app that controled Phidgets on a WCF service
- Current temperature in XML suitable for HomeAmation http://homeamation.azurewebsites.net/
garage door toast notification on garage door opening.
Hardware
1 Raspberry Pi
2 Parallax dual relay boards http://www.parallax.com/product/27114 (if using huge LEDs)
Beware current draw on micro processors even when using small LEDs.
1 ping sensor
Parallax ping http://www.parallax.com/product/28015
or
HC-SR04 or equilavent http://rodaw.me/HnPY7l
3 LEDs to taste. project will be easy to modify for other types of indicators. Perhaps 8 led bar graph
1 Arduino. I'm using a Duemilanove http://arduino.cc/en/Main/arduinoBoardDuemilanove
Note: I and others have reported difficulties using Leonardo and Leonardo clones.
1 12VDC power supply for relays
1 ~9VDC power supply for arduino (powering sensors)
I overloaded the USB supply after adding the ping after the relays.
*/
var j5 = require("johnny-five"),
board, button;
board = new j5.Board();
board.on("ready", function () {
var ping = new j5.Ping({
pin: 8,
freq: 250
});
button = new j5.Button(
{
pin: 7,
holdtime: 500,
invert: true
});
var ginches = 20;
button.on("hold", function () {
offset = ginches;
console.log("offset = " + offset);
});
function enableTrafficLights() {
redLight.on();
yellowLight.on();
greenLight.on();
} function disableTrafficLights() {
redLight.off();
yellowLight.off();
greenLight.off();
}
function initTrafficLights() {
// TODO might be better / more intuitive to use pin rather than Led
redLight = new j5.Led(3);
yellowLight = new j5.Led(2);
greenLight = new j5.Led(4);
}
//ping.on("data", function (err, value) {
// console.log("data", value);
//});
/*
main gaps loop
*/
var lowerLimit = 2;
var offset = 5;
ping.on("change", function (err, value) {
ginches = this.inches;
// flashing red
if (this.inches < (lowerLimit + offset)) {
console.log("flashing red");
redLight.strobe(200);
yellowLight.off();
greenLight.off();
//goto end
}
// solid red
else if (this.inches > lowerLimit + offset && this.inches < 6 + offset) {
console.log("red");
redLight.off().stop();
redLight.on();
yellowLight.off();
greenLight.off();
}
// red and yellow
else if (this.inches > 6 + offset && this.inches < 10 + offset) {
console.log("red and yellow");
redLight.off().stop();
redLight.on();
yellowLight.on();
greenLight.off();
}
// yellow
else if (this.inches > 10 + offset && this.inches < 20 + offset) {
console.log("yellow");
redLight.off();
yellowLight.on();
greenLight.off();
}
// green and yellow
else if (this.inches > 20 + offset && this.inches < 35 + offset) {
console.log("green and yellow");
greenLight.on();
yellowLight.on();
redLight.off();
}
// green
// if (this.inches > 35 + offset) {
else if (this.inches > 55 + offset) {
console.log("green");
redLight.off();
yellowLight.off();
greenLight.on();
}
// green
// off
// redLight.off();
// yellowLight.off();
// greenLight.off();
//end:
//console.log(typeof this.inches);
//console.log("Object is " + this.inches + "inches away");
});
initTrafficLights();
this.repl.inject({
redLight: redLight
});
console.log("You can interact with the RGB LED via the variable 'led' e.g. led.on();\n Hit control-D to exit.\n >> ");
console.log('try "on", "off", "toggle", "strobe", "stop" (stops strobing)');
});