Pages: [1]   Go Down
Author Topic: An error is causing my code to work! Bizarre...  (Read 776 times)
0 Members and 1 Guest are viewing this topic.
Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 243
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Basically, in the form below:

Code:
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
« Last Edit: April 24, 2012, 03:51:01 am by Dane » Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3113
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 243
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley

Thanks
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24448
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 243
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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. smiley-razz smiley-wink
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46227
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 243
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

If I do that I get this:

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I also find this strangely confusing:

Code:
  pinMode(FrontDoorPin,INPUT);
  pinMode(FrontDoorPin,INPUT);
Logged

Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 243
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I also find this strangely confusing

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Wales
Offline Offline
Full Member
***
Karma: 0
Posts: 243
Don't take things too seriously
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

In my non-working example,
Code:
const int relay = 4;

is changed to
Code:
const int relay = 10;

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

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 25
Posts: 1148
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley )
Code:
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.
Logged

Pages: [1]   Go Up
Jump to: