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

So, here is my code:

contact=digitalRead(pin);
if (contact == HIGH) {
  motorstep(0,0);}
}

This does not seem to work. What am I doing wrong? I want to read the status of the pin and if that pin is HIGH then execute line of code.

Thank you!

This does not seem to work

Offer it a pension scheme and health benefits.

Maybe the pin is never HIGH.


Rob

Bob may be right. If you don't have a voltmeter or an oscilloscope (better) -- then feed it to an input pin or an ISR routine and see if it ever goes high.

the pin is high. 5v on the multimeter.

What they are saying is a digital input pin is never a true high or low unless you have wired something to it correctly, or enabled the internal pull-up resistor for that pin. So draw us what you have done to the pin and also show your complete sketch the exibits the problem you are having.

Lefty

Avantime: So, here is my code:

contact=digitalRead(pin); if (contact == HIGH) { motorstep(0,0);} }

This does not seem to work. What am I doing wrong? I want to read the status of the pin and if that pin is HIGH then execute line of code.

Thank you!

Is that bracket meant to be there?

that can’t be the whole code… :wink:

in setup, do you have a pinMode(pin,INPUT);?

yes the bracket is good.

So.. I have an Arduino Mega, I have the HIGH pin on pin 8 and the input pin on pin 9. Pin 8 is going high, and the connection is good, so pin 9 also goes HIGH. The pins are declared:

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

In setup I have:

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

And in loop:

contact=digitalRead(pincontact);
if (contact = HIGH) {
  motorstep(0,0);}

}

if (contact = HIGH

Oh dear.

Why don't you just post your code - the statement above is not the same as the original post.

AWOL are you going to provide some real info to this topic or not? If not please vamoose.

Yes, it`s not the same code because I tinkered with it from the first post until now. I overlooked this mistake. The code is very long and most of it has no importance regarding my problem.

Read my last post and reply #8 carefully.
Then read your original post.

My first reply was deliberately obtuse and yes, sarcastic - you didn’t provide sufficient information (nor have you yet) to answer the question.
Instead we have to extract information like teeth.

If you want answers, provide information, or in your own words, vamoose.

I`m sorry my first post was not very complete. I thought that there was a problem with the "if" declaration, that is why I posted just that line.

But it isn't the same line as in reply #8, is it?

I told you that I modified the code in the time between post #1 and #8.

The code is very long and most of it has no importance regarding my problem.

Oh! For a quid for every time I've heard that one. :stuck_out_tongue_closed_eyes:

I told you that I modified the code in the time between post #1 and #8.

Well, if you changed it to the one in reply 8, you got it wrong. Look at the title of this thread, then look at reply 8.

I set it to “==” before and it didn’t work.

how do you know that motorstep(0,0); works? Maybe your if (pin==HIGH) is working, but the rest doesn't (some Serial.print() stuff could clarify).

It's sometimes the case that code is either too long to post or too long for some people to wade through. In this case the thing to do is as if you were reporting a bug, reduce the code to the problem area. In this case something like this

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) {
    Serial.println ("It works");
  }
}

If this doesn't work then you know to look at the hardware or at least there is only a few lines to check. If it does then look at the rest of your code.


Rob

I set it to "==" before and it didn't work.

So then what?, did you put it back to "=" ?

You seem to be too defensive and/or terse in your replies. Nobody want's you to fail, but you need to be a little more responsive to the replies and provide more information when asked. Otherwise is in bound to become a long thread with nobody happy with the results or outcome.

Lefty

1 Like