0

I want to control an RC car with a joystick in mobile. Currently, I can only make the motor move forward and backward but not able to control the speed. Node mcu is used as a processor to receive the message.

Here is the code below:-

#include <ESP8266WiFi.h>
#define PubNub_BASE_CLIENT WiFiClient
#include <PubNub.h>
#define PUBNUB_DEFINE_STRSPN_AND_STRNCASECMP
#include <L298N.h>

const char *ssid =  "mym404";     // replace with your wifi ssid and wpa2 key
const char *pass =  "786110786";

const char * pubkey = "";
const char * subkey = "";

const char * subchannel = "base";

#define ENB D6
#define IN1 D3
#define IN2 D4

L298N motorL(ENB, IN1, IN2);


#define ENA D5
#define IN3 D1
#define IN4 D7

L298N motorR(ENA, IN3, IN4);


void setup() {
    /* For debugging, set to speed of your choice */
    Serial.begin(115200);


       Serial.println("Connecting to ");
       Serial.println(ssid); 

       WiFi.begin(ssid, pass); 
       while (WiFi.status() != WL_CONNECTED) 
          {
            delay(500);
            Serial.print(".");
          }
      Serial.println("");
      Serial.println("WiFi connected");

    /* Start the Pubnub library by giving it a publish and subscribe
       keys */
    PubNub.begin(pubkey, subkey);

}

void loop() {

  int tim = millis();



    PubSubClient *sclient = PubNub.subscribe(subchannel);
    if (!sclient) return; // error
    String msg;
    SubscribeCracker ritz(sclient);
    if (!ritz.finished()) {
        ritz.get(msg);
        if (msg.length() > 0) {
            Serial.print("Received: "); Serial.println(msg);

            String X = getStringPartByNr(msg, ':', 0);
            String Y = getStringPartByNr(msg, ':', 1);

            X.remove(0,1);

            int Xcord = X.toInt();
            int Ycord = Y.toInt();

            Serial.print("X: "); Serial.println(Xcord);
            Serial.print("Y: "); Serial.println(Ycord);

            int angle;
            int throttle = (Xcord + Ycord)/2;



            if ((Xcord > 100) && (Ycord > 100)) {
              angle = round(atan2(Ycord-100,Xcord-100)*(180/3.14));
              motorR.forward();
              motorL.forward();
              motorR.setSpeed(abs(int(throttle*sin(angle))));
              motorL.setSpeed(throttle);
              Serial.print("R speed: "); Serial.println(abs(int(throttle*sin(angle))));
              Serial.print("L speed: "); Serial.println(throttle); 
              //motorR.forward();
              //motorL.forward();
            }
            else if ((Xcord < 100) && (Ycord > 100)) {
              angle = 180 - round(atan2(Ycord-100,100-Xcord)*(180/3.14));

              motorR.setSpeed(throttle);
              motorL.setSpeed(abs(int(throttle*sin(angle))));
              Serial.print("R speed: "); Serial.println(throttle);
              Serial.print("L speed: "); Serial.println(abs(int(throttle*sin(angle))));
              motorR.forward();
              motorL.forward();
            }
            else if ((Xcord < 100) && (Ycord < 100)) {
              angle = 180 + round(atan2(100-Ycord,100-Xcord)*(180/3.14));

              motorR.setSpeed(200-throttle);
              motorL.setSpeed(abs(int((200-throttle)*sin(angle)*(-1))));
              Serial.print("R speed: "); Serial.println(200-throttle);
              Serial.print("L speed: "); Serial.println(abs(int((200-throttle)*sin(angle)*(-1))));
              motorR.backward();
              motorL.backward();
            }
            else if ((Xcord > 100) && (Ycord < 100)) {
              angle = 270 + round(atan2(Xcord-100,100-Ycord)*(180/3.14));
              motorR.setSpeed(abs(int((200-throttle)*sin(angle)*(-1))));
              motorL.setSpeed(200-throttle);
              Serial.print("R speed: "); Serial.println(abs(int((200-throttle)*sin(angle)*(-1))));
              Serial.print("L speed: "); Serial.println(200-throttle);
              motorR.backward();
              motorL.backward();
            }

            Serial.print("Angle: "); Serial.println(angle);
            //Serial.print("time: "); Serial.println(tim);



        }
    }



    sclient->stop();


}

String getStringPartByNr(String data, char separator, int index)
{
    // splitting a string and return the part nr index
    // split by separator

    int stringData = 0;        //variable to count data part nr 
    String dataPart = "";      //variable to hole the return text

    for(int i = 0; i<data.length()-1; i++) {    //Walk through the text one letter at a time

      if(data[i]==separator) {
        //Count the number of times separator character appears in the text
        stringData++;

      }else if(stringData==index) {
        //get the text when separator is the rignt one
        dataPart.concat(data[i]);

      }else if(stringData>index) {
        //return text and stop if the next separator appears - to save CPU-time
        return dataPart;
        break;

      }

    }
    //return text if this is the last part
    return dataPart;
}

Port Reading:- enter image description here

What can the problem be?

  • NodeMCU's PWM ranges from 0-1024 I believe. Not just 0-255. Also, make sure the pin is PWM capable. And try a minimal sketch like: motorL.speed(1000); motorR.speed(500); motorR.forward(); motorL.forward(); – Paul Mar 29 at 20:23
  • thanks will try it – suu Mar 30 at 7:01
0

That's because the l298n library are used for the arduino or other microcontroller that have hardware PWM in their systems. The library using hardware pwm by generating pulses and make variations of the cyclic rapport aka "alpha" . Esp8266 have no pwm in their systems So try to use software pwm library for the esp8266

this is the link. https://www.arduinolibraries.info/libraries/soft-pwm

Also don't include that library and try to make it work using easy lines of codes.

  • u mean controlling pwm using duty cycles or analogWrite? – suu Mar 30 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.