-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Butterfly analogWrite and pinMode conflicts #48
Comments
What is the problem exactly ? Hang ? Wrong value ? Spike ? |
Ah sorry. The code continues but no output voltage is observed. |
Why are you calling this a second time? Not necessary.
…On Tue, Sep 17, 2019 at 9:16 AM jacky4566 ***@***.***> 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
<#48?email_source=notifications&email_token=ABTDLKTY2ZCXD2NSTUSEUMTQKD7EDA5CNFSM4IXSJSXKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HL43DYQ>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABTDLKVBLXITQJQIFFHMBM3QKD7EDANCNFSM4IXSJSXA>
.
|
Yes I understand its not necessary but many Arduino libraries do this. This behavior is also very unexpected and could easily confuse new users. 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. |
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 ***@***.***> 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
<#48?email_source=notifications&email_token=ABTDLKUQHFE5PD3CXPCYRLDQKEFXXA5CNFSM4IXSJSXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD65HP2Y#issuecomment-532314091>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABTDLKS7EZHVW2AOGM75Q2TQKEFXXANCNFSM4IXSJSXA>
.
|
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);
} |
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 ***@***.***> 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
<#48?email_source=notifications&email_token=ABTDLKX2CN3YWKLPYTVI5BTQKEM7FA5CNFSM4IXSJSXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD65NQEI#issuecomment-532338705>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABTDLKX2MDO72GVVM5YDEFDQKEM7FANCNFSM4IXSJSXA>
.
|
Kris the code is just a simplified example of how to cause the issue. 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. |
There seems to be a problem with calling pinMode multiple times and using analogWrite.
The following code will fail for me:
Quite a few of the Arduino universe libraries use this format so its a bit frustrating to change them all.
The text was updated successfully, but these errors were encountered: