Question about race conditions in toneAC2: I was seeing some odd behavior in the standard tone() function that I suspect is due to a race condition between the interrupt handler and tone() inself. The doc says you can call tone() while a previous tone is playing and it will just update the frequency, but tone() can be interrupted at any point by its own interrupt, hence the potential race. In searching the web to see if others had observed this I came across toneAC and toneAC2, and intend to use your library. I don't think I can use toneAC because I need the servo library which uses timer1 on my Uno, but toneAC2 should be just the ticket. So my question: Is there any potential race condition if toneAC2 is called while a previous tone is playing? What is the intended behavior?The main reason I want to use toneAC2 is to take advantage of "twice the volume". I'm currently doing that with tone() using the following push-pull inverter, but software is preferable to hardware:
Now that I look at the code again, I think there is no race condition, but the analysis is subtle. Near the beginning of toneAC you set _tAC2_time. If this unsigned long is set correctly, it will prevent the race condition about which I was concerned in my previous post, since the interrupt handler would then not call noToneAC2. But an unsigned long is four bytes, and setting it on an 8-bit processor is not an atomic operation and can therefore be interrupted. If it is interrupted partway through setting all four bytes, the interrupt handler will run with an invalid value in _tAC2_time, and may or may not call noToneAc2. But I think either case is harmless because nothing that the interrupt handler can change has been read yet by the rest of tomeAC2. Still, I'd move the interrupt disable eariler in toneAC2.
Hello, I work on a "home made" sound player project, using a regular 8ohms solenoid speaker and want to generate "ping" notes.I found your library and your AC trick was exactly what I wanted to make louder sound.But I've a problem with volume control : on full volume (50% duty cycle) the sound is ok, but when I decrease it, it becames twanging like a duck and thus "ping" effect sounds really ugly.Is this a known problem ? I don't understand why of shorter duty cycle distorts sound so much ?I tried another solution, by connecting speaker to a timer for frequency and other speaker end to another timer (with higher frequency) onto which I change duty cycle to simulate a variable low voltage.Result is fine, but in this way, i can't use your AC trick thus volume is less loudBy the way, I've another question :With a 50% duty cycle sound, the signal is like this :(yes, my oscilloscope comes from another century ...)Top line shows one output pin, bottom line shows other pin inverted, thus speaker receives 0 -> +5 at beginning then +5 -> 0. That's ok.With lower volume, the signal becames like this :thus, +5 -> 0 90% of time, then 0 -> +5 during remaining 10% Shouldn't it be better to have something like this :thus 0 -> 0 90% of time, then 5 -> 0 during 5%, and then 0 -> 5 during remaining 5%I achieved this by setting OCRxA to (top / volume) and OCRxB to (top - (top / volume)), and compare flags to "normal"Thus, A is high during the beginning of timer count, then low, then B is set to high during last steps and returns low at end.The result sounds not exactly the same, but is still twanging, thus it doesn't solve my problem, but I'd like to have your opinion in this signal generation method ?Thanks in advance for any help ...
Thanks for your answer.I've to find a piezo speaker to do the test, but i fear sound will not be loud enough.My goal is to make a door bell, thus it have to be eared from everywhere in my house ...
The best solution to this is using a simple amp (a transistor will do). The output of the ATmega just isn't enough to drive a cone speaker at type of a loud volume.Tim
Hi, that's what i did, but to drive the speaker with toneAC, I needed to amplify signal both ways, thus i have to use a H-bridge.As my volume troubles were not solved, I finally used my "second timer" idea, and connected it to the "enable" pin of a H-bridge component. I tried to build my own with Darlingto transistors, but never achieve something working (i think i will never understand how to deal with those weird components !) thus i took a 754410The final result is working and installed on my door since today !I will post detail soon (in french, sorry, but source code is english commented)Thanks for your help
I will post detail soon (in french, sorry, but source code is english commented)
Please enter a valid email to subscribe
We need to confirm your email address.
To complete the subscription, please click the link in the
email we just sent you.
Thank you for subscribing!
via Egeo 16