Move a DC motor
The code in this project will also work with other output bits that move including the fan and vibration-motor
Connect power bit to d0 on Arduino bit, d5 on Arduino bit to dc-motor bit. Flick the switch next to d5 to pwm.
You might also like to use a wire between d5 and the dc-motor to give you more flexibility to position the motor, or a motor-mate to attach some paper to the motor so you can see it moving more easily.
var five = require("johnny-five"),
board, motor;
board = new five.Board();
board.on("ready", function() {
motor = new five.Motor(5);
motor.start();
board.wait(5000, function() {
motor.stop();
});
this.repl.inject({
motor: motor
});
});
You can find a copy of this code in move/move.js
Run the code from the terminal e.g.
node move/move.js
The motor will spin for 5 seconds and then stop.
The board object's wait
method can be used to shedule something to happen. The first parameter is the delay in milliseconds, and the second is a function specifying what to do after waiting for the specified time.
Flicking the switch on the dc-motor module will switch the direction the motor is spinning.
Use the REPL to start or stop the motor:
motor.start()
motor.stop()
motor.isOn // property indicates whether it is moving
You can also control the motor speed:
motor.speed(50) // slow
motor.speed(120) // mid-speed
motor.speed(255) // full speed