首页 文章

arduino上的脉冲生成和读出

提问于
浏览
0

目前我正在开展一个项目,我必须从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 ") by simply connecting the 2 with the a cable from the " 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

    一堆低点和高点的垃圾邮件

    ...如果两个Arduinos的GND未连接,则会发生 .

    此外,如果串行缓冲区不会溢出,您的读取arduino将在每个循环周期打印,仅限几微秒 .

    更好的打印输出更改,或使用LED来显示正在发生的事情 .

    void loop(){
        static bool oldState;
        bool sensorState = digitalRead(sensorPin);
        if (sensorState != oldState) {
           Serial.println(sensorState);
           oldState = sensorState;
        }
    }
    

相关问题