Go Down

Topic: Arduino bluetooth control robot (Read 252 times) previous topic - next topic

Hello! Friends I want to make bluetooth control arduino but I want if robot turn any it make approximate 90° angle. First I use delay but result is not always same so I add one MPU6050 or Gy-521 accelerometer and gyroscope sensor.

 I use in this project arduino Mega 2560 r3 , adafruit motor shield, Hm 10 bluetooth module, Gy-521 or Mpu6050 accelerometer and gyroscope sensor , bo motor , chassis and 11.1V lithium ion battery.

I make connection look like this
HM 10 (
VCC - 5V
Ground - Ground
TX - 53)
Mpu6050 or Gy521 (
VCC - 5V
Ground - Ground
SCL - SCL
SDA - SDA)
I attach adafruit motor shield into Arduino Mega and left motor attach M4 and Right motor attach M3.

The code of this project
Code: [Select]
#include <SoftwareSerial.h>
#include <AFMotor.h>
#include <MPU6050_tockn.h>
#include <Wire.h>

SoftwareSerial mySerial(53, 52); // RX, TX
MPU6050 mpu6050(Wire);
AF_DCMotor leftmotor(4);
AF_DCMotor rightmotor(3);
int i = 175;
int x = 200;


void setup() {
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);     
leftmotor.setSpeed(i);
rightmotor.setSpeed(i);
mySerial.begin(9600);
 
}

void loop() {
  if (mySerial.available()) {
        switch (mySerial.read())
      {
        case 'a':
        {
          forward();
          break;
        }
        case 'b':
        {
         backward();
         break;
        }
       case 'c':
      {
         stop();
          break;
        }
         case 'd':
        {
         Rightward();
         break;
        }
       case 'e':
      {
         Leftward();
          break;
        }
       
      }
    }
   }
 
void Rightward() {
 int   Old = mpu6050.getAngleX();
   right();
     delay(100);
int   New = mpu6050.getAngleX();
  if(90 < (Old-New)){
     stop();
 } else {
    Old = mpu6050.getAngleX();
   right();
     delay(100);
   New = mpu6050.getAngleX();
    }
}

void Leftward() {
   int   Old = mpu6050.getAngleX();
   left();
     delay(100);
 int  New = mpu6050.getAngleX();
  if(90 < (New-Old)){
     stop();
 } else {
    Old = mpu6050.getAngleX();
   left();
     delay(100);
   New = mpu6050.getAngleX();
    }
}


void forward() {
    leftmotor.run(FORWARD);
    rightmotor.run(FORWARD);
    rightmotor.setSpeed(i); 
    leftmotor.setSpeed(i); 
}

void backward() {
    leftmotor.run(BACKWARD);
    rightmotor.run(BACKWARD);
    leftmotor.setSpeed(i);
    rightmotor.setSpeed(i);
}

void left() {
    leftmotor.run(RELEASE);
    rightmotor.run(FORWARD);
    rightmotor.setSpeed(x);
}

void right() {
    rightmotor.run(RELEASE);
    leftmotor.run(FORWARD);
    leftmotor.setSpeed(x);
}

void stop() {
    leftmotor.run(RELEASE);
    rightmotor.run(RELEASE);
}
.

The all forward , backward & stop work correctly but the right & left not work correctly.
For example -  if I send left command the right motor run but if the angle is > 90 so it not stop automatically.

Please help me how I turn my robot Approximate 90°.


Practice make man better. In starting Very most difficult subject practice make easiest.

PaulS

The Mega has 4 hardware serial ports. You are using none of them. So, why do you need SoftwareSerial?

right() and left() just start the motors spinning appropriately. Rightward() and Leftward() read the mpu6050, but do nothing with the data. They start a turn, but they do not loop until the amount of turn even approximately equals anything. Why is there no looping? Why isn't the mpu6050 data used?
The art of getting good answers lies in asking good questions.

Go Up