Getting a strange error with Johnny-five motors

335 views Asked by At

I'm trying to get a motor to work with Johhny-five. I'm using an arduino and I copied the code and the wiring (mostly) from their website. The only thing I changed in the wiring was instead of using a diode to make sure 5V doesn't go into the emitter pin of the transistor, I just wired it straight to the motor, without using the breadboard. Problem is, I'm getting this weird error

C:\Users\simas\node_modules\johnny-five\lib\motor.js:721 this.speed({ ^

TypeError: this.speed is not a function at Timeout.Motor.stop [as _onTimeout] (C:\Users\simas\node_modules\johnny-five\lib\motor.js:721:8) at ontimeout (timers.js:436:11) at tryOnTimeout (timers.js:300:5) at listOnTimeout (timers.js:263:5) at Timer.processTimers (timers.js:223:10) PS C:\Users\simas\Desktop\motors>

I am not at all use why this happens, please help.

(btw the code I copied from the website was :

const {Board, Motor} = require("johnny-five");
const board = new Board();

board.on("ready", () => {
 // Create a new `motor` hardware instance.
 const motor = new Motor({
   pin: 5
 });

 // Inject the `motor` hardware into
 // the Repl instance's context;
 // allows direct command line access
 board.repl.inject({
   motor
 });

 // Motor Event API

 // "start" events fire when the motor is started.
 motor.on("start", () => {
   console.log(`start: ${Date.now()}`);

   // Demonstrate motor stop in 2 seconds
   board.wait(2000, motor.stop);
 });

 // "stop" events fire when the motor is stopped.
 motor.on("stop", () => {
   console.log(`stop: ${Date.now()}`);
 });

 // Motor API

 // start([speed)
 // Start the motor. `isOn` property set to |true|
 // Takes an optional parameter `speed` [0-255]
 // to define the motor speed if a PWM Pin is
 // used to connect the motor.
 motor.start();

 // stop()
 // Stop the motor. `isOn` property set to |false|
});

)

1

There are 1 answers

2
Trent On

I struggled with this same issue as a result of using the Adafruit Motor Shield V2 example code from the Johnny-Five documentation.

This appears to be an issue with the context bound to the motor.stop call when passing the function directly to board.wait.

To get around this issue, declare your own function within the motor instance context and pass it to the board.wait call in your "start" event handler:

 // "start" event fires when the motor is started
 motor.on("start", () => {
   console.log(`start: ${Date.now()}`);

   // Demonstrate motor stop in 2 seconds
   board.wait(2000, () => {
     motor.stop();
   });
 });