Better comments

This commit is contained in:
Nathan Kunicki 2018-07-26 09:17:26 +01:00
parent 9cd657af81
commit c61d3ec0d6
3 changed files with 14 additions and 12 deletions

View File

@ -15,9 +15,9 @@ lpf2.on("discover", async (hub) => { // Wait to discover hubs
let color = 0; let color = 0;
setInterval(() => { setInterval(() => {
const hubs = lpf2.getConnectedDevices(); const hubs = lpf2.getConnectedDevices(); // Get an array of all connected hubs
hubs.forEach((hub) => { hubs.forEach((hub) => {
hub.setLEDColor(color); hub.setLEDColor(color); // Set the color
}) })
color++; color++;
if (color > 10) { if (color > 10) {

View File

@ -10,15 +10,18 @@ lpf2.on("discover", async (hub) => { // Wait to discover hubs
await hub.connect(); // Connect to hub await hub.connect(); // Connect to hub
console.log("Connected to Hub!"); console.log("Connected to Hub!");
await hub.wait(2000); // Wait two seconds before starting the train
hub.setMotorSpeed("A", 40);
hub.on("color", (port, color) => { hub.on("color", (port, color) => {
if (color === LPF2.Consts.Colors.YELLOW) { if (color === LPF2.Consts.Colors.YELLOW) { // If yellow is seen, stop the train, wait two seconds, and reverse direction
hub.setMotorSpeed("A", 0); hub.setMotorSpeed("A", 0);
await hub.wait(2000); await hub.wait(2000);
hub.setMotorSpeed("A", -40); hub.setMotorSpeed("A", -40);
} else if (color === LPF2.Consts.Colors.RED) { } else if (color === LPF2.Consts.Colors.RED) { // If red is seen, stop the train, wait two seconds, and reverse direction
hub.setMotorSpeed("A", 0); hub.setMotorSpeed("A", 0);
await hub.wait(2000); await hub.wait(2000);
@ -28,7 +31,4 @@ lpf2.on("discover", async (hub) => { // Wait to discover hubs
}); });
await hub.wait(2000);
hub.setMotorSpeed("A", 40);
}); });

View File

@ -22,29 +22,29 @@ lpf2.on("discover", async (hub) => { // Wait to discover Vernie and Remote
remote.on("button", async (button, state) => { remote.on("button", async (button, state) => {
if (vernie) { if (vernie) {
switch (state) { switch (state) {
case LPF2.Consts.ButtonStates.UP: case LPF2.Consts.ButtonStates.UP: // If up is pressed, move the track forward
{ {
vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", 50); vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", 50);
break; break;
} }
case LPF2.Consts.ButtonStates.DOWN: case LPF2.Consts.ButtonStates.DOWN: // If down is pressed, move the track backwards
{ {
vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", -50); vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", -50);
break; break;
} }
case LPF2.Consts.ButtonStates.RELEASED: case LPF2.Consts.ButtonStates.RELEASED: // Stop the track when the button is released
{ {
if (button !== "GREEN") { if (button !== "GREEN") {
vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", 0); vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", 0);
} }
break; break;
} }
case LPF2.Consts.ButtonStates.STOP: case LPF2.Consts.ButtonStates.STOP: // Move the head left or right when a red button is pressed
{ {
await vernie.setMotorAngle("D", 35, button === "LEFT" ? -20 : 20); await vernie.setMotorAngle("D", 35, button === "LEFT" ? -20 : 20);
break; break;
} }
case LPF2.Consts.ButtonStates.PRESSED: case LPF2.Consts.ButtonStates.PRESSED: // Fire when the green button is pressed
{ {
if (button === "GREEN") { if (button === "GREEN") {
await vernie.setMotorAngle("D", 80, 20); await vernie.setMotorAngle("D", 80, 20);
@ -61,6 +61,8 @@ lpf2.on("discover", async (hub) => { // Wait to discover Vernie and Remote
} }
if (vernie && remote) { if (vernie && remote) {
vernie.setLEDColor(LPF2.Consts.Colors.GREEN);
remote.setLEDColor(LPF2.Consts.Colors.GREEN);
console.log("You're now ready to go!"); console.log("You're now ready to go!");
} }