0
votes

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?

1
The question in your last sentence: "is there a better way to detect these changes?" Yes, by detecting changes, not reporting the level as you seem to be doing, although you don't give many clues. Your continuous serial output of the sensor state is going to wreck the perceived timing of the input state.Weather Vane
If you transmit a '0' when the input goes low, and a '1' when it goes high, at 9600 baud you should be able to track an input pulse of up to about 1 kHz.Weather Vane
I have to track around 10 pulses each second. So 10 Hertz would already be great. Is an interrupt based solution an option on the Arduino. Can i get an interrupt if a pin changes from high to low?Joris Mathijssen
You should have the grounds connected, as well as the port pins. At 10 Hz, for a student project there is no need to use interrupts. Remember the previous input state and act when it changes. A better way to measure pulse width is to use an input to capture a timer count. Some people like to use a one-shot timer for each pulse, my preferred method is a free run counter, subtracting the previously captured value (saved in an unsigned variable). Again, at that rate you can just read the counter yourself when the input state changes, without interrupts or hardware capture. Get that working first.Weather Vane

1 Answers

1
votes

a spam of a ton of lows and highs

... happens if the GND of the two Arduinos are not connected.

Also, your reading arduino prints at every loop cycle, which were a few microseconds only, if the Serial buffer would not overflow.

Better printout changes only, or use a led to show what's happening.

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