Boa noite,

Estou tendo dificuldades em um projeto da faculdade, na parte da comunicação bluetooth.

Preciso realizar a movimentação de um servo e de dois motores, através de uma ponte H.

Consigo realizar a movimentação do servo normalmente, porém, ao tentar programar o acionamento do motor não estou tendo sucesso.

São dóis módulos Bluetooth se comunicando. Em um lado, (Mestre) tenho 2 Joysticks, sendo 1 para o servo e o outro para os motores. Do outro lado (Escravo), tenho 1 servo motor, 1 ponte H e 2 motores ligados na ponte.

Na programação, quando tento configurar a parte dos motores, sempre acaba misturando com os sinais do servo, e no final 1 Joystick acaba controlando tudo.

O projeto é um carrinho de controle remoto.

Segue a programação do Mestre e Esravo:

Mestre:

int state = 0;
int potValue = 0;
int pot2Value = 0;

void setup() {
Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
}
// Reading the potentiometer
potValue = analogRead(A0);
int potValueMapped = map(potValue, 0, 1023, 0, 255);
Serial.write(potValueMapped); // Sends potValue to servo motor
delay(10);

if(Serial.available() > 0){ // Checks whether data is comming from the serial port
pot2Value = Serial.read(); // Reads the data from the serial port
}
// Reading the potentiometer 2
pot2Value = analogRead(A1);
int pot2ValueMapped = map(pot2Value, 0, 1023, 0, 255);
Serial.write(pot2Value); // Sends potValue to servo motor
delay(10);

}

Escravo:

#include <Servo.h>

Servo myServo;
int motorA = 5; // velocidade motor A - de 0 a 255
int motorB = 6; // velocidade motor B - de 0 a 255
int dirA = 7; // direcao do motor A - HIGH ou LOW
int dirB = 8; // direcao do motor B - HIGH ou LOW
int state = 20;
int pot2Value = 20;

void setup() {
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
pinMode(dirA, OUTPUT);
pinMode(dirB, OUTPUT);
myServo.attach(9);
Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
}
// Controlling the servo motor
myServo.write(state);
delay(10);

if(Serial.available() > 0){ // Checks whether data is comming from the serial port
pot2Value = Serial.read(); // Reads the data from the serial port
}
//Reverso
int velocidade = map(pot2Value, 512, 0, 0, 255);
digitalWrite(dirA, LOW); // SENTIDO DE ROTACAO
digitalWrite(dirB, LOW);
analogWrite(motorA, velocidade); // VELOCIDADE
analogWrite(motorB, velocidade);

if(Serial.available() > 0){ // Checks whether data is comming from the serial port
pot2Value = Serial.read(); // Reads the data from the serial port
}
//Frente
int velocidade2 = map(pot2Value, 512, 1023, 0, 255);
digitalWrite(dirA, HIGH); // SENTIDO DE ROTACAO
digitalWrite(dirB, HIGH);
analogWrite(motorA, velocidade2); // VELOCIDADE
analogWrite(motorB, velocidade2);

}

Segue em anexo a programação.

Estou ficando sem tempo, preciso de ajuda.

Obrigado !!!!

Exibições: 114

Anexos

Responder esta

Respostas a este tópico

Bom dia, 

Envie os diagramas da sua montagem - do Master e do Slave.

RSS

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço