GrumpyOldPizza / arduino-STM32L4

69 stars 60 forks source link

Butterfly analogWrite and pinMode conflicts #48

Open jacky4566 opened 4 years ago

jacky4566 commented 4 years ago

There seems to be a problem with calling pinMode multiple times and using analogWrite. The following code will fail for me:

static const int MOSFET = 9;

void setup() {
  //ON
  pinMode(MOSFET, OUTPUT);
  analogWrite(MOSFET, 255);
  delay(1000);
  //OFF
  analogWrite(MOSFET, 0);
  delay(3000);
  //ON
  pinMode(MOSFET, OUTPUT);//This pinMode causes problems
  analogWrite(MOSFET, 255);
  delay(1000);
  //OFF
  analogWrite(MOSFET, 0);
  pinMode(MOSFET, INPUT);
}

void loop() {
  delay(1);
}

Quite a few of the Arduino universe libraries use this format so its a bit frustrating to change them all.

GrumpyOldPizza commented 4 years ago

What is the problem exactly ? Hang ? Wrong value ? Spike ?

jacky4566 commented 4 years ago

Ah sorry. The code continues but no output voltage is observed. The first "ON" executes correctly 3.3v for 1s but the second does nothing. 0 volts.

kriswiner commented 4 years ago

Why are you calling this a second time? Not necessary.

On Tue, Sep 17, 2019 at 9:16 AM jacky4566 notifications@github.com wrote:

There seems to be a problem with calling pinMode multiple times and using analogWrite. The following code will fail for me:

static const int MOSFET = 9;

void setup() { //ON pinMode(MOSFET, OUTPUT); analogWrite(MOSFET, 255); delay(1000); //OFF analogWrite(MOSFET, 0); delay(3000); //ON pinMode(MOSFET, OUTPUT);//This pinMode causes problems analogWrite(MOSFET, 255); delay(1000); //OFF analogWrite(MOSFET, 0); pinMode(MOSFET, INPUT); }

void loop() { delay(1); }

Quite a few of the Arduino universe libraries use this format so its a bit frustrating to change them all.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/GrumpyOldPizza/arduino-STM32L4/issues/48?email_source=notifications&email_token=ABTDLKTY2ZCXD2NSTUSEUMTQKD7EDA5CNFSM4IXSJSXKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HL43DYQ, or mute the thread https://github.com/notifications/unsubscribe-auth/ABTDLKVBLXITQJQIFFHMBM3QKD7EDANCNFSM4IXSJSXA .

jacky4566 commented 4 years ago

Yes I understand its not necessary but many Arduino libraries do this. This behavior is also very unexpected and could easily confuse new users. I found this while using an older Motor Driver Library

Its an easy fix to remove the pinMode() calls but still not great practice since pinMode() should be repeatable under Arduino.

This also kills Charlieplexing LEDs where you need tri-state logic to change between INPUT and OUTPUT rapidly.

kriswiner commented 4 years ago

Changing the mode should work fine, repeatedly calling the same mode is no good practice.

On Tue, Sep 17, 2019 at 10:12 AM jacky4566 notifications@github.com wrote:

Yes I understand its not necessary but many Arduino libraries do this. This behavior is also very unexpected and could easily confuse new users. I found this while using an older Motor Driver Library https://www.arduinolibraries.info/libraries/motor-driver-library

Its an easy fix to remove the pinMode() calls but still not great practice since pinMode() should be repeatable under Arduino.

This also kills Charlieplexing LEDs where you need to change between INPUT and OUTPUT rapidly.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/GrumpyOldPizza/arduino-STM32L4/issues/48?email_source=notifications&email_token=ABTDLKUQHFE5PD3CXPCYRLDQKEFXXA5CNFSM4IXSJSXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD65HP2Y#issuecomment-532314091, or mute the thread https://github.com/notifications/unsubscribe-auth/ABTDLKS7EZHVW2AOGM75Q2TQKEFXXANCNFSM4IXSJSXA .

jacky4566 commented 4 years ago

Changing the mode also does not work, see my example below. Repeatedly calling the same mode may not be good practice no, but it happens. Even in Adafruit's libraries

I feel this should be a concern for you guys since it's common enough in the Arduino enviroment.

static const int MOSFET = 9;

void setup() {
  //ON
  pinMode(MOSFET, OUTPUT);
  analogWrite(MOSFET, 255);
  delay(1000);
  //OFF
  analogWrite(MOSFET, 0);
  delay(3000);
  //ON
  pinMode(MOSFET, INPUT);
  pinMode(MOSFET, OUTPUT);
  analogWrite(MOSFET, 255); //No output produced. 0v
  delay(1000);
  //OFF
  analogWrite(MOSFET, 0);
  pinMode(MOSFET, INPUT);
}

void loop() {
  delay(1);
}
kriswiner commented 4 years ago

Comment out these two lines and it should work:

pinMode(MOSFET, INPUT); pinMode(MOSFET, OUTPUT);

Why are you toggling the mode for no reason here?

If you insist on doing so, at least put a delay between the calls.

On Tue, Sep 17, 2019 at 11:14 AM jacky4566 notifications@github.com wrote:

Changing the mode also does not work, see my example below. Repeatedly calling the same mode may not be good practice no, but it happens. Even in Adafruit's libraries http://Adafruit-Motor-Shield-library

I feel this should be a concern for you guys since it's common enough in the Arduino enviroment.

static const int MOSFET = 9; void setup() { //ON pinMode(MOSFET, OUTPUT); analogWrite(MOSFET, 255); delay(1000); //OFF analogWrite(MOSFET, 0); delay(3000); //ON pinMode(MOSFET, INPUT); pinMode(MOSFET, OUTPUT); analogWrite(MOSFET, 255); //No output produced. 0v delay(1000); //OFF analogWrite(MOSFET, 0); pinMode(MOSFET, INPUT); } void loop() { delay(1); }

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/GrumpyOldPizza/arduino-STM32L4/issues/48?email_source=notifications&email_token=ABTDLKX2CN3YWKLPYTVI5BTQKEM7FA5CNFSM4IXSJSXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD65NQEI#issuecomment-532338705, or mute the thread https://github.com/notifications/unsubscribe-auth/ABTDLKX2MDO72GVVM5YDEFDQKEM7FANCNFSM4IXSJSXA .

jacky4566 commented 4 years ago

Kris the code is just a simplified example of how to cause the issue. A delay of 50ms also did not fix the problem.

Your free to do as you wish but I believe that repeated calls to "pinMode( , OUTPUT);" should not cause a failure of the pin to conduct.