gpstar81 / GPStar-proton-pack

GPStar Proton Pack and Neutrona Wand
https://www.gpstartechnologies.com
GNU General Public License v3.0
37 stars 8 forks source link

[Bug]: Attenuator (esp32) vibration stops working #387

Open Shoop64 opened 1 week ago

Shoop64 commented 1 week ago

What actions were attempted?

Once a tone is played on the buzzer, the vibration seems to stop working.

I made several changes to the code and finally got to the point where I can get both the buzzer to work and vibration to work, although I'm not sure it matches the intended functionality.

The changes I made were using ledcAttach for the buzzer pin and a simple pinmode output for the vibration pin. Then you can use ledcWriteTone(BUZZER_PIN, i_freq) to play a tone(or a freq to 0 to stop it) and then vibration I just use digitalWrite with HIGH and LOW.

What actually happened?

I've been trying to build the circuit on a breadboard and was confused about why I couldn't seem to get the vibration to work. Eventually I worked out that it will no longer work after a tone has been played. I'm not sure why.

What was expected?

The vibration should continue to function independent of the buzzer function.

Firmware Version

20240823180629 build date

Mode in Use

Any / Both Modes

Notable Hardware

I think I'm mainly using what it in the BOM for the diy attenuator.

Homework Completed

nomakewan commented 1 week ago

Are you on an ESP32 or Arduino Nano?

Shoop64 commented 1 week ago

ESP32

nomakewan commented 1 week ago

Well what a coincidence, I'm neck-deep in ESP32 code right now trying to beef up the serial data sync. I'll go ahead and add this to my plate and see if I can pinpoint the cause so we can add it to the list of fixes for 5.3.4.

nomakewan commented 1 week ago

Okay, found the problem. The problem is that the tone library actually uses ledc internally, and so its internal declarations were un-setting the ledc definition for the vibration motor that had been initialized in setup.

Now that I know how tone works I'll go ahead and get this fixed so that both the buzzer and vibration motor will work as intended. :) Thank you for the report!

EDIT: Additionally, there appears to be a bug in the Tone library which could also be related, since it has to do with defining which channel the specified tone should be using: https://github.com/espressif/arduino-esp32/pull/10305

The fix was merged, but a new release hasn't been spun yet.

nomakewan commented 1 week ago

Okay, we have a push that should resolve it for 5.3.4. The correct way to resolve this issue would be to wait for the next stable release of ESP32 (should be 3.1.0), then use the proper channel setting for Tone. However, considering we have no clue when that will be, I've included a workaround that should work in the meantime.

You can actually test it yourself; grab the Attenuator code from the main branch (5.3.3, which is build date 20240823180629), then change ledcAttach(VIBRATION_PIN, 5000, 8); on line 158 of Attenuator.ino to ledcAttachChannel(VIBRATION_PIN, 5000, 8, 5);. Then replace pinMode(BUZZER_PIN, OUTPUT); on line 155 with ledcAttachChannel(BUZZER_PIN, 440, 10, 0);.

That should allow both the buzzer and vibration motor to work normally (since we are now initializing ledc on the buzzer using channel 0, and ledc on the vibration motor using channel 5).

Shoop64 commented 1 week ago

Excellent, that makes sense to me. Thanks!

Shoop64 commented 1 week ago

I tested out the change and I lose the buzzer doing the change on line 155. Leaving it as pinMode(BUZZER_PIN, OUTPUT); but changing the vibration pin to ledcAttachChannel got both working.

nomakewan commented 1 week ago

Interesting; I wonder why enforcing channel 0 on the buzzer caused a failure. Perhaps some undocumented feature of the auto-channel? Argh.

Either way, will go ahead and put the pinMode back where it was (with a note that once ESP32 3.1.0 is out we can fix this one far more reliably). Thank you for testing!

EDIT: ESP32 3.0.5 was released and included the fix for the Tone library. That said, the shift to the PlatformIO development environment for the Attenuator has regressed ESP32 to 2.0.17 (for now), so hopefully this problem will remain resolved.