So I have two servos modified to rotate 360 degrees and I have code that works odd. I want my robot to be controled both with bluetooth and infra-red. And I have an ultrasoinc sensor to messure the distance and stop if there is an object in front of it, then go back to safe distance and enable the user to control it again.
Now, the IR control works fine, and ultrasonic sensor works fine. it messures the distance and the robot goes back until safe dinstance, but the part da doesn't work is bluetooth cotrol.
The arduino recives data from my phone I use the "Bluetooth RC Controller" app and sometimes the servos move smoothly but then sometimes they just move in little bits. While the IR control works fine always, and the servos always move smoothly.
I also noticed that the servos always rotate smoothly when reciving both IR and BT data.
Here is my code :
#define bluetooth Serial
char c;
#include<Ultrasonic.h> /*include user installed library*/
Ultrasonic ultrasonic(6, 7); /*create instance of ultrasonic and define pins*/
#include <Servo.h>
Servo leftServo;
Servo rightServo;
#include <IRremote.h>
int IRpin = 11; // pin for the IR sensor
IRrecv irrecv(IRpin);
decode_results results;
void setup()
{
leftServo.attach(9); //old servo is 9 and 11
rightServo.attach(10);
delay(500); // wait for bluetooth module to start
bluetooth.begin(115200); // Bluetooth default baud is 1152
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
leftServo.write(100); // neutral position
rightServo.write(93);
}
void loop()
{
Serial.print(ultrasonic.Ranging(CM)); /* INC for inches, CM for centimeters */
Serial.println("cm");
while (ultrasonic.Ranging(CM) >= 5 )
{
if (irrecv.decode(&results) )
{
irrecv.resume();
delay(50);// Receive the next value
}
if (bluetooth.available()){
Serial.print("done");
c = (char)bluetooth.read();
}
if ((results.value == 5316027) || (c == 'F')) // up
{
leftServo.write(180);
rightServo.write(0);
delay(100);
}
if (results.value == 2747854299 || (c == 'B')) // down
{
leftServo.write(0);
rightServo.write(180);
delay(100);
}
if (results.value == 553536955 || (c == 'R')) // right
{
leftServo.write(180);
rightServo.write(85);
delay(100);
}
if (results.value == 1386468383 || (c == 'L')) // left
{
leftServo.write(95);
rightServo.write(0);
delay(100);
}
if (results.value == 3622325019 || (c == 'S')) // okay
{
leftServo.write(100);
rightServo.write(93);
delay(100);
}
}
while (ultrasonic.Ranging(CM) <= 5)
{
leftServo.write(0);
rightServo.write(180);
delay(100);
}
results.value = 3622325019;
leftServo.write(100);
rightServo.write(93);
irrecv.resume();
c = 'S';
}