#7 !! NodeJS !! - Pan & Tilt Servo control with Joystick for Raspberry Pi using NodeJS
And now for something different… here is some simple javascript code for using the joystick widget with a couple servos on an RPi for use with a pan and tilt base. More NodeJS stuff here
Hook up your two servos with 5v, GND and GPIO pins 22 & 23 .
I recommend a dedicated 5v supply and remember to share the GND with the RPi.
Joystick Widget set for vPin 22 & 23 (to match the GPIO just for hookup convenience, they can be any other vPins) and whatever range settings work best for your servos.
const Gpio = require('pigpio').Gpio;
const PAN_PORT = 22;
const TILT_PORT = 23;
const pan = new Gpio(PAN_PORT, {mode: Gpio.OUTPUT});
const tilt = new Gpio(TILT_PORT, {mode: Gpio.OUTPUT});
const Blynk = require('blynk-library'); // Links variable 'Blynk' to the Blynk Library
const AUTH = 'xxxxxxxxxx';
// ----- Use this with Cloud Server.
// var blynk = new Blynk.Blynk(AUTH, options = { connector : new Blynk.TcpClient() });
// ----- Use this with Local Server.
var blynk = new Blynk.Blynk(AUTH, options = { connector : new Blynk.TcpClient( options = { addr: "xxx.xxx.xxx.xxx", port: 8080 } ) });
var JoyX = new blynk.VirtualPin(22); // Setup Joystick X on V22
var JoyY = new blynk.VirtualPin(23); // Setup Joystick Y on V23
JoyX.on('write', function(panValue) { // Watches for Joystick X
pan.servoWrite(panValue); // Set Servo to value
});
JoyY.on('write', function(tiltValue) { // Watches for Joystick y
tilt.servoWrite(tiltValue); // Set Servo to value
});