Luc Paquin
Published © CC BY-ND

Project #12: Robotics - Arduino BLE - Mk37

Project #12: Robotics - Arduino BLE - Mk37

IntermediateFull instructions provided1 hour19
Project #12: Robotics - Arduino BLE - Mk37

Things used in this project

Hardware components

Arduino UNO R4 WiFi
Arduino UNO R4 WiFi
×1
L298N DC Motor Driver Module
×1
Motor DC
×2
Power Switch
×1
18650 Battery Holder (11 Volts)
×1
18650 Battery
×3
DFRobot USB 3.0 to Type-C Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Fritzing

Code

DL2603Mk05p.ino

Arduino
/****** Don Luc Electronics © ******
Software Version Information
Project #12: Robotics - Arduino BLE - Mk37
12-37
DL2603Mk05p.ino
DL2603Mk05
1 x Arduino UNO R4 WiFi
1 x L298N DC Motor Driver Module
2 x Motor DC
1 x Power Switch
1 x 18650 Battery Holder (11 Volts)
3 x 18650 Battery
1 x Micro USB Cable
*/

// Include the Library Code
// LED_Matrix library
#include "Arduino_LED_Matrix.h" 
// Arduino BLE
#include <ArduinoBLE.h>

// Arduino UNO R4 WiFi
// Car Service
BLEService carService("546b10ec-9c23-4f5e-ab33-cd415313439f");
// Command Char
BLECharacteristic commandChar("c607bca7-4832-4032-a0ab-e9c180dcce1e", BLEWrite, 20);

// Create an instance of the ArduinoLEDMatrix class
ArduinoLEDMatrix matrix;

// Motor DC 1 
int motor1pin1 = 2;
int motor1pin2 = 3;

// Motor DC 2
int motor2pin1 = 4;
int motor2pin2 = 5;

// CMD
char cmd;

// Software Version Information
String sver = "12-37";

void loop() {

  // BLE Device Central
  BLEDevice central = BLE.central();
  // Central
  if (central) {
    
    // Connected
    while (central.connected()) {

      // Written
      if (commandChar.written()) {

        // CMD
        cmd = commandChar.value()[0];
       
        // Drive
        drive(cmd);

      }

    }

  }

}

getDrive.ino

Arduino
// Drive
void drive(char cmd) {
  // Forward
       if (cmd == 'F') { forward();  }  
    // Backward
  else if (cmd == 'B') { backward(); }  
  // Left
  else if (cmd == 'L') { left(); } 
  // Right     
  else if (cmd == 'R') { right(); }
  // Stop
  else { stopCar(); }
}

// Forward
void forward()  { digitalWrite(motor1pin1, LOW);  digitalWrite(motor1pin2, HIGH); digitalWrite(motor2pin1, HIGH); digitalWrite(motor2pin2, LOW); }
// Backward
void backward() { digitalWrite(motor1pin1, HIGH); digitalWrite(motor1pin2, LOW);  digitalWrite(motor2pin1, LOW);  digitalWrite(motor2pin2,HIGH); }
// Left
void left()     { digitalWrite(motor1pin1, LOW);  digitalWrite(motor1pin2, HIGH); digitalWrite(motor2pin1, LOW);  digitalWrite(motor2pin2, LOW); }
// Right
void right()    { digitalWrite(motor1pin1, LOW);  digitalWrite(motor1pin2, LOW);  digitalWrite(motor2pin1, HIGH); digitalWrite(motor2pin2, LOW); }
// Stop
void stopCar()  { digitalWrite(motor1pin1, LOW);  digitalWrite(motor1pin2, LOW);  digitalWrite(motor2pin1, LOW);  digitalWrite(motor2pin2, LOW); }

setup.ino

Arduino
// Setup
void setup()
{
 
  // Initialize the LED matrix
  matrix.begin();

  // Load and display the basic emoji frame on the LED matrix
  matrix.loadFrame(LEDMATRIX_UNO);
  
  // Motor DC 1
  pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);

  // Motor DC 2
  pinMode(motor2pin1, OUTPUT);
  pinMode(motor2pin2, OUTPUT);

  // Serial Begin
  Serial.begin(9600);
  
  // BLE Begin
  if (!BLE.begin()) {
    
    while (1);

  }

  // Name
  BLE.setLocalName("UnoR4Car");
  // Advertised Service
  BLE.setAdvertisedService(carService);
  // Characteristic
  carService.addCharacteristic(commandChar);
  // Service
  BLE.addService(carService);
  // Advertise
  BLE.advertise();

}

Credits

Luc Paquin
74 projects • 5 followers
Teacher, Instructor, E-Mentor, R&D and Consulting -Programming Language -Microcontrollers -IoT -Robotics -Machine Learning -AI -Sensors

Comments