Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Help With Bluetooth Robot
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Help With Bluetooth Robot

by Fencera on Fri Apr 16, 2021 3:02 pm

Hello, I am working on a basic Bluetooth controlled robot. I am using a single Adafruit Motorshield V2 but will stack another later. For some reason it will not work. It is using VIN power from an Arduino and have commented out a some of the extra code for simplification. Here is the code below:

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_MotorShield.h>
#include <Wire.h>
#include<SoftwareSerial.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
int bluetoothTx = 0; // bluetooth tx to 0 pin
int bluetoothRx = 1; // bluetooth rx to 1 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

Adafruit_DCMotor *MOTOR1 = AFMS.getMotor(1);
//Adafruit_DCMotor *MOTOR2 = AFMS.getMotor(2);
//Adafruit_DCMotor *MOTOR3 = AFMS.getMotor(3);
//Adafruit_DCMotor *MOTOR4 = AFMS.getMotor(4);

int speed = 230;
char value;
void setup() {
  Serial.begin(9600);
  bluetooth.begin(9600);
  AFMS.begin();
  MOTOR1->setSpeed(speed);
  //MOTOR2.setSpeed(Speed);
  //MOTOR3.setSpeed(Speed);
  //MOTOR4.setSpeed(Speed);
 
 
}
 
void loop() {
  if (bluetooth.available() > 0) {
    value = bluetooth.read();
  }
  if (value == 'F') {
    MOTOR1->run(FORWARD);
    //MOTOR2.run(FORWARD);
    //MOTOR3.run(FORWARD);
    //MOTOR4.run(FORWARD);
  } else if (value == 'B') {
    MOTOR1->run(BACKWARD);
    //MOTOR2.run(BACKWARD);
    //MOTOR3.run(BACKWARD);
    //MOTOR4.run(BACKWARD);
  } else if (value == 'L') {
    MOTOR1->run(BACKWARD);
    //MOTOR2.run(BACKWARD);
    //MOTOR3.run(FORWARD);
    //MOTOR4.run(FORWARD);
  } else if (value == 'R') {
    MOTOR1->run(FORWARD);
    //MOTOR2.run(FORWARD);
    //MOTOR3.run(BACKWARD);
    //MOTOR4.run(BACKWARD);
  } else {
    MOTOR1 ->run(RELEASE);
    //MOTOR2.run(RELEASE);
    //MOTOR3.run(RELEASE);
    //MOTOR4.run(RELEASE);
  }
 
}
Last edited by adafruit_support_bill on Thu Apr 22, 2021 8:01 pm, edited 1 time in total.
Reason: Pleas use [code] tags when posting code to the forums

Fencera
 
Posts: 34
Joined: Tue Mar 02, 2021 9:44 am

Re: Help With Bluetooth Robot

by Fencera on Thu Apr 22, 2021 7:44 pm

I've updated the code and got our HC-06 Bluetooth module to work. only problem now is that whenever we want to drive the motors with the commands the motors just pulse they don't even spin. more like a slow vibration. how do we fix this?

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_MotorShield.h>
#include <Wire.h>
 
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_MotorShield AFMS2 = Adafruit_MotorShield(0X61);
Adafruit_DCMotor *FMOTORR = AFMS2.getMotor(1);
Adafruit_DCMotor *FMOTORL = AFMS2.getMotor(2);
Adafruit_DCMotor *BMOTORR = AFMS2.getMotor(3);
Adafruit_DCMotor *BMOTORL = AFMS2.getMotor(4);
Adafruit_DCMotor *EMOTOR = AFMS.getMotor(1);
Adafruit_DCMotor *EMOTOR2 = AFMS.getMotor(2);

char blueToothDATA;
char lastDATA;

void setup()
{
Serial.begin(9600);
AFMS.begin();
AFMS2.begin();
pinMode(13,OUTPUT);
}

void loop()
{
  if(Serial.available())
  {
    blueToothDATA=Serial.read();
  }
  if (blueToothDATA== 'F')
  {
    digitalWrite(13,HIGH);
    if(lastDATA!= 'F')
    FMOTORR->setSpeed(150);
    FMOTORL->setSpeed(150);
    BMOTORR->setSpeed(150);
    BMOTORL->setSpeed(150);
    FMOTORR->run(FORWARD);
    FMOTORL->run(FORWARD);
    BMOTORR->run(FORWARD);
    BMOTORL->run(FORWARD);
      Serial.println(F("FORWARD"));
    lastDATA=blueToothDATA;
  }
  if (blueToothDATA=='B')
  {
    digitalWrite(13,HIGH);
    if(lastDATA!='B')
    FMOTORR->setSpeed(150);
    FMOTORL->setSpeed(150);
    BMOTORR->setSpeed(150);
    BMOTORL->setSpeed(150);
    FMOTORR->run(BACKWARD);
    FMOTORL->run(BACKWARD);
    BMOTORR->run(BACKWARD);
    BMOTORL->run(BACKWARD);
      Serial.println(F("BACK"));
    lastDATA=blueToothDATA;
  }
  if (blueToothDATA=='L')
  {
    digitalWrite(13,HIGH);
    if(lastDATA!='L')
    FMOTORR->setSpeed(150);
    FMOTORL->setSpeed(150);
    BMOTORR->setSpeed(150);
    BMOTORL->setSpeed(150);
    FMOTORR->run(FORWARD);
    FMOTORL->run(BACKWARD);
    BMOTORR->run(FORWARD);
    BMOTORL->run(BACKWARD);
      Serial.println(F("TURN LEFT"));
    lastDATA=blueToothDATA;
  }
   if (blueToothDATA=='R')
  {
    digitalWrite(13,HIGH);
    if(lastDATA!='R')
    FMOTORR->setSpeed(150);
    FMOTORL->setSpeed(150);
    BMOTORR->setSpeed(150);
    BMOTORL->setSpeed(150);
    FMOTORR->run(BACKWARD);
    FMOTORL->run(FORWARD);
    BMOTORR->run(BACKWARD);
    BMOTORL->run(FORWARD);
      Serial.println(F("TURN RIGHT"));
    lastDATA=blueToothDATA;
  }
  if (blueToothDATA=='X')
  {
    digitalWrite(13,HIGH);
    if(lastDATA!='X')
    EMOTOR->setSpeed(150);
    EMOTOR2->setSpeed(150);
    EMOTOR->run(FORWARD);
    EMOTOR2->run(FORWARD);
      Serial.println(F("NOM NOM"));
    lastDATA=blueToothDATA;
  }
  else
  {
     FMOTORR->run(RELEASE);
     FMOTORL->run(RELEASE);
     BMOTORR->run(RELEASE);
     BMOTORL->run(RELEASE);
     EMOTOR->run(RELEASE);
     EMOTOR2->run(RELEASE);
  }
  delay(250);
}
Last edited by adafruit_support_bill on Thu Apr 22, 2021 8:01 pm, edited 1 time in total.
Reason: Pleas use [code] tags when posting code to the forums

Fencera
 
Posts: 34
Joined: Tue Mar 02, 2021 9:44 am

Re: Help With Bluetooth Robot

by adafruit_support_bill on Thu Apr 22, 2021 8:02 pm

What motors and power supply are you using?

adafruit_support_bill
 
Posts: 81007
Joined: Sat Feb 07, 2009 10:11 am

Re: Help With Bluetooth Robot

by Fencera on Thu Apr 22, 2021 9:51 pm

they are all the 130 size dc motors 6 total, planning on addaing a servo in the future. i have it running on a 4 AA battery pack. powered through the uno

Fencera
 
Posts: 34
Joined: Tue Mar 02, 2021 9:44 am

Re: Help With Bluetooth Robot

by adafruit_support_bill on Fri Apr 23, 2021 6:49 am

A 4xAA pack should handle those motors. What is the data stream you are sending to the Arduino?

adafruit_support_bill
 
Posts: 81007
Joined: Sat Feb 07, 2009 10:11 am

Re: Help With Bluetooth Robot

by Fencera on Fri Apr 23, 2021 10:39 am

I’m sorry but what does that mean?

Fencera
 
Posts: 34
Joined: Tue Mar 02, 2021 9:44 am

Re: Help With Bluetooth Robot

by Fencera on Fri Apr 23, 2021 12:46 pm

i also forgot to mention they all have those yellow gearboxes on them

Fencera
 
Posts: 34
Joined: Tue Mar 02, 2021 9:44 am

Re: Help With Bluetooth Robot

by adafruit_support_bill on Fri Apr 23, 2021 2:07 pm

I’m sorry but what does that mean?

Your code is responding to characters coming in via the Serial port. What is generating those characters? If it is a program, please post the code for it.

adafruit_support_bill
 
Posts: 81007
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.