Search code examples
carduinoarduino-unopulse

Pulse generation and readout on arduino


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?


Solution

  • 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;
        }
    }