Program to control a micro servo motor
Write the following code in Cloud9 and save it as microServo.js
. Run the program and you should see the servo motor moving from 0 to 180 degrees to and fro.
The code for is microServo.js
as follows:
var b = require('bonescript'); var servo = 'P9_14'; var duty_min = 3; var loopTime = 5; var angle = 0; var increment = true; b.pinMode(servo, b.OUTPUT); var loopTimer = setInterval(updateAngle, loopTime); function updateAngle(x) { if(angle == 180 ) increment = false; if(angle == 0) increment = true; if(increment == true) angle = angle + 1; else angle = angle - 1; var duty_cycle = (angle * 0.064) + duty_min; console.log("angle =",angle); console.log("duty cycle = ",duty_cycle); b.analogWrite(servo, duty_cycle/100, 60); }
Explanation
In this program, we created loopTimer
, which will increment/decrement the shaft angle by one degree. We know that we have a limit of 3% to 14.5% to drive...