Bonsoir,
Voici mon projet qui consiste à contrôler un robot par vocal avec un smartphone via Bluetooth (j'ai utilisé HC-06 et le shield moteur L293D) :
#include <AFMotor.h>
#include <Servo.h>
String voice;
AF_DCMotor motor3 (3, MOTOR34_1KHZ);
AF_DCMotor motor4 (4, MOTOR34_1KHZ);
Servo myServo;
void setup()
{
Serial.begin(9600);
myServo.attach(10);
myServo.write(130);
}
void loop()
{
while (Serial.available()){
delay(10);
char c = Serial.read();
if (c == '#') {break;}
voice += c;
}
if (voice.length() > 0){
if(voice == "*avancer"){
forward_car();
}
else if(voice == "*reculer"){
back_car();
}
else if(voice == "*droite") {
right_car();
}
else if(voice == "*gauche") {
left_car();
}
else if(voice == "*stop") {
stop_car();
}
voice="";
}
}
void forward_car()
{
motor3.run(FORWARD);
motor3.setSpeed(700);
motor4.run(FORWARD);
motor4.setSpeed(700);
delay(2000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void back_car()
{
motor3.run(BACKWARD);
motor3.setSpeed(700);
motor4.run(BACKWARD);
motor4.setSpeed(700);
delay(2000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void right_car()
{
myServo.write(80);
delay(1000);
myServo.write(130);
delay(1000);
motor3.run(FORWARD);
motor3.setSpeed(190);
motor4.run(BACKWARD);
motor4.setSpeed(190);
delay(1000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void left_car()
{
myServo.write(170);
delay(1000);
myServo.write(130);
delay(1000);
motor3.run(BACKWARD);
motor3.setSpeed(190);
motor4.run(FORWARD);
motor4.setSpeed(190);
delay(1000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void stop_car ()
{
motor3.run(RELEASE);
motor4.run(RELEASE);
}
Mon problème est : quand je dis droite, le servomoteue bloque, puis les autres moteurs ne tournent pas, ensuite le bluetooth se déconnecte, mais si par exemple j'enlève les commandes du servomoteur, tout fonctionne normalement.
Ps : j'ai essayer le servomoteur seul et il fonctionne très bien.
J'espère vraiment que quelqu'un pourra m'aider. Merci d'avance.
Bonsoir,
Voici un programme qui consiste à contrôler un robot avec une télécommande via bluetooth (une application depuis un smartphone) :
#include <AFMotor.h>
AF_DCMotor motor3 (3, MOTOR34_1KHZ);
AF_DCMotor motor4 (4, MOTOR34_1KHZ);
String readString;
void setup() {
//set all the motor control pins to output
Serial.begin(9600);
}
void loop()
{
while(Serial.available())
{
delay(50);
char c = Serial.read();
readString+=c;
}
if(readString.length() > 0)
{
if(readString == "FORWARD")
{
Avancer();
}
if(readString == "BACK")
{
Reculer();
}
if(readString == "LEFT")
{
Gauche();
}
if(readString == "RIGHT")
{
Droite();
}
if(readString == "STOP")
{
Stop();
}
readString = "";
}
}
void Avancer()
{
motor3.run(FORWARD);
motor3.setSpeed(700);
motor4.run(FORWARD);
motor4.setSpeed(700);
delay(2000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Reculer()
{
motor3.run(BACKWARD);
motor3.setSpeed(700);
motor4.run(BACKWARD);
motor4.setSpeed(700);
delay(2000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Droite()
{
motor3.run(FORWARD);
motor3.setSpeed(190);
motor4.run(BACKWARD);
motor4.setSpeed(190);
delay(1000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Gauche()
{
motor3.run(BACKWARD);
motor3.setSpeed(190);
motor4.run(FORWARD);
motor4.setSpeed(190);
delay(1000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Stop()
{
motor3.run(RELEASE);
motor4.run(RELEASE);
}
et voici un autre programme permettant de contrôler le robot par vocal via bluetooth (depuis une application aussi) :
#include <AFMotor.h>
AF_DCMotor motor3 (3, MOTOR34_1KHZ);
AF_DCMotor motor4 (4, MOTOR34_1KHZ);
String readString;
String voice;
void setup() {
//set all the motor control pins to output
Serial.begin(9600);
}
void loop()
{
while (Serial.available())
{
delay(10);
char v = Serial.read();
if (v == '#')
{
break;
}
voice += v;
}
if (voice.length() > 0){
if(voice == "*avancer"){
Avancer();
}
else if(voice == "*reculer"){
Reculer();
}
else if(voice == "*droite") {
Droite();
}
else if(voice == "*gauche") {
Gauche();
}
else if(voice == "*stop") {
Stop();
}
voice="";
}
}
void Avancer()
{
motor3.run(FORWARD);
motor3.setSpeed(700);
motor4.run(FORWARD);
motor4.setSpeed(700);
delay(2000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Reculer()
{
motor3.run(BACKWARD);
motor3.setSpeed(700);
motor4.run(BACKWARD);
motor4.setSpeed(700);
delay(2000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Droite()
{
motor3.run(FORWARD);
motor3.setSpeed(190);
motor4.run(BACKWARD);
motor4.setSpeed(190);
delay(1000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Gauche()
{
motor3.run(BACKWARD);
motor3.setSpeed(190);
motor4.run(FORWARD);
motor4.setSpeed(190);
delay(1000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Stop()
{
motor3.run(RELEASE);
motor4.run(RELEASE);
}
les deux marchent normalement (séparemment), mais quand j'essaie de combiner les deux programmes (pour que l'utilisateur aie le choix du contrôle) ça ne marche pas :
#include <AFMotor.h>
AF_DCMotor motor3 (3, MOTOR34_1KHZ);
AF_DCMotor motor4 (4, MOTOR34_1KHZ);
String readString;
String voice;
void setup() {
//set all the motor control pins to output
Serial.begin(9600);
}
void loop()
{
while(Serial.available())
{
delay(50);
char c = Serial.read();
readString+=c;
}
if(readString.length() > 0)
{
if(readString == "FORWARD")
{
Avancer();
}
if(readString == "BACK")
{
Reculer();
}
if(readString == "LEFT")
{
Gauche();
}
if(readString == "RIGHT")
{
Droite();
}
if(readString == "STOP")
{
Stop();
}
readString = "";
}
while (Serial.available())
{
delay(10);
char v = Serial.read();
if (v == '#')
{
break;
}
voice += v;
}
if (voice.length() > 0){
if(voice == "*avancer"){
Avancer();
}
else if(voice == "*reculer"){
Reculer();
}
else if(voice == "*droite") {
Droite();
}
else if(voice == "*gauche") {
Gauche();
}
else if(voice == "*stop") {
Stop();
}
voice="";
}
}
void Avancer()
{
motor3.run(FORWARD);
motor3.setSpeed(700);
motor4.run(FORWARD);
motor4.setSpeed(700);
delay(2000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Reculer()
{
motor3.run(BACKWARD);
motor3.setSpeed(700);
motor4.run(BACKWARD);
motor4.setSpeed(700);
delay(2000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Droite()
{
motor3.run(FORWARD);
motor3.setSpeed(190);
motor4.run(BACKWARD);
motor4.setSpeed(190);
delay(1000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Gauche()
{
motor3.run(BACKWARD);
motor3.setSpeed(190);
motor4.run(FORWARD);
motor4.setSpeed(190);
delay(1000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void Stop()
{
motor3.run(RELEASE);
motor4.run(RELEASE);
}
Je ne comprends pas où est le problème