Pages: [1]   Go Down
Author Topic: [Solved] Are pins forced to LOW after constructor calls?  (Read 817 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Edison Member
*
Karma: 136
Posts: 1491
If you believe something is right, you won't see what's wrong (David Straker).
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On my Due i have the following code (Arduino IDE 1.5.2). It should force pin 8 to HIGH, but pin 8 will be LOW instead.
Code:
// THIS DOES NOT WORK ON THE DUE, PIN 8 WILL BE LOW
class pin {
  public:
    pin() {
      pinMode(8, OUTPUT);
      digitalWrite(8, HIGH);
    }
    int z;
};

class pin x;

void setup() {
  x.z++;
}
void loop() {
}

If I put the constructor call into the setup(), it will work. So this is fine and pin 8 will be HIGH:
Code:
class pin {
  public:
    pin() {
      pinMode(8, OUTPUT);
      digitalWrite(8, HIGH);
    }
    int z;
};

void setup() {
  class pin x;
  x.z++;
}
void loop() {
}

On the Uno, both examples produce HIGH at pin 8.

Any ideas? Did I miss something? I had looked into this forum, but did not see any thead about this topic.

Unfortunately this currently breaks my library: During the constructor call a do a low active reset on an external device. Of course i expect, that the output pin stays HIGH within setup() and loop(). But this is not true and my external device will be put into reset again.

Oliver
« Last Edit: February 22, 2013, 06:16:43 pm by olikraus » Logged

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

In your first example, your constructor runs before "init()".
Take a look at the source of "init()".
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.

Germany
Offline Offline
Edison Member
*
Karma: 136
Posts: 1491
If you believe something is right, you won't see what's wrong (David Straker).
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hmmm good hint. So here is the main loop (objdump):
Code:
int main( void )
{
   802a4: b508      push {r3, lr}
init();
   802a6: f000 fc83 bl 80bb0 <init>

delay(1);
   802aa: 2001      movs r0, #1
   802ac: f000 fd76 bl 80d9c <delay>

#if defined(USBCON)
USBDevice.attach();
   802b0: 4806      ldr r0, [pc, #24] ; (802cc <main+0x28>)
   802b2: f000 f849 bl 80348 <_ZN10USBDevice_6attachEv>
#endif

setup();
   802b6: f7ff ff35 bl 80124 <setup>

for (;;)
{
loop();
   802ba: f7ff ff3b bl 80134 <loop>
if (serialEventRun) serialEventRun();

And, yes, fully correct, the "init()" code includes this:
Code:
// Disable pull-up on every pin
  for (int i = 0; i < PINS_COUNT; i++)
   80bea: e004      b.n 80bf6 <init+0x46>
  digitalWrite(i, LOW);

Thanks AWOL. Good point.
I expect that this will break many existing libs...

Oliver
Logged

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

Quote
I expect that this will break many existing libs
which is why many libraries have a "begin" method which must be explicitly called.
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.

Germany
Offline Offline
Edison Member
*
Karma: 136
Posts: 1491
If you believe something is right, you won't see what's wrong (David Straker).
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hmmm... I never had seen this relationship. Thanks for clarification. Karma+

Oliver
Logged

Pages: [1]   Go Up
Jump to: