串口不显示,小车不动

2019-07-236030
AI 快速预览详细 收起
本文介绍了如何解决DFRobot GoBLE蓝牙遥控小车串口不显示且小车不动的问题。通过检查串口输出和按钮状态,可以定位问题并进行调试。具体步骤包括初始化串口、读取摇杆和按钮状态,并通过串口输出这些信息。此外,还提供了根据按钮状态控制小车运动的具体代码示例。
#include <DFMobile.h>

#include <Metro.h>
#include "GoBLE.h"
int joystickX, joystickY;
int buttonState[7];
Metro motorMetro = Metro(50);
int LeftWheelSpeed;
int RightWheelSpeed;
DFMobile Robot (4, 5, 7, 6);
void setup(){
  Goble.begin();
  Serial.begin(115200);
}

void loop() {  
  if(Goble.available()){
    joystickX = Goble.readJoystickX();
    joystickY = Goble.readJoystickY();
   
    buttonState[SWITCH_UP]     = Goble.readSwitchUp();
    buttonState[SWITCH_DOWN]   = Goble.readSwitchDown();
    buttonState[SWITCH_LEFT]   = Goble.readSwitchLeft();
    buttonState[SWITCH_RIGHT]  = Goble.readSwitchRight();
    buttonState[SWITCH_SELECT] = Goble.readSwitchSelect();
    buttonState[SWITCH_START]  = Goble.readSwitchStart();
   
    Serial.print("Joystick Value: ");
    Serial.print(joystickX);
    Serial.print("  ");
    Serial.println(joystickY);
   
    for (int i = 1; i < 7; i++) {
      Serial.print("Button ID: ");
      Serial.print(i);
      Serial.print("\t State: ");
      if (buttonState == PRESSED)   Serial.println("Pressed!");
      if (buttonState == RELEASED)  Serial.println("Released!");
      int speedPin_M1 = 5;   
int speedPin_M2 = 6;  
int directionPin_M1 = 4;
int directionPin_M2 = 7;
      if (buttonState[SWITCH_UP]) {
  digitalWrite(directionPin_M2,LOW);   
  digitalWrite(directionPin_M1,HIGH);}
   if (buttonState[SWITCH_DOWN]) {
  digitalWrite(directionPin_M2,HIGH);   
  digitalWrite(directionPin_M1,LOW);}
   if (buttonState[SWITCH_LEFT]) {
  digitalWrite(directionPin_M2,HIGH);   
  digitalWrite(directionPin_M1,HIGH);}
   if (buttonState[SWITCH_RIGHT]) {
  digitalWrite(directionPin_M2,LOW);   
  digitalWrite(directionPin_M1,LOW);}

   if (motorMetro.check()) {
        if (joystickX > 200 ||joystickX < -200) {    //when joystick X is pushed up
          LeftWheelSpeed = joystickX;                //Both wheels move forward
          RightWheelSpeed = joystickX;
          Robot.Speed (LeftWheelSpeed, RightWheelSpeed);
          Serial.print("move ");

        }

        else if (joystickY > 200 ||joystickY < -200) {      /file:///C:\Users\薛磊\AppData\Roaming\Tencent\QQTempSys\@IR3P(8S$C$Z$TY~5I{QEPC.gif right
          LeftWheelSpeed = joystickY - 80;
          RightWheelSpeed = -joystickY- 80;
          Robot.Speed(LeftWheelSpeed, RightWheelSpeed);
          Serial.print("twist ");

        }

        else if (joystickX == 0 && joystickY== 0) {
          Robot.Speed(0, 0);
      }
    }
   
  }
  }
}

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(5)
gada888
gada888
数据线没连好,或者代码写的有问题。
shzrzxlee
shzrzxlee
DFMobile Robot (4, 5, 7, 6);
KIKI
KIKI
什么小车?
shzrzxlee
shzrzxlee
串口不显示 要么USB接口接触不好,第一次用的话是驱动没装
shzrzxlee
shzrzxlee
串口不显示 不能上传的
- 没有更多了 -