Help with DC motors and Joystick widget

So after weeks of researching PWM, NodeJS, and DC motors on the Raspberry Pi, I was finally able to figure out how to control my RC car’s motors attached to a L298N motor driver. Blynk Joystick works great for PWM and moving forward on the car but I have no clue how to get it to go in reverse. What I would like to do is have the joystick’s upper half span (X axis) control the motor’s forward motion and use the lower span for reverse (same as Gunner’s remote vehicle for anybody that has seen his video) and I know the joystick can automatically return to center (neutral position) and that would stop the motors. If I ever figure that out then I’ll have to tackle left and right turns, but baby steps for now.
If anybody has any help in achieving the above in NodeJS, it will be greatly appreciated.

const Blynk = require('blynk-library');

const AUTH = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

var blynk = new Blynk.Blynk(AUTH, options = { connector : new Blynk.TcpClient( options = { addr: "192.168.x.xx", port:xxxx } ) });

const Gpio = require('pigpio').Gpio;

const forwardR = new Gpio(27, {mode: Gpio.OUTPUT});
const forwardL = new Gpio(10, {mode: Gpio.OUTPUT});
const reverseR = new Gpio(22, {mode: Gpio.OUTPUT});
const reverseL = new Gpio(17, {mode: Gpio.OUTPUT});


var JoyX = new blynk.VirtualPin(24);


JoyX.on('write', function(forwardLValue) {
  forwardL.pwmWrite(parseInt(forwardLValue));
});

JoyX.on('write', function(forwardRValue));
  forwardR.pwmWrite(parseInt(forwardRValue));
});

By reversing the PWM pin! Here is a working example in c++, but it so simple, you will be able to rewrite it in NodeJS.
As you can see, if spd is less than -3 it goes one direction, if larger then +3 the opposite. The brake is for braking function in my code - not needed here. the spd variable is mapped from incoming ‘motor1’ with the range from 0 (full ‘reverse’) to 1023 (full ‘forward’). That is just for ease of sending two byte variable and can be reworked here. This code was not created for Blynk

Just one note: Both pins (here INA, INB) has to be PWM capable.

void motorControl(byte brake, int motor1) {
  int spd = map(motor1, 0, 1023, -255, 255); 
  if (brake) {
    digitalWrite(INA, HIGH);
    digitalWrite(INB, HIGH);
  }
  else {
    if (spd < -3) {
      analogWrite(INA, spd * -1);
      digitalWrite(INB, LOW);
    }
    else if (spd > 3) {
      analogWrite(INA, 255 - spd);
      digitalWrite(INB, HIGH);
    }
    else {
      digitalWrite(INA, LOW);
      digitalWrite(INB, LOW);
    }
  }
}

Thank you for the response but I’m stuck on trying to port your example to NodeJS. I’m not familiar enough with programming languages to even know where to start. I’ve been doing a lot reading/research but all I find many examples in python to do what I want but nothing in NodeJS.

https://www.google.com/search?newwindow=1&ei=WOdjW-LNEvLr9AOEoYKoCQ&q=joystick+control+of+h-bridge+in+nodejs

Get it working without Blynk, then substitute Blynk’s Joystick widget coordinates later… You have probably already seen this NodeJS Joystick example, and by taking the joystick coordinates and combining them with whatever control code you choose from the Google search should get you closer…

Thanks Gunner, I have gone though all those results…I’m kind of slow learner but I just need to continue going over everything until something makes sense.

I think the biggest confusion here is that Blynk itself is just a GUI / HMI… it is NOT the control… your programming language of choice does that. However learning said language is outside of this forum… I am trying to learn C++, Node.JS and Python on my own as well. I like Blynk for it’s relitivly customisable user interface, but it is not the end-all-be-all.

1 Like