-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Fix for setChannel #5
Conversation
@cfnz Which board do you use? |
Hi, I was running on a Wemos D1 Mini and using PlatformIO/Visual Studio Code. But now, looking at the code I can see the issue. From my understanding, we have an array of 3 ints for the pins and the mask is being moved along to look at what each pin should be in turn. It then sets the value of the pin array, but it assumes that the index to the pin array matches the mask... but it doesn't. The index to the pin array is only moved along if that pin needed to change, so the index and the mask gets out of sync. In my testing to find the issue I have some code, so I may as well provide it and the test output below. (I have formatted the output to line things up a bit.). The pn = the pin number and what it was set to. The Dn is the number read back from digitalRead. c is the channel number. (This output is in the middle of the stream, so c:0 came after a c:7.)
Here you can see that only the 3rd pin changes. Moving the i decrement out of the if statement results in the following:
HC4051 hc4051 = HC4051(D0, D1, D2, D3);
uint8_t _pins[3] = { D0, D1, D2 };
uint8_t _channel = 0;
void loop() {
for (int c = 0; c < 8; c++) {
// hc4051.setChannel(c);
Serial.print("c:");
Serial.print(c);
uint8_t _new = c;
if (_new != _channel) {
uint8_t _changed = _new ^ _channel;
uint8_t mask = 0x04;
uint8_t i = 2;
Serial.print(" ");
while (mask) {
// only write changed pins. // AVR only?
if (mask & _changed) {
Serial.print(" p");
Serial.print(i);
Serial.print(":");
Serial.print((mask & _new));
digitalWrite(_pins[i--], (mask & _new));
}
mask >>= 1;
}
_channel = _new;
}
Serial.print(" D2:");
Serial.print(digitalRead(D2));
Serial.print(" D1:");
Serial.print(digitalRead(D1));
Serial.print(" D0:");
Serial.print(digitalRead(D0));
Serial.println();
Serial.println();
delay(1000);
}
} (updated for syntax highlighting, adding cpp after backquotes) |
@cfnz |
@cfnz Will merge asap. |
@cfnz Again thanks for reporting and the PR, really appreciated! |
No trouble... And thanks for your work, it is really appreciated too! |
As mentioned in Issue #4, this fixes setChannel as it decrements variable i regardless if there is a change or not. I believe this is correct behaviour as without it, the digital pins were not getting set properly.