Skip to content

analogWrite() uses a threshold when writing to a digital port #706

Open
@tedtoal

Description

@tedtoal

When analogWrite() is called to write to a pin that is in digital mode, it maps the analog output value to 8 bits and then applies a threshold of 128, writing LOW if < 128 else HIGH. To me this is plain wrong, as one expects in C and C++ that only a value of 0 will result in LOW.

I suggest the code be changed as follows:

311,316c311
<   value = mapResolution(value, _writeResolution, 8);
<   if (value < 128) {
<     digitalWrite(pin, LOW);
<   } else {
<     digitalWrite(pin, HIGH);
<   }
---
>   digitalWrite(pin, value == 0 ? LOW : HIGH);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions