1 (edited by Vin1 2024-03-18 01:53:19)

Topic: Adding switch button to existing code using l298n module with 1 dc mot

#include <dht.h>
#include <BTS7960.h>

dht DHT;
int Sensor = 12;
int temperature;
int humidity;

/////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__ESP8266_HARDSERIAL_POINT

#include <RemoteXY.h>

// RemoteXY connection settings
#define REMOTEXY_SERIAL Serial
#define REMOTEXY_SERIAL_SPEED 115200
#define REMOTEXY_WIFI_SSID ""
#define REMOTEXY_WIFI_PASSWORD ""
#define REMOTEXY_SERVER_PORT 6377

// L298N motor driver pins
#define ENA 13 // Enable A
#define IN1 7 // Input 1
#define IN2 2 // Input 2

// RemoteXY configurate 
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =   // 116 bytes
  { 255,3,0,22,0,109,0,17,0,0,0,93,2,106,200,200,84,1,1,5,
  0,5,22,86,60,60,86,36,25,25,16,121,27,26,2,32,166,44,22,60,
  70,83,9,0,136,26,31,31,79,78,0,79,70,70,0,67,7,39,40,10,
  13,16,75,4,4,39,27,11,67,59,39,40,10,112,16,75,4,4,178,27,
  11,129,6,29,87,8,13,11,117,4,24,84,101,109,112,101,114,97,116,117,
  114,101,32,32,32,32,72,117,109,105,100,105,116,121,32,0 };
 
// this structure defines all the variables and events of your control interface
struct {

    // input variables
  int8_t joystick_1_x; // from -100 to 100 
  int8_t joystick_1_y; // from -100 to 100 
  uint8_t switch_01; // =1 if switch ON and =0 if OFF

    // output variables
  char text_temp[11];  // string UTF8 end zero
  char text_hum[11];  // string UTF8 end zero

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0

} RemoteXY;
#pragma pack(pop)

// Left motor
const uint8_t LEFT_R_PWM_PIN = 6;
const uint8_t LEFT_L_PWM_PIN = 11;
const uint8_t LEFT_R_EN_PIN = 8;
const uint8_t LEFT_L_EN_PIN = 9;

// Right motor
const uint8_t RIGHT_R_PWM_PIN = 10;
const uint8_t RIGHT_L_PWM_PIN = 3;
const uint8_t RIGHT_R_EN_PIN = 4;
const uint8_t RIGHT_L_EN_PIN = 5;

// Control motor
BTS7960 motorController_1(RIGHT_R_EN_PIN, RIGHT_L_EN_PIN, RIGHT_R_PWM_PIN, RIGHT_L_PWM_PIN);
BTS7960 motorController_2(LEFT_R_EN_PIN, LEFT_L_EN_PIN, LEFT_R_PWM_PIN, LEFT_L_PWM_PIN);

void Wheel(int joystickX, int joystickY) {
   int speed = map(joystickX, -100, 100, -20, 20); // Map joystick X-axis to motor speed range
  int turnValue = map(joystickY, -100, 100, -20, 20); // Map joystick Y-axis to turning value

  // Calculate individual motor speeds based on turnValue
  int speedLeft = speed - turnValue;
  int speedRight = speed + turnValue;

  // Control the left motor
  if (speedLeft > 0) {
    motorController_2.pwm = speedLeft;
    motorController_2.front();
  } else if (speedLeft < 0) {
    motorController_2.pwm = -speedLeft;
    motorController_2.back();
  } else {
    motorController_2.stop();
  }

  // Control the right motor
  if (speedRight > 0) {
    motorController_1.pwm = speedRight;
    motorController_1.front();
  } else if (speedRight < 0) {
    motorController_1.pwm = -speedRight;
    motorController_1.back();
  } else {
    motorController_1.stop();
  }
}

void setup() {
  RemoteXY_Init();
  Serial.begin(115200); // Begin the serial monitor for output
  motorController_1.begin(); // Set the motor driver pins as output
  motorController_1.enable();
  motorController_2.begin(); // Set the motor driver pins as output
  motorController_2.enable();
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
}

void loop() {
  RemoteXY_Handler();
  int dhtCheck = DHT.read11(Sensor);
  temperature = DHT.temperature;
  humidity = DHT.humidity;

  dtostrf(temperature, 0, 1, RemoteXY.text_temp);
  dtostrf(humidity, 0, 1, RemoteXY.text_hum);

  // Manage the motors
  Wheel(RemoteXY.joystick_1_x, RemoteXY.joystick_1_y);
  delay(200);

  // Control the separate DC motor
  digitalWrite(ENA, RemoteXY.switch_01 == 0 ? LOW : HIGH);
 
}