目前我正在开发一个项目,我必须从 Arduino 读取脉冲并检查结果是高还是低。
我必须编写自己的代码来生成 Arduino 的高/低输出:
//Pulse Generator Arduino Code
int potPin = 2; // select the input pin for the knob
int outputPin = 13; // select the pin for the output
float val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(outputPin, OUTPUT); // declare the outputPin as an OUTPUT
Serial.begin(9600);
}
void loop() {
val = analogRead(potPin); // read the value from the k
val = val/1024;
digitalWrite(outputPin, HIGH); // sets the output HIGH
delay(val*1000);
digitalWrite(outputPin, LOW); // sets the output LOW
delay(val*1000);
}
它使用旋钮来改变脉冲之间的延迟。
我目前正在尝试使用另一个 Arduino(我们称其为“count Arduino”)读取高/低数据,只需将 2 个 Arduino 用电缆从“outputPin”连接到Arduino 计数端口。
我正在使用 digitalRead 读取端口,没有任何延迟。
//Count Arduino Code
int sensorPin = 22;
int sensorState = 0;
void setup() {
pinMode(sensorPin, INPUT);
Serial.begin(9600);
}
void loop(){
sensorState = digitalRead(sensorPin);
Serial.println(sensorState);
}
首先,它尝试每 1 秒发送一次脉冲,但结果是大量低点和高点的垃圾邮件。始终有 3 个低点和 3 个高点并重复。它甚至不接近每 1 秒 1 个,而更像是每 1 毫秒 1 个。
我不知道我做错了什么。是时间问题还是有更好的方法来检测这些变化?
最佳答案
a spam of a ton of lows and highs
...如果两个Arduino的GND没有连接,就会发生这种情况。
此外,如果串行缓冲区不会溢出,您的读数 arduino 在每个循环周期都会打印,只有几微秒。
更好的打印输出仅更改,或使用 LED 来显示正在发生的情况。
void loop(){
static bool oldState;
bool sensorState = digitalRead(sensorPin);
if (sensorState != oldState) {
Serial.println(sensorState);
oldState = sensorState;
}
}
关于c - arduino 上的脉冲生成和读出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37273326/