【arduino代码纠错】_手机控制蓝牙小车
本帖最后由 simon_xu 于 2015-5-6 19:30 编辑大家好,我是新手,最近在自学做一辆用手机控制的蓝牙。
在网上找了一个蓝牙控制小车的代码(带加速功能),但验证时显示代码有误。错误信息:
Bluetooth_Car.ino: In function 'void Speed_Change(int)':
Bluetooth_Car.ino:139:27: error: wrong type argument to unary plus
希望大家能帮我看下,代码哪里有问题,谢谢大家!
附上代码:
/*********************************
程序名:蓝牙小车
/*********************************/
const int ENA = 8;
const int ENB = 9;
const int IN1 = 10;
const int IN2 = 11;
const int IN3 = 12;
const int IN4 = 13;
int uartready = 0;
int bluetooth_info = 0;
unsigned int car_flag = 0;
unsigned char uart_data_number = 0;
unsigned char uart_data;
unsigned char uart_test_data;
char UART_DATA;
int speed_data = 0;
void Forward(void)
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void Back(void)
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void Stop(void)
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void Right_Forward(void)
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void Right_Back(void)
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void Right_Place(void)
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void Lift_Forward(void)
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void Lift_Back(void)
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void Lift_Place(void)
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void setup()
{
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
Stop();
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
Serial.begin(9600);
}
/*****************************************************************************
电机调速
/****************************************************************************/
void Speed_Change(int speed)
{
switch (speed)
{
case 0:
analogWrite(ENA, 25);
analogWrite(ENB, 25);
break;
case 1:
analogWrite(ENA, 50);
analogWrite(ENB, 50);
break;
break;
case 2:
analogWrite(ENA, 75);
analogWrite(ENB, 75);
break;
case 3:
analogWrite(ENA, 100);
analogWrite(ENB, 100);
break;
case 4:
analogWrite(ENA, 125);
analogWrite(ENB, 125);
break;
case 5:
analogWrite(ENA, 150);+
analogWrite(ENB, 150);
break;
case 6:
analogWrite(ENA, 175);
analogWrite(ENB, 175);
break;
case 7:
analogWrite(ENA, 200);
analogWrite(ENB, 200);
break;
case 8:
analogWrite(ENA, 255);
analogWrite(ENB, 255);
break;
default: break;
}
}
/*****************************************************************************
数据处理函数
/****************************************************************************/
void Data_Process(void)
{
if (uart_data == 'R' && uart_data == 'T') //判断是否为小车控制
{
switch (uart_data) //判断指令
{
case '1': car_flag = 1; break;
case '2': car_flag = 2; break;
case '3': car_flag = 3; break;
case '4': car_flag = 4; break;
case '5': car_flag = 5; break;
case '6': car_flag = 6; break;
case '7':
speed_data = speed_data + 1;
if (speed_data >= 8)
speed_data = 8;
Speed_Change(speed_data);
break;
case '8': car_flag = 8; break;
case '9':
speed_data = speed_data - 1;
if (speed_data <= 0)
speed_data = 0;
Speed_Change(speed_data);
break;
default: break;
}
}
uart_data = 0;
uart_data = 0;
uart_data = 0;
uart_data = 0;
}
void loop()
{
while (Serial.available() > 0)
{
UART_DATA = Serial.read();
if (uartready == 1)
{
uart_data = UART_DATA;
uart_data_number++;
if (uart_data == 'F')
{
Data_Process();
uart_data_number = 0;
}
}
//蓝牙数据标识符
if (UART_DATA == '+')
{
bluetooth_info = 1;
uart_data_number = 0;
}
//蓝牙数据处理
if (bluetooth_info == 1)
{
uart_test_data = UART_DATA;
uart_data_number++;
if (uart_test_data == 'C' && uart_test_data == 'T' && uart_test_data == 'E' && uart_test_data == 'D' && uart_test_data == '\r' && uart_test_data == '\n')
{
uart_data_number = 0;
uartready = 1;
bluetooth_info = 0;
}
}
}
switch (car_flag) //判断指令
{
case 1: Lift_Forward(); break;
case 2: Forward(); break;
case 3: Right_Forward(); break;
case 4: Lift_Back(); break;
case 5: Stop(); break;
case 6: Right_Back(); break;
case 8: Back(); break;
default: break;
}
}
错误发出来看看 楼上建议你把错误截图发出来 好帮助鉴别 编译能过,除了你贴的代码里多了个+号 Ricky 发表于 2015-5-6 10:15
错误发出来看看
Arduino:1.6.3 (Windows 7), 板:"Arduino Uno"
Bluetooth_Car.ino: In function 'void Speed_Change(int)':
Bluetooth_Car.ino:139:27: error: wrong type argument to unary plus
编译有误。
大连林海 发表于 2015-5-6 17:51
楼上建议你把错误截图发出来 好帮助鉴别
错误信息是这两条,谢谢啦!
Bluetooth_Car.ino: In function 'void Speed_Change(int)':
Bluetooth_Car.ino:139:27: error: wrong type argument to unary plus
Cain 发表于 2015-5-6 18:16
编译能过,除了你贴的代码里多了个+号
谢谢,果然是这个原因,太感谢啦
:kiss:
页:
[1]