Can't figure out why this isnt working?

I'm trying to debug another project, that has a similar problem to this. I would take it step by step to see what was wrong and started with this.

It's just a simple program that tries to read digital input. The problem is that no matter if I set the wire in or out it's wobbling between low and high. Its mostly staying at high. The only thing it can see is when the wire is in. Then its high no matter what...

This is confusing.. Please help :wink:

Regards Rumrobot

int input;

void setup() 
{
  Serial.begin(9600);
  pinMode(4, OUTPUT);
  pinMode(3, INPUT);
}

void loop() 
{
  digitalWrite(4, HIGH);
  input = digitalRead(3);

  if (input == HIGH)
  {
    Serial.println("HIGH");
  }
  
  if (input == LOW)
  {
    Serial.println("LOW");
  }
}

(The wire isn't in on the picture. And if I fiddle with the Arduino when the wire isn't in, it starts wobbling between high and low again.)

Floating input.

This is not a problem with Arduino, it's a problem with your project, and should not be in this forum section.

Also "isn't working" is about the dumbest description imaginable. You never defined what "working" means.

The problem is that no matter if I set the wire in or out it's wobbling between low and high

To keep the input HIGH or LOW it needs to be connected to 5V or GND and not left floating. How exactly is the input wired ?

  digitalWrite(4, HIGH);
  input = digitalRead(3);

What is the purpose of the digitalWrite() here ?

I'm going to guess that "the wire" is intended to be between pin 4 and pin 3. If so then you're doing the test wrong. What you should do is leave the wire connected, write HIGH to pin 4 then read pin 3 then put a delay(1000) in, write LOW to pin 4 and then read pin 3.

All you have proved so far is that having nothing connected to a pin is NOT the same as having a LOW.

Steve