-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoasterBrain.js
More file actions
191 lines (170 loc) · 5.12 KB
/
coasterBrain.js
File metadata and controls
191 lines (170 loc) · 5.12 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var admin = require("firebase-admin");
var serviceAccount = require("serviceKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://testing-cf658.firebaseio.com"
});
var db = admin.database();
//here is the variables linked to the database
var motorState = db.ref().child('motorOn');
var coasterBack = db.ref().child('coasterBack');
var coasterEstop = db.ref().child('coasterEstop');
var coasterMode = db.ref().child('coasterMode');
var lightStatus = db.ref().child('lightStatus');
var loopCount = db.ref().child('loopCount');
var motorState = db.ref().child('motorOn');
var recordTime = db.ref().child('recordTime');
var timeDeparted = db.ref().child('timeDeparted');
var timeElapsed = db.ref().child('timeElapsed');
var timeReturned = db.ref().child('timeReturned');
var totalLoops = db.ref().child('totalLoops');
// Database setup is taken care of above
var five = require('johnny-five');
var board = new five.Board();
//check the coaster light status
board.on('ready', function(){
//outputs
var rideLights = new five.Led(12);//ride lights white
var greenLight = new five.Led(7);//green
var yellowLight = new five.Led(6);//yellow
var redLight = new five.Led(5);//red
var motor = new five.Led(13);
// inputs
sendButton = new five.Button(2);
stopButton = new five.Button(3);
lightButton = new five.Button(4);
carBack = new five.Button(8);
//listening to firebase database data
lightStatus.on('value',function(snap){
//console.log("The lights are on? " + snap.val());
if(snap.val()===true){
rideLights.off();
}
else{
rideLights.on();
}
});
motorState.on('value',function(state){
if(state.val()===true){
motor.on();
}
else{
motor.off();
}
});
coasterMode.on('value',function(getMode){
if(getMode.val()=="Single loop"){
greenLight.off();
yellowLight.on();
}
if(getMode.val()=="Continuous"){
greenLight.off();
yellowLight.on();
}
if(getMode.val()=="Loop for"){
greenLight.off();
yellowLight.on();
}
if(getMode.val()=="EMERGENCY STOP"){
greenLight.off();
yellowLight.off();
redLight.on();
motor.off();
}
if(getMode.val()=="System Ready"){
greenLight.on();
yellowLight.off();
redLight.off();
motor.off();
}
})//end of coasterMode();
//input listeners Arduino --------------------------------------------------
sendButton.on("down",function(){
//console.log("The send button was pressed");
coasterEstop.once('value',function(getData){
if(getData.val()===false){
coasterMode.set("Single loop");
sendCoaster();
}
});
});
stopButton.on("down",function(){
//console.log("The stop button was pressed");
coasterEstop.once('value',function(getData){
if(getData.val()===false){
coasterEstop.set(true);
coasterMode.set("EMERGENCY STOP");
motor.off();
}
else{
coasterEstop.set(false);
coasterMode.set("System Ready");
}
});
});
lightButton.on("down",function(){
console.log("the light button was pressed");
lightStatus.once('value',function(check){
if(check.val()===true){
lightStatus.set(false);
}
else{
lightStatus.set(true);
}
})
})//end light button
carBack.on("down",function(){
console.log("The car has returned");
//the coaster has completed a loop so add to the total count
totalLoops.once('value',function(getNum){
var loopNum = getNum.val();
totalLoops.set(loopNum+1);//adds one each loop
});
coasterEstop.once('value',function(getData){
if(getData.val()===false){
var d = new Date();
var n = d.getTime();
timeReturned.set(n);//send the final time
timeDeparted.once('value',function(getTime){
var l = getTime.val();
var c = n-l;//total ride time
timeElapsed.set(c);//set the time elapsed
recordTime.once('value',function(getTime){
if(getTime.val()>c){
recordTime.set(c);
}//end set new record
});
});//end set ride time
}//end submit ride final time
});
coasterMode.once('value',function(getMode){
if(getMode.val()==="Single loop"){
coasterMode.set("System Ready");
}
if(getMode.val()==="Loop for"){
loopCount.once('value',function(goAgain){
if(goAgain.val()>0){
var newCount = goAgain.val()-1;
loopCount.set(newCount);
sendCoaster();
}
else{
coasterMode.set("System Ready");
}
});
}
if(getMode.val()==="Continuous"){
sendCoaster();//if the mode is in coninuous just keep sending the coaster
}
});
})//end of car returned block
function sendCoaster(){
var d = new Date();
var n = d.getTime();
timeDeparted.set(n);//set the time that the car departs
motorState.set(true);
setTimeout(function(){
motorState.set(false);
},4000);
}
});//end of the johnny-five board ready