学习用ESP32-CAM做一个小项目(串口控制电机转动时出现乱码问题)

最近在用ESP32-CAM做一个小型机器人,使用的是arduino的IDE,因为刚刚开始,而且自己也是一个菜鸟小白,所以在这里记录一下学习的过程,以及向大家说明一下自己遇到的坑。

现在项目是进展到使用串口控制几个电机的转速。相关实物如图,比较简陋。

 

下面四个是电机。

但是目前遇到的问题就是这四个电机的运动会给串口通信带来乱码,目前还没找到具体原因。相应的代码如下:

#include<Arduino.h>

int channel1 = 1;//使用ESP32的第一个PWM通道
int channel2 = 2;//使用ESP32的第二个PWM通道
int channel3 = 3;//使用ESP32的第三个PWM通道
int channel4 = 4;//使用ESP32的第四个PWM通道

int freq = 50;//PWM频率
int resolution = 12;//PWM分辨率
int Spee;

const int motor1 = 12;//设置GPIO12口驱动电机1
const int motor2 = 13;//设置GPIO13口驱动电机2
const int motor3 = 14;//设置GPIO14口驱动电机3
const int motor4 = 15;//设置GPIO15口驱动电机4

void setup() 
{
  // put your setup code here, to run once:
    pinMode(4,OUTPUT);//用于检测程序是否执行
    // put your setup code here, to run once:
    ledcSetup(channel1, freq, resolution); // 设置通道1
    ledcAttachPin(motor1, channel1);  // 将通道1与对应的电机1引脚关联
    ledcSetup(channel2, freq, resolution); // 设置通道2
    ledcAttachPin(motor2, channel2);  // 将通道2与对应的电机2引脚关联
    ledcSetup(channel3, freq, resolution); // 设置通道2
    ledcAttachPin(motor3, channel3);  // 将通道2与对应的电机2引脚关联
    ledcSetup(channel4, freq, resolution); // 设置通道2
    ledcAttachPin(motor4, channel4);  // 将通道2与对应的电机2引脚关联
    Serial.begin(56000);
    while(Serial.read()>=0){}    
}

void forth(int T,int Speed)
{
//  for(int dutyCycle=307;dutyCycle>=250;dutyCycle=dutyCycle-10)
//  {
//    ledcWrite(channel1,dutyCycle);//设置通道1占空比
//    ledcWrite(channel2,dutyCycle);//设置通道2占空比
//    ledcWrite(channel3,dutyCycle);//设置通道3占空比
//    ledcWrite(channel4,dutyCycle);//设置通道4占空比
//    delay(30);
//    }
    ledcWrite(channel1,Speed);
    ledcWrite(channel2,Speed);
    ledcWrite(channel3,Speed);
    ledcWrite(channel4,Speed);
    digitalWrite(4,HIGH);
    delay(T);
//  for(int dutyCycle=250;dutyCycle<=307;dutyCycle=dutyCycle+10)
//  {
//    ledcWrite(channel1,dutyCycle);//设置通道1占空比
//    ledcWrite(channel2,dutyCycle);//设置通道2占空比
//    ledcWrite(channel3,dutyCycle);//设置通道3占空比
//    ledcWrite(channel4,dutyCycle);//设置通道4占空比    
//    delay(30);
//    }
}

int getSpeed()
{
  int comInt;
    if(Serial.available()>0){
      delay(100);
      comInt = Serial.parseInt();
      Serial.print("the result of Serial is:");
      Serial.println(comInt);
      delay(100);
    }
    // clear serial buffer
    while(Serial.read() >= 0){}
    delay(5);
  return comInt;
  }

void loop() 
{
  // put your main code here, to run repeatedly:
  //digitalWrite(4,HIGH);
  if(Serial.available()>0)
  {
  Spee = getSpeed();
  digitalWrite(4,LOW);
  }
  delay(5);
//  while(Serial.available()<0)

  forth(10000,Spee);

}

因为本人是个菜鸟,所以还没有养成给代码进行注释的好习惯,接下来会慢慢注意。

四个电机口分别是12、13、14、15。

在进行串口调试时,会出现如下乱码的情况:

 为了搞清楚电机转动对串口的影响,我先用单个电机转动对其进行测试。测试代码如下:

#include<Arduino.h>

int number1;
int number2;
int result;

int channel1=1;

int freq = 50;//PWM频率
int resolution = 12;//PWM分辨率
int Spee;

const int motor1 = 12;//设置GPIO12口驱动电机1

void setup() {
  // put your setup code here, to run once:
   Serial.begin(56000);
   pinMode(4,OUTPUT);
   ledcSetup(channel1, freq, resolution); // 设置通道1
   ledcAttachPin(motor1, channel1);  // 将通道1与对应的电机1引脚关联
   while(Serial.read() >= 0){}
}

void loop() {
  // put your main code here, to run repeatedly:
    if(Serial.available()>0)
    {
    delay(100);
    number1=Serial.parseInt();
    Spee=number1;
    Serial.print("number1=");
    Serial.println(number1);
    delay(10);
    while(Serial.read() >= 0){}
    }
    if(number1>300)
    {
      digitalWrite(4,HIGH);
      ledcWrite(channel1,Spee);
      }
    else
    {
      digitalWrite(4,LOW);
      ledcWrite(channel1,Spee);
      }
}

通过ESP32-CAM上的LED灯来间接反映电机旋转的问题,通过几次测试,发现单个电机的转动有时也会出现串口乱码的问题,其中原因是USB口与电脑的接触不良。如果出现该问题,可以尝试将接口拔出来再重新插入。电机转动时的振动会加剧这个原因,所以很容易出现该问题。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是基于 Arduino IDE 的 ESP32-CAM 点灯科技和蓝牙无线监控的代码示例: ``` #include <BluetoothSerial.h> #include <Wire.h> #include <Arduino.h> // 定义 LED 灯的引脚 const int ledPin = 4; // 定义蓝牙串口对象 BluetoothSerial SerialBT; void setup() { // 初始化串口和 LED 灯 Serial.begin(9600); pinMode(ledPin, OUTPUT); // 初始化蓝牙串口 SerialBT.begin("ESP32-CAM Bluetooth"); Serial.println("ESP32-CAM Bluetooth 开始工作"); } void loop() { // 检测蓝牙串口是否有数据可读 if (SerialBT.available()) { // 读取蓝牙串口的数据 char data = SerialBT.read(); // 根据读取到的数据来控制 LED 灯的开关 switch (data) { case '0': digitalWrite(ledPin, LOW); Serial.println("LED 灯已关闭"); break; case '1': digitalWrite(ledPin, HIGH); Serial.println("LED 灯已打开"); break; default: break; } } } ``` 在上述代码中,我们首先定义了 LED 灯的引脚为 4 号引脚,然后初始化了蓝牙串口对象 SerialBT。在 setup 函数中,我们初始化了串口和 LED 灯,并且开启了蓝牙串口。在 loop 函数中,我们不断检测蓝牙串口是否有数据可读,如果有数据可读,则通过读取到的数据来控制 LED 灯的开关。其中,当读取到 '0' ,表示关闭 LED 灯;当读取到 '1' ,表示打开 LED 灯。 需要注意的是,我们使用了 BluetoothSerial 库来实现蓝牙串口通信,因此需要先在 Arduino IDE 中安装该库。同,由于 ESP32-CAM 板子上没有 USB 转串口芯片,因此我们需要通过 FTDI 等 USB 转串口模块将 ESP32-CAM 与电脑连接起来,以便进行代码烧录和调试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Abyss___

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值