node-poweredup/examples/train_reverse.js
2018-07-26 09:09:59 +01:00

34 lines
741 B
JavaScript

const LPF2 = require("..").LPF2;
const lpf2 = new LPF2();
lpf2.scan(); // Start scanning for hubs
console.log("Looking for Hubs...");
lpf2.on("discover", async (hub) => { // Wait to discover hubs
await hub.connect(); // Connect to hub
console.log("Connected to Hub!");
hub.on("color", (port, color) => {
if (color === LPF2.Consts.Colors.YELLOW) {
hub.setMotorSpeed("A", 0);
await hub.wait(2000);
hub.setMotorSpeed("A", -40);
} else if (color === LPF2.Consts.Colors.RED) {
hub.setMotorSpeed("A", 0);
await hub.wait(2000);
hub.setMotorSpeed("A", 40);
}
});
await hub.wait(2000);
hub.setMotorSpeed("A", 40);
});