0

AdaBox 011: fixing problems with Alarm Clock project
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

AdaBox 011: fixing problems with Alarm Clock project

by bitflung on Wed May 15, 2019 10:17 am

This thread serves to document the issues I faced and solutions I used when building the project posted at the link below:
https://learn.adafruit.com/pyportal-ala ... k/overview

I'll occasionally update this top level post to add missing details as needed.

I hijacked another thread for this in the past, but thought i'd start a dedicated thread and reference/copy the content here.
(my old comments were here: viewtopic.php?f=59&t=150560 - multiple large comments throughout that thread)

Top level issues I faced with the Alarm Clock project fit into these categories:
  1. Software
  2. Included hardware
  3. Additional hardware
  4. 3D Model for printing

Some issues rendered the project entirely unusable (or intractable to build) so I figure it may be useful to fully document the fixes I found necessary to get this up and running as a functioning alarm clock for my nearly 6yo son. I'll list these first as "Critical" issues.

Critical Software Issues:
  1. Incomplete Code: project was missing the adafruit_logger class. FIX: I wrote my own logger with a similar API/behavior
  2. Touchscreen: actually a hardware issue (listed below) but fixed in software: I wrote a "SmartTouch" class to filter out false touch events
  3. Weather API exceptions: my alarm clock throws an exception about once every 2 days due to the weather API returning invalid data. the call to update weather doesn't handle the exception so the clock crashes and must be manually rebooted (I haven't fixed this yet)
  4. Alarm audio playback uses a BLOCKING CALL, but the code is written as though it were NON-BLOCKING (fixed by passing 'False' to the optional second parameter when calling play_file() to play the alarm audio)

Critical Included Hardware Issues:
  1. Touchscreen: frequently registers false touch events. Implemented a software workaround described above. Seems to be a quality issue with the touchscreen overlay.

Critical Additional Hardware Issues:
  1. Parts list is incomplete: if you buy all the parts listed for purchase on the project page you'll still not have everything you need to build this project
  2. Parts list is inaccurate: the listed speaker WILL NOT WORK with the PyPortal until/unless you add some resistance (4-12 Ohms) in series with the speaker (fixed in my project by adding an 8 ohm speaker in series with the 4 ohm speaker bought through adafruit)


Non-critical issues I faced included:

Software:
  1. Boot flow: without wifi the code won't even display the UI on the clock. (I haven't fixed this yet)
  2. UX for changing the alarm time: swiping on a polled resistive screen doesn't work well at all for me. (fixed by modifying code to accept taps above/below each digit)
  3. UX for storing alarm time: setting a new alarm time doesn't STORE that setting for the future; if you reboot the device the alarm time goes back to defaults. (I haven't fixed this yet)
  4. UX for displaying time: the UI has no option for 12hr time. fine for most of the world, but intractable for a 6yo who only recently mastered as 12hr clock (fixed by modifying code to display hours mod 12)

3D Model:
  1. Thin walls: can't be printed with horizontal compensation (used for dimensional accuracy). as a result the final working print had to be post processed quite a bit (melted parts of the case with a soldering iron to reform manually so the parts fit together properly)
  2. Button design is just plain bad: it floats above the opening it is meant to sit inside of. not sure, might be due to the button hardware from the parts list being larger than used by the original designers. (i haven't fixed this yet but plan to design a new button in CAD and print again)
Last edited by bitflung on Wed May 15, 2019 4:30 pm, edited 2 times in total.

bitflung
 
Posts: 44
Joined: Mon Mar 11, 2019 3:53 pm

Re: AdaBox 011: fixing problems with Alarm Clock project

by dastels on Wed May 15, 2019 3:54 pm

Please, what parts did you find missing from the list?

dastels
 
Posts: 61
Joined: Tue Oct 20, 2015 3:22 pm

Re: AdaBox 011: fixing problems with Alarm Clock project

by bitflung on Wed May 15, 2019 4:16 pm

dastels wrote:Please, what parts did you find missing from the list?


(copied from my post in the other thread; edited to correct a typo in the final paragraph regarding resistance of available speakers on adafruit.com)

The list of components to buy for this project is incomplete. it currently contains:
  1. pyportal (https://www.adafruit.com/product/4116)
  2. speaker (https://www.adafruit.com/product/3968)
  3. button (https://www.adafruit.com/product/471)
  4. barrel jack (https://www.adafruit.com/product/610)
  5. power supply (https://www.adafruit.com/product/276)
  6. power boost (https://www.adafruit.com/product/2465)
  7. battery (https://www.adafruit.com/product/328)
  8. alternate power supply (https://www.adafruit.com/product/1995)

this list is missing (not all links are vetted, e.g. the slide switch might be the wrong size for the 3d printed case?):
  1. 6x M3x5mm screws (https://www.albanycountyfasteners.com/3 ... 1-1006.htm)
  2. 4x M2.5x5mm screws (https://www.albanycountyfasteners.com/3 ... 1-1006.htm)
  3. 6x M2x5mm screws (https://www.albanycountyfasteners.com/2 ... 1-1000.htm)
  4. "STEMMA" cable aka JST-PH 3-pin (https://www.adafruit.com/product/3893)
  5. Pico Blade 2-pin cable (https://www.adafruit.com/product/3922)
  6. Slide switch (https://www.adafruit.com/product/805)
  7. jumper wires (https://www.adafruit.com/product/758)

furthermore at least one listed part is WRONG:
  1. speaker (https://www.adafruit.com/product/3968)

this speaker has too low of a resistance, so if you use it you'll need to add a resister in SERIES with the speaker. you need to total resistance to be between 8 and 16 ohms, so add anything from 4-12 ohms resistance. i had an 8 ohm speaker laying in a parts drawer, so i added that in series (giving me a 12 ohm load on the audio driver and putting that extra resistance to use rather than just wasting the energy as heat). Failing to add resistance in series with this speaker results in too much current being sunk from the audio driver and therefore choppy garbage noises rather than intended audio during playback.

I don't see any 40mm 8-to-16 ohm speakers listed for sale here, so your options are:
  • source a speaker from elsewhere
  • add resistance in series with the speaker suggested by adafruit (e.g. another speaker)
  • leave the hole open in the case and just put a different speaker elsewhere (e.g. inside the case but not slotted into that hole as planned)
  • redesign the case before printing it to accommodate a different size speaker

bitflung
 
Posts: 44
Joined: Mon Mar 11, 2019 3:53 pm

Re: AdaBox 011: fixing problems with Alarm Clock project

by dastels on Wed May 15, 2019 4:41 pm

Re: the speaker.

The listed speaker does work.

Cutting the jumper trace next to the built-in speaker should help, although I didn't have a problem. You have a later version of the hardware, which could have slight changes to the audio circuit.

dastels
 
Posts: 61
Joined: Tue Oct 20, 2015 3:22 pm

Re: AdaBox 011: fixing problems with Alarm Clock project

by bitflung on Wed May 15, 2019 4:54 pm

dastels wrote:Re: the speaker.
The listed speaker does work.
...
You have a later version of the hardware, which could have slight changes to the audio circuit.


the silkscreen on the PCB states that it supports 8-16 ohm speakers. the listed 4 ohm speaker absolutely did not work on the current hardware. audio playback produced a few garbled clicks and a short buzz, nothing more.

...
Cutting the jumper trace next to the built-in speaker should help, although I didn't have a problem.
...

that's a good idea - the built in speaker would be in parallel to the larger speaker, reducing the effective resistance. my setup is working well right now but I'll look into cutting that trace anyway. it may further improve the audio :)

bitflung
 
Posts: 44
Joined: Mon Mar 11, 2019 3:53 pm

Re: AdaBox 011: fixing problems with Alarm Clock project

by shawng on Thu May 16, 2019 5:37 pm

Thanks for the write up. I came across many of these issues too. I wrote to the non-volatile memory to save the Alert time - viewtopic.php?f=60&t=151790

There was some code in the alert button (to change the time) that blocked repeated touches. I got rid of that and now I can just hold my finger above or below the number to increment values until I let go.

I would have liked the Pico Blade cable... Guess I'll have to make another order.

I grabbed the logger class from the 4x mpy lib files. I don't remember what link had that but it is out there somewhere.

My screen is registering the touches but the accuracy is not so good (overall it is ok). I have not had any false touches registered.

My issue still is that I want to play a longer audio for the alarm but I cannot find a way to interrupt it (I'm not using the external button yet, only the touch screen).

shawng
 
Posts: 36
Joined: Sun Oct 23, 2011 6:09 am

Re: AdaBox 011: fixing problems with Alarm Clock project

by bitflung on Thu May 16, 2019 7:56 pm

[edit: posted from mobile, typos abound. sorry]
[edit 2: that's a great solution for storing the alarm to too. looking forward to implement it!]

hey, thanks for joining the thread and confirming the issues. would be interesting to see if some of your changes might be better than mine. I've got github with my code, but i need to commit the most recent changes still. I'll post a link soon - would be open to suggested improvements.

eager to find the logger .mpy too. if you find it (or can share just the mpy) I'd appreciate a link.

My issue still is that I want to play a longer audio for the alarm but I cannot find a way to interrupt it (I'm not using the external button yet, only the touch screen).


your taking about the audio playback being a blocking call? if you add a second parameter of 'False' to the function call that plays the alarm audio it plays in the background. works nicely. when I post my GitHub link I'll reference the specific line numbers to look at. should help satisfy what you're trying to do (if I understood the issue properly anyway)

bitflung
 
Posts: 44
Joined: Mon Mar 11, 2019 3:53 pm

Re: AdaBox 011: fixing problems with Alarm Clock project

by shawng on Thu May 16, 2019 9:10 pm

Hi, I tried the non-blocking "false" option for the audio but had some issues in my setup.
1) the audio just played a few seconds and then restarted (I have a full 40sec clip I wanted to play...2MB WAV file).
2) I could interrupt the screen, close the red alert page and get back to the time page... but the music was still playing (maybe that's what you meant by playing in the background).
3) the sound quality was diminished

It is pretty easy to just hit reset on the clock so I just might stick with that and look to make it start up faster.

My adafruit_logging.mpy was in adafruit-circuitpython-bundle-4.x-mpy-20190513.zip. That should be available from their GitHub site at https://github.com/adafruit/Adafruit_Ci ... e/releases

shawng
 
Posts: 36
Joined: Sun Oct 23, 2011 6:09 am

Re: AdaBox 011: fixing problems with Alarm Clock project

by bitflung on Fri May 17, 2019 9:13 pm

EDIT: oh, and for what it's worth, the alarm file we use is 17 seconds, 256kB, and plays very well.

shawng wrote:Hi, I tried the non-blocking "false" option for the audio but had some issues in my setup.
1) the audio just played a few seconds and then restarted (I have a full 40sec clip I wanted to play...2MB WAV file).

I opened up my github, it actually looks up to date. might have checked in last time i worked on it after all.
https://github.com/bitflung/pyportal/tr ... /CIRCUITPY

i think your issue comes down to the audio starting, another tick event occurring, and the code starting the audio file playing back again while it was already playing.
you'll see my solution to the described issue in code.py:
Code: Select all | TOGGLE FULL SIZE
            if(not pyportal.audio.playing):
                logger.info("alarm: playing audio");
                pyportal.play_file(alarm_file, False)


this code ensures that the playback is started just once. each 'tick' event that occurs during playback hits the if statement above and skips the code underneath. the next 'tick' that fires AFTER the file is done playing back will result in the file being played again.

2) I could interrupt the screen, close the red alert page and get back to the time page... but the music was still playing (maybe that's what you meant by playing in the background).

yup, that's exactly right. background playback enables normal tick events to execute properly even while the audio is playing back.

3) the sound quality was diminished

i suspect most of the quality degradation you're noticing comes from the code restarting playback repeatedly. before i added the code described above it had the effect of sounding VERY degraded. with that code in place, however, i think it still sounded slightly degraded compared to the blocking method. at this point my best guess for that difference is that the background method might be using DMA and as a result might be constrained to a low bit-width transaction (e.g. two 16-bit words per 32-bit hardware DMA transfer, versus a full 24-bit word transferred in software)

It is pretty easy to just hit reset on the clock so I just might stick with that and look to make it start up faster.

i didn't have spare parts to create an external reset button, and i didn't realize i needed the unlisted switch when i bought the parts (and i don't feel like paying shipping just for a switch now) --- so when i reset i have to reach inside the case to hit the reset button on the back of the pyportal. it's a real pain. i'm very eager to get the code running stable so i don't have to reset so often.

My adafruit_logging.mpy was in adafruit-circuitpython-bundle-4.x-mpy-20190513.zip. That should be available from their GitHub site at https://github.com/adafruit/Adafruit_Ci ... e/releases

nice, thanks!
i just downloaded that and see adafruit_logging.mpy in there.

also, regarding your touchscreen, take a look at the SmartTouch class i wrote. it's in my code.py (seems i need to setup a toolchain to publish it as a standalone library). it filters out "bad touches", requiring some number of consecutive touch events all within proximity of each other. works really well on my device.

bitflung
 
Posts: 44
Joined: Mon Mar 11, 2019 3:53 pm

Re: AdaBox 011: fixing problems with Alarm Clock project

by shawng on Mon May 20, 2019 1:15 pm

I'll give those ideas a try, thanks.
If you are saving the alarm to the built-in-memory, also grab the alarm state, otherwise, the alarm gets active after every reboot.

shawng
 
Posts: 36
Joined: Sun Oct 23, 2011 6:09 am

Re: AdaBox 011: fixing problems with Alarm Clock project

by bitflung on Mon May 20, 2019 1:31 pm

i just realized i didn't fully understand an issue you raised last time, that the audio continues to play after closing the red alert screen.

this is due to the original code having a clunky way of handling the alarm termination event and me forgetting to explicitly describe how i fixed that. there is a function call to stop the audio playback when you exit the alarm state:
Code: Select all | TOGGLE FULL SIZE
def exit(self):
        global alarm_armed, pyportal
        super().exit()
        pyportal.audio.stop()
        alarm_armed = bool(snooze_time)

bitflung
 
Posts: 44
Joined: Mon Mar 11, 2019 3:53 pm

Re: AdaBox 011: fixing problems with Alarm Clock project

by shawng on Tue May 21, 2019 7:21 am

Thanks, both of those steps fixed my main issues... no more immediate looping of the alarm music and I can cancel the alarm by touching the screen. The WAV file playback is still very staticky and after the alarm is stopped there is a low level sound coming from the speaker... like a 300 baud modem. I'll try hooking up a better speaker and see if that helps.

The low level whistling comes after cancelling the alarm, just after a second sound like a short laser zap plays. That cancel zap sound is not from anything I added so I am not sure where it comes from.

shawng
 
Posts: 36
Joined: Sun Oct 23, 2011 6:09 am

Re: AdaBox 011: fixing problems with Alarm Clock project

by bitflung on Tue May 21, 2019 11:30 am

shawng wrote:Thanks, both of those steps fixed my main issues... no more immediate looping of the alarm music and I can cancel the alarm by touching the screen.

awesome, glad to hear that!

The WAV file playback is still very staticky

hmm. i wonder if your file and mine are at different bitrates... might be that the hardware can't handle high bitrate when playing in the background. i've uploaded my son's alarm audio to github, can you try that and see if it sounds better?
https://github.com/bitflung/pyportal/bl ... /alarm.wav

and after the alarm is stopped there is a low level sound coming from the speaker... like a 300 baud modem.

i hear that sometimes too. it seems related to the content on the screen to me - if i tap the 'mugsy' button the screen changes and the audio goes away at least for a while. i wonder if the audio API provides a 'power down' type feature for the external components? on mine, at least, the audio is very quiet - like a white noise generator on its absolute lowest volume setting. i wasn't sure if the presence of that audio was from my changes or if it was always (occasionally) there after the audio driver was used... did you ever playback audio and stop the audio without resetting the board before?

I'll try hooking up a better speaker and see if that helps.

i won't stop you from doing that, but i don't think it'll help. maybe the issue is related to having a low impedance circuit driving the speaker (with the original on-board buzzer still attached)? i'll still need to test out cutting the PCB trace for that on-board speaker to see whether that fixes the issue with the 4 ohm speaker drawing too much current.

The low level whistling comes after cancelling the alarm, just after a second sound like a short laser zap plays. That cancel zap sound is not from anything I added so I am not sure where it comes from.

i don't have the laser zap sound on mine. any chance you've got more than one play_file() function running in your code still? if so that might explain away the reason for your audio being static-like still... if you're willing, perhaps try backing up your code.py and running mine as a test?

bitflung
 
Posts: 44
Joined: Mon Mar 11, 2019 3:53 pm

Re: AdaBox 011: fixing problems with Alarm Clock project

by shawng on Tue May 21, 2019 7:30 pm

Thanks again for putting your code out there. So, I loaded it up... I first noticed that all my incorrect touch events went away! Not a single incorrect touch (okay a couple of times the touch did not register but it was probably busy doing something). That is an amazing improvement on what was going on before! Thank you.

However the 12 hour version of the clock never fired my 23h alarm (when my alarm was set to 23 or 11). The alarm did not seem to get the 12 hour treatment so maybe that has something to do with it. I used the original 24h clock code and that worked fine.

I still have my audio issues though. The scratchy audio sound went away when I used a shorter wav file and I noticed the scratchiness on mine happened only after a few seconds. I still get the laser sound about seconds after the short or long wav file stop. Then the speaker keeps emitting that really soft baud-like sound.

The touch fix is great!

shawng
 
Posts: 36
Joined: Sun Oct 23, 2011 6:09 am

Re: AdaBox 011: fixing problems with Alarm Clock project

by bitflung on Tue May 21, 2019 8:07 pm

[edit: I'll try testing with PM alarms. not sure i tried that after making the clock 12hr time. you're probably spot on with how i managed to create a bug in that code]

awesome to hear that my SmartTouch filter worked some magic on your touch screen too!

look for the class definition and check the parameters in there, perhaps the missed touch events was from over zealous filtering. just a couple knobs to turn in there to experiment and perhaps you'll get to config that doesn't filter out intentional touch events :)

as for the audio, I'll play on my end and see if i can reproduce the issue. is it reasonable for you to share the audio file?

bitflung
 
Posts: 44
Joined: Mon Mar 11, 2019 3:53 pm

Please be positive and constructive with your questions and comments.