An error is causing my code to work! Bizarre...

Whoops - in stripping back the extraneous code I also removed the description.

Basically, in the form below:

const int FrontDoorPin = 4;				
const int relay = 4;  


void setup() 
{
  pinMode(FrontDoorPin,INPUT);
  digitalWrite(FrontDoorPin, HIGH);	//pull up
  pinMode(relay, OUTPUT);   
}
void loop() 
{
  if (digitalRead(FrontDoorPin) == LOW) 
  {
//trigger here
  }
}

my code works. A press on a button (connected to pin 4 and ground) is noticed by the Arduino.

If I remove “const int relay = 4” the Arduino thinks the button is pressed - constantly?

THANKS

Not to be rude... but stripping your code out of what doesn't matter for this doubt of yours so we wouldn't have to parse through all the program would be nice.

bubulindo:
Not to be rude... but stripping your code out of what doesn't matter for this doubt of yours so we wouldn't have to parse through all the program would be nice.

As it turns out you're not at all rude, but presumptuous because I did exactly that. My actual code is 943 lines long, the above stripped version 187. :slight_smile:

Thanks

I'm with Bubulindo on this; I find it hard to believe that 186 lines is the smallest code which would display this fault.
I'm not sure I care whether that is rude or not.

AWOL:
I find it hard to believe that 186 lines is the smallest code which would display this fault.

AWOL:
I'm not sure I care whether that is rude or not.

You were right, I could and have stripped it down more than I already had. I have now stripped it down to the bare bones. The rest I didn't do this earlier is because in this reduced state the sketch won't "do" anything, but the logic is still there.

This wasn't rude either - not even close. However - to not care whether something might be rude is in itself rude. :stuck_out_tongue: :wink:

Because you have declared the pin to be an output pin, reading from it returns the last value written to the pin. Output pins are, by default, LOW, so it is not surprising that the pin always reads LOW.

PaulS:
Because you have declared the pin to be an output pin, reading from it returns the last value written to the pin. Output pins are, by default, LOW, so it is not surprising that the pin always reads LOW.

Hi Paul - actually the reverse appears to be true. In its current state (posted above) the sketch works. But I'm confused as to why. If I remove the 'relay' output assignation it ceases to work.

Dane:
If I remove “const int relay = 4” the Arduino thinks the button is pressed - constantly?

If I do that I get this:

sketch_apr24c.cpp: In function 'void setup()':
sketch_apr24c:8: error: 'relay' was not declared in this scope

I also find this strangely confusing:

  pinMode(FrontDoorPin,INPUT);
  pinMode(FrontDoorPin,INPUT);

Sorry - that'll be an error I introduced when stripping back my code. Of course it has to be and should be done but it's one of the reasons I was keen to strip as little possible. Please note I'm not trying to start an argument - just explaining the anomoly. I've amended the code.

Yes, very good. But what about the error when I remove the line you suggested?

In my non-working example,
const int relay = 4;
is changed to

const int relay = 10;

Sorry, I had explained this in my original "first post" - which I then contrived to wipe!

Editing of posts after people have discussed in several following posts them makes reading the thread very hard as they refer to something no longer there. Keep the edit feature to minor typos or suchlike.

Back to the subject: The code as I read it at this moment you declare two variables with the same content. The setup code does: Set pin to Input, Write High to pin, set pin to Output. The loop then reads the output pin. Reading an output pin "should" return the last value written...(similar case) which is HIGH.

If we leave out the 2nd defenition, or rather change (to avoid the undefined compiler error) then the code is
Set pin to input, Write high. The code then just reads the input pin.

You claim the exact opposite is happening. Have I understood you correctly?

I tried your code on my Arduino and it works for either case. Here is my code (which I will not edited after posting :slight_smile: )

const int FrontDoorPin = 4;				
const int relay = 4;  // changed to 11 .. still works

void setup() 
{
  pinMode(FrontDoorPin,INPUT);
  digitalWrite(FrontDoorPin, HIGH);	//pull up
  pinMode(relay, OUTPUT);   
}
void loop() 
{
  if (digitalRead(FrontDoorPin) == LOW) 
    digitalWrite(13,HIGH) ;
  else digitalWrite(13,LOW) ;
}

I even listed the assembly output and there is no "funnies" about the compiler getting confused or optimizing something away.