1701
- 收藏
- 点赞
- 分享
- 举报
帮我看一下Arduino的代码问题
想做一个蓝牙的测距小车,用的是osoyoo家的小车,程序代码在下面。蓝牙和测距的代码各自分开都是可以实行的,但是加在一起蓝牙就控制不了小车了,不明白自己的编程那里写错了,望大佬指导。
这是测距的代码(最下面的是总代码)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
const int TrigPin = A0;
const int EchoPin = A1;
float cm;
void setup()
{
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop()
{
digitalWrite(TrigPin, LOW); /
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //
cm = (int(cm * 100.0)) / 100.0; //
delay(1000);
lcd.init(); // initialize the lcd
lcd.backlight(); //Open the backlight
lcd.print("Distance:");
lcd.setCursor(0, 1);
lcd.print(cm);
lcd.print("cm");}
在void loop加入了do_Distance_Tick()以后蓝牙功能就无法实现了
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
const int TrigPin = A0;
const int EchoPin = A1;
1. float cm;
#define IN1 7 //K1、K2 motor direction
#define IN2 8 //K1、K2 motor direction
#define IN3 9 //K3、K4 motor direction
#define IN4 10 //K3、K4 motor direction
#define ENA 5 // Needs to be a PWM pin to be able to control motor speed ENA
#define ENB 6 // Needs to be a PWM pin to be able to control motor speed ENA
#define M_SPEED1 150 //motor speed
#define M_SPEED2 150 //motor speed
#define MAX_PACKETSIZE 32 //Serial receive buffer
char buffUART[MAX_PACKETSIZE];
unsigned int buffUARTIndex = 0;
unsigned long preUARTTick = 0;
bool stopFlag = true;
bool JogFlag = false;
uint16_t JogTimeCnt = 0;
uint32_t JogTime=0;
enum DS
{
MANUAL_DRIVE,
AUTO_DRIVE_LF, //line follow
}Drive_Status=MANUAL_DRIVE;
enum DN
{
GO_ADVANCE,
GO_LEFT,
GO_RIGHT,
GO_BACK,
STOP_STOP,
DEF
}Drive_Num=DEF;
/*motor control*/
void go_back(int t) //motor rotate clockwise -->robot go ahead
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4,HIGH);
delay(t);
}
void go_ahead(int t) //motor rotate counterclockwise -->robot go back
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4,LOW);
delay(t);
}
void go_stop() //motor brake -->robot stop
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4,LOW);
}
void turn_left(int t) //left motor rotate clockwise and right motor rotate counterclockwise -->robot turn right
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(t);
}
void turn_right(int t) //left motor rotate counterclockwise and right motor rotate clockwise -->robot turn left
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(t);
}
/*set motor speed */
void set_motorspeed(int lspeed,int rspeed) //change motor speed
{
analogWrite(ENA,lspeed);//lspeed:0-255
analogWrite(ENB,rspeed);//rspeed:0-255
}
//WiFi / Bluetooth through the serial control
void do_Uart_Tick()
{
char Uart_Date=0;
if(Serial.available())
{
size_t len = Serial.available();
uint8_t sbuf[len + 1];
sbuf[len] = 0x00;
Serial.readBytes(sbuf, len);
//parseUartPackage((char*)sbuf);
memcpy(buffUART + buffUARTIndex, sbuf, len);//ensure that the serial port can read the entire frame of data
buffUARTIndex += len;
preUARTTick = millis();
if(buffUARTIndex >= MAX_PACKETSIZE - 1)
{
buffUARTIndex = MAX_PACKETSIZE - 2;
preUARTTick = preUARTTick - 200;
}
}
if(buffUARTIndex > 0 && (millis() - preUARTTick >= 100))//APP send flag to modify the obstacle avoidance parameters
{ //data ready
buffUART[buffUARTIndex] = 0x00;
Uart_Date=buffUART[0];
buffUARTIndex = 0;
}
switch (Uart_Date) //serial control instructions
{
case '2':
Drive_Status=MANUAL_DRIVE; Drive_Num=GO_ADVANCE;Serial.println("forward"); break;
case '4':
Drive_Status=MANUAL_DRIVE;Drive_Num=GO_LEFT;Serial.println("turn left");break;
case '6':
Drive_Status=MANUAL_DRIVE;Drive_Num=GO_RIGHT;Serial.println("turn right");break;
case '8':
Drive_Status=MANUAL_DRIVE;Drive_Num=GO_BACK;Serial.println("go back");break;
case '5':
Drive_Status=MANUAL_DRIVE;Drive_Num=STOP_STOP;Serial.println("stop");break;
default:break;
}
}
//robot motor control
void do_Drive_Tick()
{
if(Drive_Status == MANUAL_DRIVE)
{
switch (Drive_Num)
{
case GO_ADVANCE:
set_motorspeed(255,255);
go_ahead(15);
JogFlag = true;
JogTimeCnt = 3;
JogTime=millis();
break;
case GO_LEFT:
set_motorspeed(100,100);
turn_left(2);
JogFlag = true;
JogTimeCnt = 1;
JogTime=millis();
break;
case GO_RIGHT:
set_motorspeed(100,100);
turn_right(2);
JogFlag = true;
JogTimeCnt = 1;
JogTime=millis();
break;
case GO_BACK:
set_motorspeed(150,150);
go_back(10);
JogFlag = true;
JogTimeCnt = 1;
JogTime=millis();
break;
case STOP_STOP:
go_stop();
JogTime = 0;
break;
default:
break;
}
Drive_Num=DEF;
//keep the car running for 100ms
if(millis()-JogTime>=100)
{
JogTime=millis();
if(JogFlag == true)
{
stopFlag = false;
if(JogTimeCnt <= 0)
{
JogFlag = false; stopFlag = true;
}
JogTimeCnt--;
}
if(stopFlag == true)
{
JogTimeCnt=0;
go_stop();
}
}
}
}
void do_Distance_Tick()
{
digitalWrite(TrigPin, LOW); /TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //
cm = (int(cm * 100.0)) / 100.0; //小数点
delay(1000);
lcd.init(); // initialize the lcd
lcd.backlight(); //Open the backlight
lcd.print("Distance:");
lcd.setCursor(0, 1);
lcd.print(cm);
lcd.print("cm");
}
void setup() {
/******L298N******/
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
go_stop();
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop() {
do_Uart_Tick();
do_Drive_Tick();
do_Distance_Tick();
}
我来回答
回答0个
时间排序
认可量排序
暂无数据
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片![alt](url)
相关问答
-
2018-12-13 09:24:51
-
2018-10-24 14:49:42
-
2019-01-12 11:01:48
-
2018-12-16 11:25:33
-
2021-02-24 15:25:26
-
2018-11-14 17:19:34
-
2021-02-22 14:30:32
-
2018-12-07 10:16:21
-
2019-01-31 14:26:27
-
2018-12-27 16:40:39
-
2018-12-21 13:58:48
-
2018-12-17 15:46:14
-
2018-12-20 11:24:37
-
2018-12-10 13:42:26
-
2018-11-29 10:36:53
-
2019-01-07 16:38:30
-
2018-12-21 09:37:32
-
2018-11-09 13:55:00
-
2018-12-06 16:35:18
无更多相似问答 去提问
点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
悬赏问答
-
50如何获取vpss chn的图像修改后发送至vo
-
5FPGA通过Bt1120传YUV422数据过来,vi接收不到数据——3516dv500
-
50SS928 运行PQtools 拼接 推到设备里有一半画面会异常
-
53536AV100的sample_vdec输出到CVBS显示
-
10海思板子mpp怎么在vi阶段改变视频数据尺寸
-
10HI3559AV100 多摄像头同步模式
-
9海思ss928单路摄像头vio中加入opencv处理并显示
-
10EB-RV1126-BC-191板子运行自己编码的程序
-
10求HI3519DV500_SDK_V2.0.1.1
-
5有偿求HI3516DV500 + OV5647驱动
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认