0

Currently I'm working on a project where I have to read out pulses from a Arduino and check if the result is High or Low.

I had to write my own code to generate the high/low output from the 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);
}

It uses a knob to change the delay between the pulses.

Im currently trying to read the high/low data with another Arduino (Lets call this one the "count Arduino") by simply connecting the 2 with the a cable from the "outputPin" to a port on the count Arduino.

I'm using digitalRead to read the port without any delay.

//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);
}

First it tried with a pulse every 1 second but the result was a spam of a ton of lows and highs. Always 3 Lows and 3 highs and repeating. It wasn’t even close to one every 1 second but more like 1 every 1 millisecond.

I cant figure out what i'm doing wrong. Is it timing issue or is there a better way to detect these changes?

4

1 回答 1

1

大量低点和高点的垃圾邮件

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

此外,如果串行缓冲区不会溢出,您的阅读 arduino 会在每个循环周期(仅几微秒)打印。

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

void loop(){
    static bool oldState;
    bool sensorState = digitalRead(sensorPin);
    if (sensorState != oldState) {
       Serial.println(sensorState);
       oldState = sensorState;
    }
}
于 2016-05-17T13:43:15.973 回答