Ahmed Ibrahim Ahmed
Published © GPL3+

How to Control 2WD Robot Wirelessly Through Processing

Want to control you robot wirelessly through your laptop?! We're going to do that in this step by step tutorial with Arduino and Processing.

IntermediateFull instructions provided4 hours14,542
How to Control 2WD Robot Wirelessly Through Processing

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Bluetooth Module HC-05
×1
2WD Robot Kit (Includes Motors and Wheels)
It Includes Motors and wheels
×1
L298N Motor Driver Module
×1
7.4V 1.5Ah
Any 7.4V and more than 1A should do the job well.
×1
Jumper wires (generic)
Jumper wires (generic)
Male-Female Jumper Wires
×1

Software apps and online services

Processing
The Processing Foundation Processing
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Final Wiring Diagram

Code

Final Processing Code

Processing
import controlP5.*; //import ControlP5 library
import processing.serial.*;

Serial port;

ControlP5 cp5; //create ControlP5 object

void setup() { //same as arduino program

  size(500, 300);    //window size, (width, height)

  printArray(Serial.list());   //prints all available serial ports

  port = new Serial(this, "/dev/tty.HC-05-DevB", 9600);      //please change the "/dev/tty.HC-05-DevB" according to your COM name.

  //lets add buton to empty window

  cp5 = new ControlP5(this);

  cp5.addButton("forward")     //"forward" is the name of button
    .setPosition(190, 50)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
    ;   

  cp5.addButton("backward")     //"backward" is the name of button
    .setPosition(190, 210)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
    ;

  cp5.addButton("right")     //"right" is the name of button
    .setPosition(350, 130)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
    ;

  cp5.addButton("left")     //"left" is the name of button
    .setPosition(30, 130)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
    ;

  cp5.addButton("stop")     //"left" is the name of button
    .setPosition(190, 130)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
    ;
}

void draw() {  //same as loop in arduino

  background(150, 0, 150); // background color of window (r, g, b) or (0 to 255)

  //lets give title to our window
  fill(255);
  text("Motor Control GUI", 195, 30);  // ("text", x coordinate, y coordinat)
}

//lets add some functions to our buttons
//so whe you press any button, it sends perticular char over serial port

void forward() {
  port.write('f');
}

void backward() {
  port.write('b');
}

void right() {
  port.write('r');
}

void left() {
  port.write('l');
}

void stop() {
  port.write('s');
}

Final Arduino Code

Arduino
#define in1 4                                                           //defining const. variable named "in1" with value 0, which refers to the PICO D0.
#define in2 5                                                           //defining const. variable named "in2" with value 1, which refers to the PICO D1.
#define in3 7                                                           //defining const. variable named "in3" with value 2, which refers to the PICO D2.
#define in4 8                                                           //defining const. variable named "in4" with value 3, which refers to the PICO D3.
#define motorASpeedPin 9
#define motorBSpeedPin 3

char val;

/*
   forward function takes four arguments x, y, motorSpeed, speedPin.
   this function returns nothing.
   responsible for running motor A forward by a specific speed.
*/
void forward(int x, int y, int motorSpeed, int speedPin) {
  digitalWrite(x, HIGH);
  digitalWrite(y, LOW);
  analogWrite(speedPin, motorSpeed);
}

/*
   backward function takes four arguments x, y, motorSpeed, speedPin.
   this function returns nothing.
   responsible for running motor B backward with a specific speed.
*/
void backward(int x, int y, int motorSpeed, int speedPin) {
  digitalWrite(x, LOW);
  digitalWrite(y, HIGH);
  analogWrite(speedPin, motorSpeed);
}

void right(int motorSpeed) {
  forward(in1, in2, motorSpeed, motorASpeedPin);
  backward(in3, in4, motorSpeed, motorBSpeedPin);
}

void left(int motorSpeed) {
  forward(in3, in4, motorSpeed, motorBSpeedPin);
  backward(in1, in2, motorSpeed, motorASpeedPin);
}

void stopAll() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

void setup() {

  for (int i = 3 ; i <= 5 ; i++) {
    pinMode(i, OUTPUT);
  }

  for (int i = 7 ; i <= 9 ; i++) {
    pinMode(i, OUTPUT);
  }

  Serial.begin(9600);
  Serial.println("Please enter 'f' to run the motor FORWARD, or 'b' to run the motor BACKWARD");
  Serial.println("or 'r' to run the motor RIGHT, or 'l' to run the motor LEFT, or 's' to STOP all motors. ");
}

void loop() {

  if (Serial.available() > 0) {

    val = Serial.read();
  }

  if (val == 'f') {
    forward(in1, in2, 255, motorASpeedPin);
    forward(in3, in4, 255, motorBSpeedPin);
    Serial.println("forward");
  }
  if (val == 'b') {
    backward(in1, in2, 255, motorASpeedPin);
    backward(in3, in4, 255, motorBSpeedPin);
    Serial.println("backward");
  }
  if (val == 'r') {
    right(255);
    Serial.println("right");
  }
  if (val == 'l') {
    left(255);
    Serial.println("left");
  }
  if (val == 's') {
    stopAll();
    Serial.println("stopAll");
  }
}

Credits

Ahmed Ibrahim Ahmed
11 projects • 109 followers
An Egyptian Computer Engineering Student, Robotics And Embedded Systems Geek.
Contact

Comments

Please log in or sign up to comment.