L298N and Esp32

I am using 4 motors and L298N with Vietduino Esp32 to control. I use lipo battery to power L298N. Esp32 is used to connect to the network and is controlled via the network on the phone. But when I connect, I cannot control the motor. I need it urgently, please help me.
English is not my native language so I have some mistakes in communication, I hope you will forgive me :blush:
This is code!

#define DEFAULT_MQTT_HOST "mqtt1.eoh.io" 

// You should get Auth Token in the ERa App or ERa Dashboard
#define ERA_AUTH_TOKEN "f76f573b-54b5-4412-b02c-78c5d6beea5b"
#include <Arduino.h> 
#include <ERa.hpp> 
#include <ERa/ERaTimer.hpp> 


const char ssid[] = "Brian";
const char pass[] = "12092010";
ERaTimer timer; // setup timer 

ERA_WRITE(V0){
  int value = param.getInt();
  if(value){
    digitalWrite(2, 1);
    digitalWrite(13,0);
    digitalWrite(5, 0);
    digitalWrite(23, 1);
  }else{
    digitalWrite(2, 0);
    digitalWrite(13,0);
    digitalWrite(5, 0);
    digitalWrite(23, 0);
  }
}
ERA_WRITE(V1){
  int value = param.getInt();
  if(value){
    digitalWrite(2, 0);
    digitalWrite(13,1);
    digitalWrite(5, 1);
    digitalWrite(23, 0);
  }else{
    digitalWrite(2, 0);
    digitalWrite(13,0);
    digitalWrite(5, 0);
    digitalWrite(23, 0);
  }
}
ERA_WRITE(V2){
  int value = param.getInt();
  if(value){
    digitalWrite(2, 0);
    digitalWrite(13,1);
    digitalWrite(5, 0);
    digitalWrite(23, 1);
  }else{
    digitalWrite(2, 0);
    digitalWrite(13,0);
    digitalWrite(5, 0);
    digitalWrite(23, 0);
  }
}
ERA_WRITE(V3){
  int value = param.getInt();
  if(value){
    digitalWrite(2, 1);
    digitalWrite(13,0);
    digitalWrite(5, 1);
    digitalWrite(23, 0);
  }else{
    digitalWrite(2, 0);
    digitalWrite(13,0);
    digitalWrite(5, 0);
    digitalWrite(23, 0);
  }
}

/* This function print uptime every second */
void timerEvent() { //Khai báo hàm timer set thời gian cập nhật dữ liệu lên sever
    ERA_LOG("Timer", "Uptime: %d", ERaMillis() / 1000L); 
}

void setup() { //setup mạch
    /* Setup debug console */
    Serial.begin(115200); //Mở Serial monitor với baud rate là 115200 
    ERa.begin(ssid, pass); //setup mạng cho mạch esp32
    pinMode(2, OUTPUT);
    pinMode(13,OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(23, OUTPUT);
    /* Setup timer called function every second */
    timer.setInterval(1000L, timerEvent); 
}

void loop() { //Vòng lặp liên tục
    ERa.run();//khởi chạy Era
    timer.run();//khởi chạy timer
}

i am no professional but I feel pin modes should be declared before digital writes and also you have 0 as LOW and 1 as High in your digitalwrite LOW= 0 HIGH = 1 and must be capitals but like I said I am a beginner myself

@Always_be_kind Can you elaborate on your opinion? I still don't quite understand. :thinking::thinking:

anything above this is the global variables setup is run after global variables are initialized

The commands in setup() will be run when your program starts. You can use it to declare variable values, declare libraries, set parameters, etc. I'm just a newbie to Arduino so there are still many things I don't know clearly. Please forgive me! :sweat_smile:

yes but the global variables from my understanding are not for the actual program its for variables the Loop is the program once setup is complete the loop will loop

basically the global is only accessed when instructed to by the loop and setup only runs once

and functions should in my understanding should be in the loop as well

This function helps declare the timer function to set the time to update data to the server.

where in the loop are you calling the function

once loop starts it just loops the loop lol

im not familiar with era so I probably cant help

i just know that anything above setup is called the global variables and that setup only runs once then the loop is the actual program.

I know. In void setup, the code only runs once, but in void loop, the code can repeat. Is that what you mean?

yes once loop starts it repeats until reset

if and else statements should be part of loop

But if and else is the program of ERA_WRITE and era has been run in loop.

well its looking for a value so if it does not keep checking for changes in value the it will only see the initial value

Oh thanks, this is a very helpful example for me.

1 Like

here is an example of basic motor control using a key board
rover keyboard control.txt (1.7 KB)

1 Like

i hope it helps you