按照要求写了代码,然而输出和预期不符
问题在这里:
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
题目的预期输出是这样:
这里是我的代码:
// -----------------------------------------
// 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,不论我输入什么都是这样。请问是哪里的问题?如何修改?谢谢!