无此用户服务 2021-10-28 17:48 采纳率: 50%
浏览 27

Arduino debug 实在想不明白,到底哪里出了问题

按照要求写了代码,然而输出和预期不符

问题在这里:

Motor Controller Interface

Write a simple motor controller interface that lets the operator select direction, speed and steering for a robot using Pulse Width Modulation to control external motors.
Use the following inputs to the Serial Monitor:

f = forwards
b = backwards
l = turn left
r = turn right
0 = stop
1 - 5 = set speed from 10% to 50% duty cycle

Use appropriate output messages on the Serial Monitor to confirm each command and set the PWM pins to the correct duty cycle

题目的预期输出是这样:

img

这里是我的代码:


// -----------------------------------------
// Global Variables
// -----------------------------------------

// Left motor forward
int Left_motor_go = 8;
// Left motor backward
int Left_motor_back = 9;

// Right motor forward
int Right_motor_go = 10;
// Right motor backward
int Right_motor_back = 11;

// Speed range
int speed1 = 25;
int speed2 = 51;
int speed3 = 76;
int speed4 = 102;
int speed5 = 127;

// A string to store the instructions from the input
char input[1000];

// ----------------------------------------------------
// The setup() method runs once, when the sketch starts
// ----------------------------------------------------
void setup() {
  
  // Initialize the serial communications
  Serial.begin(9600);

  // Print the program details
  Serial.println("-------------------------------------");
  Serial.println("Program: Name"); 
  Serial.println("Initializing ...");

  // Initialize motor drive IO as output mode
  // PIN 8 (PWM)
  pinMode(Left_motor_go, OUTPUT);
  // PIN 9 (PWM)
  pinMode(Left_motor_back, OUTPUT);
  // PIN 10 (PWM)
  pinMode(Right_motor_go, OUTPUT);
  // PIN 11 (PWM)
  pinMode(Right_motor_back, OUTPUT);

  // Print the motor pin configuration for wiring
  Serial.println("Right Motor Pin 1 = 10");
  Serial.println("Right Motor Pin 2 = 11");
  Serial.println("Left Motor Pin 1 = 8");
  Serial.println("Left Motor Pin 1 = 9");

  // Initialization completed successfully
  Serial.println("Initialization complete");
}

// A function to control the motor to move forward
void run(int time, int run_speed) {

  // Right motor forward
  digitalWrite(Right_motor_go, HIGH);
  digitalWrite(Right_motor_back, LOW);

  // Control the right motor speed
  analogWrite(Right_motor_go, run_speed);
  analogWrite(Right_motor_back, 0);

  // Left motor forward
  digitalWrite(Left_motor_go, HIGH);
  digitalWrite(Left_motor_back, LOW);

  // Control the left motor speed
  analogWrite(Left_motor_go, run_speed);
  analogWrite(Left_motor_back, 0);

  // The running time of the function
  delay(time * 100);
  
}

// A function to control the motor to move backward
void back(int time, int back_speed) {

  // Right motor backward
  digitalWrite(Right_motor_go, LOW);
  digitalWrite(Right_motor_back, HIGH);

  // Control the right motor speed
  analogWrite(Right_motor_go, 0);
  analogWrite(Right_motor_back, back_speed);

  // Left motor backward
  digitalWrite(Left_motor_go, LOW);
  digitalWrite(Left_motor_back, HIGH);

  // Control the left motor speed
  analogWrite(Left_motor_go, 0);
  analogWrite(Left_motor_back, back_speed);

  // The running time of the function
  delay(time * 100);
  
}

// A function to control the motor to turn left
void left(int time, int left_speed) {

  // Right motor forward
  digitalWrite(Right_motor_go, HIGH);
  digitalWrite(Right_motor_back, LOW);

  // Control the right motor speed
  analogWrite(Right_motor_go, left_speed);
  analogWrite(Right_motor_back, 0);

  // Left motor not working
  digitalWrite(Left_motor_go,LOW);
  digitalWrite(Left_motor_back,LOW);

  // Control the left motor speed
  analogWrite(Left_motor_go, 0);
  analogWrite(Left_motor_back, 0);

  // The running time of the function
  delay(time * 100);
  
}

// A function to control the motor to turn right
void right(int time, int right_speed) {

  // Right motor not working
  digitalWrite(Right_motor_go,LOW);
  digitalWrite(Right_motor_back,LOW);

  // Control the right motor speed
  analogWrite(Right_motor_go, 0);
  analogWrite(Right_motor_back, 0);

  // Left motor forward
  digitalWrite(Left_motor_go, HIGH);
  digitalWrite(Left_motor_back, LOW);

  // Control the left motor speed
  analogWrite(Left_motor_go, right_speed);
  analogWrite(Left_motor_back, 0);

  // The running time of the function
  delay(time * 100);

}

// A function to make the mortor stop
void stop(int time) {

  // Stop the right motor
  digitalWrite(Right_motor_go,LOW);
  digitalWrite(Right_motor_back,LOW);

  // Stop the left motor
  digitalWrite(Left_motor_go,LOW);
  digitalWrite(Left_motor_back,LOW);

  // The running time of the function
  delay(time * 100);
  
}

// A function to control the speed of the motor and print the current speed
void control_and_print_speed(char input[], int motor_speed) {

  int i = 0;
  while (i <= 10) {
    if (input[i] == 1) {
      motor_speed = speed1;
      Serial.println("Speed = 10%");
    } else if (input[i] == 2) {
      motor_speed = speed2;
      Serial.println("Speed = 20%");
    } else if (input[i] == 3) {
      motor_speed = speed3;
      Serial.println("Speed = 30%");
    } else if (input[i] == 4) {
      motor_speed = speed4;
      Serial.println("Speed = 40%");
    } else if (input[i] == 5) {
      motor_speed = speed5;
      Serial.println("Speed = 50%");
    }
    i++;
  }
  
}

// A function to control the movement and print the current movement
void control_and_print_movement(char input[], int motor_speed) {

  int i = 0;
  while (i <= 10) {
    if (input[i] == "f") {
      run(10, motor_speed);
      Serial.println("Forwards");
    } else if (input[i] == "b") {
      back(10, motor_speed);
      Serial.println("Backwards");
    } else if (input[i] == "l") {
      left(10, motor_speed);
      Serial.println("Turn left");
    } else if (input[i] == "r") {
      right(10, motor_speed);
      Serial.println("Turn right");
    } else if (input[i] == 0) {
      stop(10);
      Serial.println("Stop");
    }
    i++;
  }
  
}

// ------------------------------------------
// The loop() method runs over and over again
// ------------------------------------------
void loop() {

  // See if there's incoming serial data
  if  (Serial.available() > 0) {

    // Wait for the completion of data transferring
    delay(100);

    // Store the input in the string
    int index = 0;
    while (Serial.available() > 0) {
      input[index] = Serial.read();
      index++;
    }

    // The speed of the motor
    int motor_speed = 0;

    // Call the function to control speed
    control_and_print_speed(input, motor_speed);

    // Call the function to control movement
    control_and_print_movement(input, motor_speed);

    
    
  }

  
}

我现在的输出会在打一个Speed = 40%之后反复输出stop,不论我输入什么都是这样。请问是哪里的问题?如何修改?谢谢!

  • 写回答

2条回答 默认 最新

  • 有问必答小助手 2021-10-29 18:52
    关注

    你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答


    本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。

    评论

报告相同问题?

问题事件

  • 创建了问题 10月28日

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办