即时操作RC汽车项目,我使用2个DC电动机作为后轮,前方的一个伺服电动机在前面使用Nano Arduino作为微控制器,HC 06-Slave Bluetooth Bluuetooth接收器,FWD/REV和PWM的L298N模块。对于我的发射器,我使用nano arduino a操纵杆和HC-06师父。
我需要操纵杆y轴才能驱动FWD/REV和X轴进行转向。我单独测试了X轴转向的伺服器,并且在测试FWD/Rev的Y轴上的代码时也可以工作。但是,当我组合代码时,它不起作用时,我的伺服器开始发抖,我的直流电动机开始回应X和Y !!
有人可以帮助我。
这是IM使用的代码:
主代码(发射器(;
// == MASTER DEVICE - Joystick ==
//int xAxis, yAxis;
void setup() {
Serial.begin(9600); // Default communication rate of the Bluetooth
module
}
void loop() {
xAxis = analogRead(A0); // Read Joysticks X-axis
yAxis = analogRead(A1); // Read Joysticks Y-axis
// Send the values via the serial port to the slave HC-05 Bluetooth
device
Serial.write(xAxis/4); // Dividing by 4 for converting from 0 -
1023 to 0 - 256, (1 byte) range
Serial.write(yAxis/4);
delay(20);
}
从代码(仅伺服(;
#include <Servo.h>
Servo myServo;
int yAxis;
void setup() {
myServo.attach(3);
Serial.begin(9600); // Default communication rate of the Bluetooth
module
}
void loop() {
yAxis = 510 / 4;
// Read the incoming data from the Joystick, or the master
Bluetooth device
if(Serial.available() > 0){ // Checks whether data is comming from
the serial port
yAxis = Serial.read(); // Reads the data from the serial port
}
delay(10);
myServo.write(yAxis);
delay(10);
}
直流电动机FWD/REV;
的从属代码#include <Servo.h>
#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7
//int servoPin = 3;
int xAxis;
unsigned int x = 0;
unsigned int y = 0;
int motorSpeedA = 0;
int motorSpeedB = 0;
//Servo Servo1;
void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin(9600); // Default communication rate of the Bluetooth module
// Servo1.attach(servoPin);
}
void loop() {
// Default value - no movement when the Joystick stays in the center
x = 510 / 4;
y = 510 / 4;
// Read the incoming data from the Joystick, or the master Bluetooth device
while (Serial.available() >= 2) {
x = Serial.read();
// delay(10);
// y = Serial.read();
}
delay(10);
// Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below
xAxis = x*4;
// yAxis = y*4;
// Y-axis used for forward and backward control
if (xAxis < 470) {
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(xAxis, 470, 0, 0, 255);
motorSpeedB = map(xAxis, 470, 0, 0, 255);
}
else if (xAxis > 550) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(xAxis, 550, 1023, 0, 255);
motorSpeedB = map(xAxis, 550, 1023, 0, 255);
}
// If joystick stays in middle the motors are not moving
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}
我想将它们组合起来,因此其中一个轴是DC电动机FWD/REV,另一个轴是右/向左/左/左。
您必须使用发送的数据更加明确。
不要发送x = 20,y = 30作为char [2] = {20,30}。
最低限度为char [4] = {'x',20,'y',30}。
然后,您将检查您是否收到4个char的完整数据报,然后您可以检查您不会将X与y混淆。