#define Leftmotor0 3 #define Leftmotor1 4 #define STBY 8 #define PWM 9 void setup() { // put your setup code here, to run once: Serial.begin(38400); pinMode(Leftmotor0,OUTPUT); pinMode(Leftmotor1,OUTPUT); digitalWrite(STBY,HIGH); } void loop() { // put your main code here, to run repeatedly: if(Serial.available()) { int read = Serial.parseInt(); int output=read*255/80; if(read>=0&&read<81) { digitalWrite(Leftmotor0,1); analogWrite(PWM,output); delay(500); output=0; } if(read<0) { digitalWrite(Leftmotor0,0); digitalWrite(Leftmotor1,1); analogWrite(PWM,0-output); delay(500); } } }为什么串口监视输入一次负数之后再输入正数就不转了
时间: 2024-01-25 22:02:21 浏览: 12
这可能是由于串口缓冲区中还有上一次的负数数据,导致第二次输入的正数被忽略了。为了解决这个问题,可以在读取完整个数字后,再使用Serial.read()函数清除串口缓冲区中的所有数据,以确保下一次输入不会受到任何影响。可以将以下代码添加到if(Serial.available())语句块的末尾:
while(Serial.available()>0) {
Serial.read();
}
相关问题
#define Leftmotor0 3 #define Leftmotor1 4 #define STBY 8 #define PWM 9 void setup() { // put your setup code here, to run once: Serial.begin(38400); pinMode(Leftmotor0,OUTPUT); pinMode(Leftmotor1,OUTPUT); digitalWrite(STBY,HIGH); } void loop() { // put your main code here, to run repeatedly: if(Serial.available()) { int read = Serial.parseInt(); int output=read*255/80; if(read>=0&&read<81) { digitalWrite(Leftmotor0,1); analogWrite(PWM,output); delay(500); output=0; } if(read<0) { digitalWrite(Leftmotor0,0); digitalWrite(Leftmotor1,1); analogWrite(PWM,0-output); delay(500); } } }我这个代码我想要在我每一次输入一个数据后在延时之后可以跳出我的if语句并且返回我的read 为0
您可以在每个if语句后加上`return 0;`来跳出if语句并返回read为0。修改后的代码如下:
```
#define Leftmotor0 3
#define Leftmotor1 4
#define STBY 8
#define PWM 9
void setup() {
// put your setup code here, to run once:
Serial.begin(38400);
pinMode(Leftmotor0,OUTPUT);
pinMode(Leftmotor1,OUTPUT);
digitalWrite(STBY,HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()) {
int read = Serial.parseInt();
int output=read*255/80;
if(read>=0&&read<81) {
digitalWrite(Leftmotor0,1);
analogWrite(PWM,output);
delay(500);
output=0;
return 0;
}
if(read<0) {
digitalWrite(Leftmotor0,0);
digitalWrite(Leftmotor1,1);
analogWrite(PWM,0-output);
delay(500);
return 0;
}
}
}
```
请注意,`return 0;`会直接退出`loop()`函数,因此下一次循环将从头开始执行。如果您希望程序能够保持运行状态并等待下一个输入,请将`return 0;`改为`read = 0;`。
现在我需要随机从0到80给100个数字#include<math.h> #define PWM 9 #define STBY 8 #define Leftmotor0 3 #define Leftmotor1 4 void setup() { pinMode(PWM, OUTPUT); // 设置模拟输出引脚为输出模式 digitalWrite(STBY,HIGH); pinMode(Leftmotor0,OUTPUT); pinMode(Leftmotor1,OUTPUT); } void xuanzhuan()//电机转 { digitalWrite(Leftmotor0,HIGH); digitalWrite(Leftmotor1,LOW); } void ting() { digitalWrite(Leftmotor0,LOW);//电机停 digitalWrite(Leftmotor1,LOW); } float genhao(float a) { float result; result=sqrt(a); return(result); } void loop() { // 模拟量数值,可根据需要调整(范围为 0 - 1023) float a=80; float b,c; b=genhao(a*a); c=(1-b/80)*255; analogWrite(PWM, c); // 使用PWM输出模拟信号 xuanzhuan(); }这是我的代码我想要通过随机的数字的大小来控制电机的转动,数字越小转动的速度越大
好的,您可以在 `loop()` 函数中使用 `random()` 函数生成随机数,并将其传递给 `genhao()` 函数进行计算。然后,您可以将计算结果用于控制电机的转速。以下是修改后的代码示例:
```c++
#include <math.h>
#define PWM 9
#define STBY 8
#define Leftmotor0 3
#define Leftmotor1 4
void setup() {
pinMode(PWM, OUTPUT); // 设置模拟输出引脚为输出模式
digitalWrite(STBY, HIGH);
pinMode(Leftmotor0, OUTPUT);
pinMode(Leftmotor1, OUTPUT);
}
void xuanzhuan() { // 电机转
digitalWrite(Leftmotor0, HIGH);
digitalWrite(Leftmotor1, LOW);
}
void ting() { // 电机停
digitalWrite(Leftmotor0, LOW);
digitalWrite(Leftmotor1, LOW);
}
float genhao(float a) {
float result;
result = sqrt(a);
return (result);
}
void loop() {
for (int i = 0; i < 100; i++) { // 生成 100 个随机数并控制电机
float a = random(0, 81); // 生成随机数
float b, c;
b = genhao(a * a);
c = (1 - b / 80) * 255;
analogWrite(PWM, c); // 使用 PWM 输出模拟信号
xuanzhuan(); // 电机转
delay(1000); // 持续时间
ting(); // 电机停
delay(1000); // 停止时间
}
}
```
在这个示例中,我们使用 `for` 循环生成 100 个随机数,并将它们传递给 `genhao()` 函数进行计算。然后,我们将计算结果传递给 `analogWrite()` 函数以控制电机的转速。我们还添加了一些 `delay()` 函数来控制电机的运行时间和停止时间。