Hello, I am trying to make a bluetooth RC car using an Arduino Uno, a motor shield made by OSEPP which only works with the v1 library, and an HMS Soft bluetooth module, which is communicated to via the Serial app that can be used on a mobile phone. It uses 4 DC Motors. When I use code without using the bluetooth module, the motors will work, but when I use code with the bluetooth module, I can communicate with the module via my phone, but my wheels will not turn. I believe this might be part of the fact that the Arduino Uno may not be able to handle the shield and the bluetooth, and if there is a solution to this I would like to find out. I have my code for the bluetooth, although the bluetooth code has been commented out. There is another code with functions for the code.
Bluetooth code:
#include <NewPing.h>
#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
#include <AFMotor.h>
//4 wheel drive shield motors
AF_DCMotor frontleft(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor backleft(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor backright(3, MOTOR12_1KHZ); // create motor #2, 64KHz pwm
AF_DCMotor frontright(4, MOTOR12_1KHZ); // create motor #2, 64KHz pwm
//ultrasonic
#define TRIGGER_PIN 3
#define ECHO_PIN 9
#define MAX_DISTANCE 400
//neopixel lights
#define PIN 13
#define NUMPIXELS 6
#include <Servo.h>
#define servohand 10
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
Servo ultrasonic;
int rightspeed = 200;
int leftspeed = 200;
int randright = random(0, 244);
int randleft = random(0, 244);
int response = 1;
int ledPin = 13;
int state = 0;
int flag = 0;
boolean condit = true;
int trigPin = 4;
int echoPin = 7;
long duration;
int distance;
void setup()
{
Serial.begin (9600);
// motors
frontleft.setSpeed(200); // set the speed to 200/255
backleft.setSpeed(200); // set the speed to 200/255
frontright.setSpeed(200); // set the speed to 200/255
backright.setSpeed(200); // set the speed to 200/255
//LED Test
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
//Ultrasonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//neopixels
pixels.begin();
//ultrasonic servo
ultrasonic.attach(servohand);
}
void loop()
{
//frontleft.run(FORWARD);
//frontleft.setSpeed(200);
//frontright.run(FORWARD); // the other way
//backleft.run(FORWARD);
//backright.run(FORWARD);
frontleft.run(BACKWARD);
frontright.run(BACKWARD); // the other way
backleft.run(BACKWARD);
backright.run(BACKWARD);
delay(1000);
/*
if (Serial.available() > 0)
{
state = Serial.read();
flag = 0;
}
//use switch case in next version
if (state == '0')
{
condit = false;
digitalWrite(ledPin, LOW);
if (flag == 0)
{
Serial.println("STOP");
flag = 1;
motorsOff();
lights(0, 0, 0);
//turns off motors and neopixel, can be seen as a reset button on the controller
}
}
else if (state == '1')
{
lights(200, 0, 0);
if (flag == 0)
{
Serial.println("forward");
flag = 1;
forward(); //makes the robot go forward
}
}
else if (state == '2')
{
digitalWrite(ledPin, HIGH);
if (flag == 0)
{
Serial.println("backward");
condit = true;
flag = 1;
lights(200, 200, 0);
reverse(); // robot will go backward
}
}
else if (state == '3')
{
digitalWrite(ledPin, HIGH);
if (flag == 0)
{
Serial.println("right turn");
condit = true;
flag = 1;
lights(0, 200, 0);
rightTurn();
}
}
else if (state == '4')
{
digitalWrite(ledPin, HIGH);
if (flag == 0)
{
Serial.println("random forward");
condit = true;
flag = 1;
lights(200, 0, 200);
randRun();
}
}
else if (state == '5')
{
digitalWrite(ledPin, HIGH);
Serial.println("free run");
if (flag == 0)
{
lights(0, 0, 200);
freeRun();
}
}
else if (state == '6')
{
digitalWrite(ledPin, HIGH);
if (flag == 0)
{
Serial.println("lights");
condit = true;
flag = 1;
lights(0, 100, 200);
}
}
else if (state == '7')
{
digitalWrite(ledPin, HIGH);
Serial.println("free view");
if (flag == 0)
{
lights(0, 0, 200);
servo();
}
}
*/
}
Functions used with this code:
#include <AFMotor.h>
//move forward !!Reason for 154 and 180 being the motor speeds is due to the variation in the motors. For a perfectly equal set of motors will be able to work properly with equal speed settings.
//adjust the speeds of the motors according to your needs.
void forward()
{
//forward motion
// frontleft.run(FORWARD);
// frontright.run(FORWARD); // turn it on going forward
// backleft.run(FORWARD);
// backright.run(FORWARD);
uint8_t i;
Serial.print("tick");
frontright.run(FORWARD);
for (i=0; i<255; i++) {
frontright.setSpeed(i);
delay(10);
}
}
//move in reverse
void reverse()
{
frontleft.run(BACKWARD);
frontright.run(BACKWARD); // the other way
backleft.run(BACKWARD);
backright.run(BACKWARD);
}
//turn right indefinately
void rightTurn() // similar to forward test
{
// set up both motors for reverse direction
frontleft.run(FORWARD);
frontright.run(RELEASE); // the other way
backleft.run(RELEASE);
backright.run(FORWARD);
}
//turn off all motors
void motorsOff()
{
frontleft.run(RELEASE);
frontright.run(RELEASE); // the other way
backleft.run(RELEASE);
backright.run(RELEASE);
}
//random forward
void randRun()
{
frontleft.run(FORWARD);
frontright.run(FORWARD); // the other way
backleft.run(FORWARD);
backright.run(FORWARD);
}
//free run with ultrasonic
void freeRun()
{
frontleft.run(FORWARD);
frontright.run(FORWARD); // turn it on going forward
backleft.run(FORWARD);
backright.run(FORWARD);
if (sonar.ping_cm() < 5)
{
reverse();
delay(200);
rightTurn();
delay(200);
}
}
//neopixels
void lights(int RLED, int GLED, int BLED)
{
for(int i = 0; i < 6; i++)
{
pixels.setPixelColor(i, pixels.Color(RLED, GLED, BLED));
pixels.show();
}
}
//experimental
void dance()
{
frontleft.run(FORWARD);
frontright.run(FORWARD); // the other way
backleft.run(FORWARD);
backright.run(FORWARD);
}
void servo()
{
ultrasonic.write(45);
delay(500);
ultrasonic.write(180);
delay(500);
}