Testing a pin to see if it is HIGH or LOW

I am new to Arduino programming and I am having problems with testing. I have attached the code.

int squareWavePinOutput = 5;
int squareWavePinInput = 7;

void setup()
{
Serial.begin(9600);
pinMode(squareWavePinOutput, OUTPUT);
pinMode(squareWavePinInput, INPUT);
}

void loop()
{
digitalWrite(squareWavePinOutput, HIGH);
delay(100);
digitalWrite(squareWavePinOutput, LOW);
delay(100);

int val = digitalRead(squareWavePinOutput);

if(val == HIGH)
{
Serial.println("1");
}

if(val == LOW)
{
Serial.println("0");

}
}

Shouldn't need to test it, as you just wrote it.

void loop()
  {
    digitalWrite(squareWavePinOutput, HIGH);
    Serial.println("1");

    delay(100);
    digitalWrite(squareWavePinOutput, LOW);
    Serial.println("0");

    delay(100);

  }

Otherwise it will always read 0 as that was the last thing you wrote out.

Don't forget code tags next time (the # buttton on the menu).

And welcome to the forum :slight_smile:

The reason that I am trying to test it is because I am trying to send that pulsing signal from a transmitter to a receiver and I want to determine how many times that the receiver gets a HIGH. This is going to be used to measure distance using time of flight.

If you want to test an asynchronous signal on a single processor, kick off a PWM signal.

The reason that I didn't use analogWrite is because the Reference page says that it can be influenced by how close that your hand is to it, which doesn't work for what I am ultimately trying to do.

muskiepalooza:
The reason that I didn't use analogWrite is because the Reference page says that it can be influenced by how close that your hand is to it, which doesn't work for what I am ultimately trying to do.

News to me.

Don't you need to look at the input signal at some point then?

Perhaps you should look at the PING library, which uses an ultrasonic sensor.

The ultrasonic sensor would work in theory, but this project is going to be intended to be used outside. I basically just need to figure out a way to read the input from a receiver and count the amount of HIGHs. I have been playing with it for a few days and just can't get it figured out.

I'd kick off a PWM signal with an analogWrite, and jumper that to my input pin.
I may even change the prescaler and divider values to change the frequency.
No hand-waving involved.

In that case, read the input pin.
If it's high, add 1 to your count.
read it again - it it's not low, keep reading until it does go low.
once low, read it again until it goes high - if it's high, add 1 to your count.
read it again - it it's not low, keep reading until it does go low.

Think you could that? Maybe use while() to wait out the high periods after the input changes from low to high.