Arduino Serial.read() 只读取第二个输入
Arduino Serial.read() only reads the second input
每当我在串行监视器中输入时,Arduino 只读取第二个用户输入。
我尝试创建一个 4 个电机(A、B、C、D),当我输入速度然后输入电机字母时它会移动。例如(180a)以 180 的速度移动电机。
我不明白为什么序列号只在我第二次输入命令时读取。它有效,但这是唯一的问题。
这是我的代码:
// Pins for motor A
const int a_pwm_pin = 9;
const int a_dir1_pin = 8;
const int a_dir2_pin = 7;
// Pins for motor B
const int b_pwm_pin = 10;
const int b_dir1_pin = 11;
const int b_dir2_pin = 12;
// Pins for motor C
const int c_pwm_pin = 4;
const int c_dir1_pin = 3;
const int c_dir2_pin = 2;
// Pins for motor D
const int d_pwm_pin = 5;
const int d_dir1_pin = 6;
const int d_dir2_pin = 13;
void setup() {
// Set up pins for motor A
pinMode(a_pwm_pin, OUTPUT);
pinMode(a_dir1_pin, OUTPUT);
pinMode(a_dir2_pin, OUTPUT);
// Set up pins for motor B
pinMode(b_pwm_pin, OUTPUT);
pinMode(b_dir1_pin, OUTPUT);
pinMode(b_dir2_pin, OUTPUT);
// Set up pins for motor C
pinMode(c_pwm_pin, OUTPUT);
pinMode(c_dir1_pin, OUTPUT);
pinMode(c_dir2_pin, OUTPUT);
// Set up pins for motor D
pinMode(d_pwm_pin, OUTPUT);
pinMode(d_dir1_pin, OUTPUT);
pinMode(d_dir2_pin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 4)
{
int speed;
delay(2);
char command = Serial.read();
Serial.print(command);
switch (command)
{
case 'a': speed = Serial.parseInt();
// A Motor Movement
aforward(speed);
break;
case 'b': speed = Serial.parseInt();
// B Motor Movement
bforward(speed);
break;
case 'c': speed = Serial.parseInt();
// C Motor Movement
cforward(speed);
break;
case 'd': speed = Serial.parseInt();
// D Motor Movement
dforward(speed);
break;
default: Serial.println("");
}
}
}
// Drive forward
void aforward(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor A forward
digitalWrite(a_dir1_pin, HIGH);
digitalWrite(a_dir2_pin, LOW);
analogWrite(a_pwm_pin, speed);
}
void bforward(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor B forward
digitalWrite(b_dir1_pin, HIGH);
digitalWrite(b_dir2_pin, LOW);
analogWrite(b_pwm_pin, speed);
}
void cforward(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor C forward
digitalWrite(c_dir1_pin, HIGH);
digitalWrite(c_dir2_pin, LOW);
analogWrite(c_pwm_pin, speed);
}
void dforward(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor D forward
digitalWrite(d_dir1_pin, HIGH);
digitalWrite(d_dir2_pin, LOW);
analogWrite(d_pwm_pin, speed);
}
所以您的代码存在一些问题。首先,你的 serial.available if 语句在第 5 个字符之前不会为真,“> 4”是“大于 4”使得“大于或等于”将解决该问题。另一个问题是你的在每个“case”语句中使用 Serial.parseInt()。将它移到顶部,这样它只有 运行 一次。我创建了一个工作循环,您可以将其复制到您的项目中,一切都应该有效。祝你有美好的一天,如果有任何问题或您可能有任何问题,请发表评论。
void loop(){
if (Serial.available() >= 4)
{
delay(2);
int speed = Serial.parseInt();
char command = Serial.read();
Serial.print(command);
switch (command)
{
case 'a':
// A Motor Movement
aforward(speed);
break;
case 'b':
// B Motor Movement
bforward(speed);
break;
case 'c':
// C Motor Movement
cforward(speed);
break;
case 'd':
// D Motor Movement
dforward(speed);
break;
default: Serial.println("");
}
}
}
每当我在串行监视器中输入时,Arduino 只读取第二个用户输入。
我尝试创建一个 4 个电机(A、B、C、D),当我输入速度然后输入电机字母时它会移动。例如(180a)以 180 的速度移动电机。
我不明白为什么序列号只在我第二次输入命令时读取。它有效,但这是唯一的问题。
这是我的代码:
// Pins for motor A
const int a_pwm_pin = 9;
const int a_dir1_pin = 8;
const int a_dir2_pin = 7;
// Pins for motor B
const int b_pwm_pin = 10;
const int b_dir1_pin = 11;
const int b_dir2_pin = 12;
// Pins for motor C
const int c_pwm_pin = 4;
const int c_dir1_pin = 3;
const int c_dir2_pin = 2;
// Pins for motor D
const int d_pwm_pin = 5;
const int d_dir1_pin = 6;
const int d_dir2_pin = 13;
void setup() {
// Set up pins for motor A
pinMode(a_pwm_pin, OUTPUT);
pinMode(a_dir1_pin, OUTPUT);
pinMode(a_dir2_pin, OUTPUT);
// Set up pins for motor B
pinMode(b_pwm_pin, OUTPUT);
pinMode(b_dir1_pin, OUTPUT);
pinMode(b_dir2_pin, OUTPUT);
// Set up pins for motor C
pinMode(c_pwm_pin, OUTPUT);
pinMode(c_dir1_pin, OUTPUT);
pinMode(c_dir2_pin, OUTPUT);
// Set up pins for motor D
pinMode(d_pwm_pin, OUTPUT);
pinMode(d_dir1_pin, OUTPUT);
pinMode(d_dir2_pin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 4)
{
int speed;
delay(2);
char command = Serial.read();
Serial.print(command);
switch (command)
{
case 'a': speed = Serial.parseInt();
// A Motor Movement
aforward(speed);
break;
case 'b': speed = Serial.parseInt();
// B Motor Movement
bforward(speed);
break;
case 'c': speed = Serial.parseInt();
// C Motor Movement
cforward(speed);
break;
case 'd': speed = Serial.parseInt();
// D Motor Movement
dforward(speed);
break;
default: Serial.println("");
}
}
}
// Drive forward
void aforward(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor A forward
digitalWrite(a_dir1_pin, HIGH);
digitalWrite(a_dir2_pin, LOW);
analogWrite(a_pwm_pin, speed);
}
void bforward(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor B forward
digitalWrite(b_dir1_pin, HIGH);
digitalWrite(b_dir2_pin, LOW);
analogWrite(b_pwm_pin, speed);
}
void cforward(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor C forward
digitalWrite(c_dir1_pin, HIGH);
digitalWrite(c_dir2_pin, LOW);
analogWrite(c_pwm_pin, speed);
}
void dforward(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor D forward
digitalWrite(d_dir1_pin, HIGH);
digitalWrite(d_dir2_pin, LOW);
analogWrite(d_pwm_pin, speed);
}
所以您的代码存在一些问题。首先,你的 serial.available if 语句在第 5 个字符之前不会为真,“> 4”是“大于 4”使得“大于或等于”将解决该问题。另一个问题是你的在每个“case”语句中使用 Serial.parseInt()。将它移到顶部,这样它只有 运行 一次。我创建了一个工作循环,您可以将其复制到您的项目中,一切都应该有效。祝你有美好的一天,如果有任何问题或您可能有任何问题,请发表评论。
void loop(){
if (Serial.available() >= 4)
{
delay(2);
int speed = Serial.parseInt();
char command = Serial.read();
Serial.print(command);
switch (command)
{
case 'a':
// A Motor Movement
aforward(speed);
break;
case 'b':
// B Motor Movement
bforward(speed);
break;
case 'c':
// C Motor Movement
cforward(speed);
break;
case 'd':
// D Motor Movement
dforward(speed);
break;
default: Serial.println("");
}
}
}