If (pin == HIGH) {Do something) not working

motorstep works for sure. But now I followed Graynomad`s advice, and I connected a LED on pin 10 and modified "Serial.println ("It works");" with "digitalWrite(10,HIGH);" and the led is permanently on....

retrolefty I am not being defensive, I am very thankful for the advice I`m getting from you guys.

So.. Starting from this code

int pincontact5v = 8;
int pincontact = 9;
int contact=0;

void setup () {
  pinMode(pincontact5v, OUTPUT);
  pinMode (pincontact, INPUT);
  digitalWrite(pincontact5v, HIGH);

}

void loop () {
  
  contact=digitalRead(pincontact);
  if (contact == HIGH) {
    digitalWrite(10,HIGH);
  }
}

The led on pin 10 is permanently on, without pincontact being HIGH

try to reflect directly the state:

void loop () {
  contact=digitalRead(pincontact);
  digitalWrite(10,contact);
}

at reset you didn't initalize pin 10 status:

add a digitalWrite(10, LOW); at the end of setup( ).

Once you read a HIGH value, you set pin 10 to HIGH, but when the reading is LOW, you didn't set the pin 10 back to LOW.
(instead with the other example, serial.print(...) will just stop printing).

if (contact == HIGH) {
digitalWrite(10,HIGH);
} else {
digitalWrite(10, LOW);
}

It's unlikely but maybe you have a bad pin, try another.

(instead with the other example, serial.print(...) will just stop printing).

Why I suggested it.


Rob

The led on pin 10 is permanently on, without pincontact being HIGH

First mention of an external led! How exactly have you wired up the led to your output pin? How exactly have you wired the input pin? Before it sounded like you just had a jumper wire from the output pin to the input pin?

Lefty

I connected the led for testing purposes after Granyomad's advice.

I attached the connection scheme and here is the code, once again.

int pincontact5v = 8;
int pincontact = 9;
int contact=0;

void setup () {
  pinMode(pincontact5v, OUTPUT);
  pinMode (pincontact, INPUT);
  digitalWrite(pincontact5v, HIGH);
 digitalWrite(13, LOW);
}

void loop () {
  contact=digitalRead(pincontact);
  digitalWrite(13,contact);
}

As a result the LED flickers when connecting pincontact but also the led does not turn completely off, it`s a bit bright even when disconnecting pincontact.

Ok, I think that is better if you follow this link to understand how to use digital Inputs

http://www.arduino.cc/en/Tutorial/Button

but also the led does not turn completely off

As there is no pull up/down resistor on the input with the switch I suspect it's oscillating and therefore driving the LED at some frequency.

Add a resistor (or enable the internal pullup) to the input, and also add a current-limiting resistor in series with the LED.

Also one of the switch wires should go to GND, not another input pin.


Rob

the LED has a resistor on it. How do I enable the internal pull-up?

How do I enable the internal pull-up?

Write a HIGH to the input.

pinMode (pincontact, INPUT);
digitalWrite (pincontact, HIGH);

Also note what I added about the input wires, one should go to GND (unless there is some reason to use the pincontact5v pin that's not obvious).


Rob

the LED has a resistor on it.

Wired in series with the LED? What value is the resistor?

enabling internal pullup to a digital input pin is just a digitalWrite(pin#,HIGH);

Lefty

Graynomad:

Also one of the switch wires should go to GND, not another input pin.

You sir are a.. Genius! :slight_smile: It worked. But I cannot figure out why. Does not the HIGH state mean that there is 5V input on that pin?

You sir are a.. Genius! smiley It worked. But I cannot figure out why. Does not the HIGH state mean that there is 5V input on that pin?

Compared to what?

And that is not a smartalec comment. There is always a reference in electronic equipment -- something that is often forgotten...

compared to GND input in that PIN

But I cannot figure out why. Does not the HIGH state mean that there is 5V input on that pin?

enabling a pull-up to a input pin makes the default value HIGH. When you ground the pin it will read as a LOW. What isn't clear is if that was a switch you were using to route the output pin to the input pin? If so then if the switch was closed the input pin should read whatever value your output pin is set to and you should not have ever needed to disconnect the wire between the input pin and output pin. However now with the internal pull-up on, you should detect changes on the input pin regardless of if you disconnect the wire from the output pin and either ground it (makes it LOW) or leave it disconnected (makes it HIGH).

So do you want the input pin to just follow the output pin at all times, or do you want it to detect changing a switch? That is what is confusing me.

Also about that LED resistor question, any answer yet?

Lefty

I`m building a DIY stamping press, and I need that as part A lowers towards part B, immediately after contact between the two parts, part A stops movement. I isolated both parts, sent 5V through part B and I thought that after contact, part A should send 5V back to the arduino in order to command stop of movement for part A.

I don`t know what value the resistor is. I bought the leds pre-wired from Ebay :slight_smile:

Avantime:
I`m building a DIY stamping press, and I need that as part A lowers towards part B, immediately after contact between the two parts, part A stops movement. I isolated both parts, sent 5V through part B and I thought that after contact, part A should send 5V back to the arduino in order to command stop of movement for part A.

Then just ground part B, and enable pullup on input pin reading part A. A LOW input will mean parts are touching, a HIGH input means they are not touching, it's that simple.

As far as your LED with built in resistor (if indeed it has that), then that means it's designed to work with only one specific voltage and will draw too much current if driven by a higher voltage, or draw less current if driven by too low a voltage. You really should find out what voltage it was designed to work with, or wire it to +5vdc through a current meter to ground and see how much current it is drawing at 5vdc. Otherwise you have a big unknown when using those leds for this and future projects.

Lefty

I`m a beginner in electrics and electronics. The led is a 12V one. I though that the only problem would be that the light output would be lower than on 12v. I do not have a 5V LED around.

By "ground part B" you mean that I should connect it to GND on arduino and by enabling pull-up on input pin reading part A you mean digitalWrite(pinpartA, HIGH)? :slight_smile:

I really appreciate your help, thank you!