Arduino Forum

Using Arduino => Microcontrollers => Topic started by: Mixe on Jan 13, 2017, 09:08 am

Title: ATtiny and tone()
Post by: Mixe on Jan 13, 2017, 09:08 am
Hi all

I've been trying to get the tone() function to work on an ATtiny45. It won't happen. When I search for info I find all these threads about how well it works, and even though I copy their sketches they won't produce sound on mine.

I use a small piezo connected to pin 1. The code is really simple:

Code: [Select]

#define PIEZO 1

void setup(){
pinMode(PIEZO, OUTPUT);
}

void loop(){
tone(PIEZO,500);
delay(1000);
noTone(PIEZO);
delay(1000);
}


Like that. Not a sound. Naturally I tried the piezo on an UNO, and that works fine. I also tried the piezo with another tone library (http://w8bh.net/avr/TrinketTone.pdf) and that worked. But it uses delays, so I can't have it. It would interfere with the other code I need to run on the chip.

I use an UNO as ISP, and the board settings are downloaded from board manager. I get no complaints from the compiler when I call tone(), so it seems to be included. I've tried both 1 and 8 MHz.

Any ideas? I'm lost.

Title: Re: ATtiny and tone()
Post by: Mixe on Jan 13, 2017, 11:53 pm
Ok, not a lot of answers. But I found the error myself and thought I'd share it so noone else needs to spend several days on this.

The ATtiny core provided in the board manager is faulty. The tone() function is included but doesn't work. When I installed an older ATtiny core (https://code.google.com/archive/p/arduino-tiny/downloads) it works just fine!

Installing that was a bit of a hassle for me though, since the install instructions in the "ReadMe" file are obsolete. I'm on mac, and had to find the library folder/Arduino15  etc to manually insert the board profiles into the existing boards.txt. Probably a piece of cake for someone more at ease under the hood than me.

Does anyone know who you contact about a found error in the cores provided by the boards manager? 
Title: Re: ATtiny and tone()
Post by: pert on Jan 14, 2017, 12:49 am
The ATtiny core provided in the board manager is faulty.
Which one? Before you were able to install the core via Boards Manager you had to add a URL to File > Preferences > Additional Boards Manager URLs. Don't you think it would have been helpful to specify which URL you used?

Does anyone know who you contact about a found error in the cores provided by the boards manager?  
The author of the core. I can't tell you who that is because you didn't specify which Boards Manager URL you used. I can tell you who you should not contact about the issue, the Arduino IDE developers who have nothing to do with that 3rd party core other than being kind enough to allow 3rd party hardware packages to be installed via Boards Manager. This despite the fact that 3rd party hardware cuts into the sales of Arduino's products which support the development of the Arduino IDE. Now they have to deal with support requests for these cores. For this reason I would recommend you to close your issue report:
https://github.com/arduino/Arduino/issues/5848 (https://github.com/arduino/Arduino/issues/5848)
unless you have determined that this is absolutely a bug with the Arduino IDE or Arduino AVR Boards core, which I really doubt.
Title: Re: ATtiny and tone()
Post by: DrAzzy on Jan 14, 2017, 05:45 pm
Which core? On my core, I haven't touched tone() since I got it working off the fancy timer1, so I'm pretty sure it works. If it's my core, and it doesn't work, that's something i should be made aware of so I can fix it

On my core, use pin 4 fit best results with tone, btw - though I remember testing it on the scope with both pin 4 and someven other pin
Title: Re: ATtiny and tone()
Post by: Mixe on Jan 15, 2017, 10:59 pm
Ok, I downloaded version 1.02 of attiny by David A. Mellis. Is that yours?

And I will naturally close my issue report. I started that for lack of better advice- and, I might add, in an attempt to be helpful.
Title: Re: ATtiny and tone()
Post by: uxomm on Jan 16, 2017, 01:22 am
Ok, I downloaded version 1.02 of attiny by David A. Mellis. Is that yours?
It is https://github.com/SpenceKonde/ATTinyCore (https://github.com/SpenceKonde/ATTinyCore)
Title: Re: ATtiny and tone()
Post by: DrAzzy on Jan 16, 2017, 04:25 am
Huh, I thought he had gotten that working... I thought i remember that coming up a while back

Mine is this one https://github.com/SpenceKonde/ATTinyCore (https://github.com/SpenceKonde/ATTinyCore)

Title: Re: ATtiny and tone()
Post by: pert on Jan 16, 2017, 06:58 am
Here's the damellis/attiny tone() issue report:
https://github.com/damellis/attiny/issues/21 (https://github.com/damellis/attiny/issues/21)
The problem is that damellis/attiny references the arduino:arduino core, it doesn't include its own implementation of tone(). The arduino:arduino core's tone() doesn't work on ATtiny85. That would have been understandable before the ATtiny85 based Gemma was added as an official Arduino board but now it does constitute a bug, which has been reported:
https://github.com/arduino/Arduino/issues/3519 (https://github.com/arduino/Arduino/issues/3519)
So everyone involved is aware of this issue but nobody seems to be motivated to do anything about it. Luckily we have DrAzzy's excellent ATTinyCore to save the day! I recommend you to use that core instead of the much less capable damellis/attiny.
Title: Re: ATtiny and tone()
Post by: Mixe on Jan 16, 2017, 08:19 am
Thanks for the info! Beeing a relative newbie, I followed a tutorial on how to get the ATtiny:s from board manager. In a perfect world that core should have a warning when included by the  Board Manager until issues are solved. Or just remove the reference to the Arduino tone(),  so you'll at least get an error when compiling. But then again, I'm not capable of doing that myself, and in a world of open source you probably shouldn't ask people to do stuff you're not doing yourself.

Anyway, thanks for the info and advice. I have now found a fully working core. Unfortunately, this costed me precious evenings when trying to get a prototype working for a crucial demonstration.
Title: Re: ATtiny and tone()
Post by: pert on Jan 16, 2017, 09:49 am
maybe that core should be removed from Board Manager until issues are solved?
You're misunderstanding how Boards Manager works. There are several official Arduino cores(AVR. SAM, SAMD) and also some Arduino certified packages(e.g. Edison, Galileo, Curie) always available for installation via Boards Manager. Those companies pay a licensing fee to be "Arduino Certified". Originally official and certified cores were all that was available via Boards Manager. Next, Adafruit figured out how to hack the Arduino IDE to make it possible to install the esp8266 core via Boards Manager. Soon after that Arduino opened Boards Manager up to make it super easy for users of the Arduino IDE to install and update 3rd party hardware packages via Boards Manager, which previously had required a slightly more complex manual installation process and didn't have update notifications. So now anyone who wants to make their hardware package installable via Boards Manager can easily do so. All the user needs to do is add the URL to their JSON file to File > Preferences > Additional Boards Manager URLs. When you followed the tutorial that's what you did, maybe you forgot about it. Without that URL damellis/attiny would not be available via Boards Manager. So if you want to remove the "attiny by David Mellis" entry from Boards Manager, just remove that URL(https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json) from your preferences. Arduino has no control over what URLs the user adds to their preferences. It's worth spending a bit of time researching the available options and auditing your choice before deciding on a 3rd party hardware package because there is definitely a lot of variation in the quality of them. If you had clicked the "More info" link in the "attiny by David Mellis" Boards Manager entry and then done a search for tone in the issue tracker you would have quickly seen that tone() is not currently supported by that package.

damellis/attiny is a very simple project because it references the Arduino AVR Boards core. This made it easy to write and less effort to maintain than a project like ATTinyCore, which includes its own core. damellis/attiny still works well for many uses but not having its own core does impose limitations in that any necessary core modifications must be pushed to the Arduino AVR Boards core. I think the Arduino developers would be happy to merge a pull request that adds ATtiny85, etc. support to tone(), someone just needs to make the effort to submit the pull request. damellis/attiny was good in its day and many people are still using it but now we have very actively maintained superior core provided by the hard work of DrAzzy.

I'm actually the person who added Boards Manager installation support to damellis/attiny. It was the first project I did this for right after that capability was added to the Arduino IDE because at that time I was using damellis/attiny and was unaware of ATTinyCore so I thought it would be a useful contribution to the Arduino community. Since then David Mellis has made it the official installation method for the project and it's actually a little tricky to do a manual installation of it now.
Title: Re: ATtiny and tone()
Post by: silly_cone on Feb 06, 2017, 10:42 pm
DrAzzy, do you have plans to update your core to allow use of the tone() function?

I tried manually adding the core the OP linked to, but I ran into a slew of compiler errors.

The totality of the code is:

Code: [Select]
void setup() {
 pinMode(4, OUTPUT);
}

void loop() {
  tone(4, 2000);
}


So I'm pretty sure it has to do with broken pathways.  I've already removed the old core, so I don't have the errors handy, but does anyone know if there are detailed instructions for manually installing new cores for IDE 1.6.9?
Title: Re: ATtiny and tone()
Post by: DrAzzy on Feb 06, 2017, 11:12 pm
My core supports tone() - if it's not working, that's news to me.
Last I checked on the tiny85 with a simple test sketch (something like what you have), the code compiled and produced output at the specified frequency (verified with the scope, with both the hardware timer pin, and a different pin using the interrupt).

Since you're using 1.6.9, make sure that LTO is disabled in the tools menu - the new compiler version that supports it came with 1.6.11 (specifically, AVR boards 1.6.13 is what brings the new compiler version - It comes with IDE version 1.6.11) - if you have that enabled on a version that doesn't support it, nothing will compile.

Assuming that doesn't fix it, please post the full text of any and all error messages.
Title: Re: ATtiny and tone()
Post by: silly_cone on Feb 07, 2017, 04:35 pm
Hi DrAzzy, I'm still not having any luck with the piezo, unfortunately.  There are no compiler errors or anything that pop up.  The errors I mentioned above were from using the Tiny Core and I'm pretty sure it had to do with broken directory pathways for accessing the compiler.

I'm using your core (Attiny Core by Spence Konde from the link you gave above) version 1.1.2, and I've tried several combinations of clock speeds still to no avail.  Currently, I have Timer1 set to CPU, Clock is 8 MGHz internal, BOD and LTO both disabled.  I've tried using pins 0, 1, 2, and 4.  And I've tried several attiny85 chips as well.

The piezo I'm using tests fine when plugged into a nano.  But all I get from the attiny is a single small click right when its powered on.  When I plug an LED into the pins and send it a PWM signal, the LED lights up like it should.  The only way I'm able to get any sound of the piezo at all is to send it a PWM signal of ~250 or less.  Then it buzzes, but its not a very clean sound.
Title: Re: ATtiny and tone()
Post by: Mixe on Feb 07, 2017, 05:07 pm
That sounds exactly like the errors I got when I was using David A Mellis' ATtiny core. Are you sure you are using the right core? The one I got to work was this one (https://code.google.com/archive/p/arduino-tiny/downloads).
Not without some hiccups though- I too get error messages when compiling, but that's just phrase confusement and the compiler seems to correct it automatically. They look like this:
Code: [Select]

Board arduino:avr:attiny85at1 doesn't define a 'build.board' preference. Auto-set to: AVR_ATTINY85AT1


It  should be easy enough to change the faulty preference phrase, but it works and I haven't bothered.



Title: Re: ATtiny and tone()
Post by: silly_cone on Feb 07, 2017, 05:14 pm
I uninstalled the Damellis core both from the preferences and the board manager.  It no longer appears anywhere in the program.  The Spence Konde ATTiny Core is the only one that shows up.

I'll go back and try the other Tiny Core from here (https://code.google.com/archive/p/arduino-tiny/downloads) and see if I can get past those error codes (they do look the same as what I was getting).  What did you do to correct them?  And I apologize if you already covered this in a previous post.
Title: Re: ATtiny and tone()
Post by: Mixe on Feb 07, 2017, 05:17 pm
No worries, I haven't. Like I wrote: the errors are automatically corrected by the compiler, like it says in the error message, and the code compiles anyway. So I haven't done anything to correct them.
Title: Re: ATtiny and tone()
Post by: silly_cone on Feb 07, 2017, 05:20 pm
Weird, mine would not compile using that core; it gave a "Problem compiling for ...." message.   I'll go back and give it another go, then report back.
Title: Re: ATtiny and tone()
Post by: Coding Badly on Feb 07, 2017, 06:01 pm
I too get error messages when compiling...
That is a warning.  The IDE produces it.  It can be ignored.

Title: Re: ATtiny and tone()
Post by: silly_cone on Feb 07, 2017, 06:10 pm
here is the complete compiler output:

Code: [Select]

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware "C:\Program Files (x86)\Arduino\hardware" -hardware "C:\Users\Alan\AppData\Local\Arduino15\packages" -hardware "C:\Users\Alan\Documents\Arduino\hardware" -tools "C:\Program Files (x86)\Arduino\tools-builder" -tools "C:\Program Files (x86)\Arduino\hardware\tools\avr" -tools "C:\Users\Alan\AppData\Local\Arduino15\packages" -built-in-libraries "C:\Program Files (x86)\Arduino\libraries" -libraries "C:\Users\Alan\Documents\Arduino\libraries" -fqbn=tiny:avr:attiny85at16p -ide-version=10609 -build-path "C:\Users\Alan\AppData\Local\Temp\build6b670ed53e9cccc2da9fe5b6ba847dfa.tmp" -warnings=none -prefs=build.warn_data_percentage=75 -verbose "C:\Users\Alan\AppData\Local\Temp\arduino_modified_sketch_850444\sketch_feb07a.ino"
C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware "C:\Program Files (x86)\Arduino\hardware" -hardware "C:\Users\Alan\AppData\Local\Arduino15\packages" -hardware "C:\Users\Alan\Documents\Arduino\hardware" -tools "C:\Program Files (x86)\Arduino\tools-builder" -tools "C:\Program Files (x86)\Arduino\hardware\tools\avr" -tools "C:\Users\Alan\AppData\Local\Arduino15\packages" -built-in-libraries "C:\Program Files (x86)\Arduino\libraries" -libraries "C:\Users\Alan\Documents\Arduino\libraries" -fqbn=tiny:avr:attiny85at16p -ide-version=10609 -build-path "C:\Users\Alan\AppData\Local\Temp\build6b670ed53e9cccc2da9fe5b6ba847dfa.tmp" -warnings=none -prefs=build.warn_data_percentage=75 -verbose "C:\Users\Alan\AppData\Local\Temp\arduino_modified_sketch_850444\sketch_feb07a.ino"
Warning: Board tiny:avr:attiny85at16p doesn't define a 'build.board' preference. Auto-set to: AVR_ATTINY85AT16P
Warning: Board tiny:avr:attiny85at8 doesn't define a 'build.board' preference. Auto-set to: AVR_ATTINY85AT8
Warning: Board tiny:avr:attiny85at1 doesn't define a 'build.board' preference. Auto-set to: AVR_ATTINY85AT1
Warning: Board tiny:avr:attiny85at16e doesn't define a 'build.board' preference. Auto-set to: AVR_ATTINY85AT16E
WARNING: Spurious .github folder in 'Adafruit MMA8451 Library' library
Warning: platform.txt from core 'Arduino Tiny' contains deprecated tools.avrdude.cmd.path={runtime.ide.path}/hardware/tools/avr/bin/avrdude, automatically converted to tools.avrdude.cmd.path={path}/bin/avrdude. Consider upgrading this core.
Warning: platform.txt from core 'Arduino Tiny' contains deprecated tools.avrdude.config.path={runtime.ide.path}/hardware/tools/avr/etc/avrdude.conf, automatically converted to tools.avrdude.config.path={path}/etc/avrdude.conf. Consider upgrading this core.
Warning: platform.txt from core 'Arduino Tiny' contains deprecated recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} "{build.path}/{archive_file}" "{object_file}", automatically converted to recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} "{archive_file_path}" "{object_file}". Consider upgrading this core.
Warning: platform.txt from core 'Arduino Tiny' contains deprecated recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mmcu={build.mcu} -o "{build.path}/{build.project_name}.elf" {object_files} "{build.path}/{archive_file}" "-L{build.path}" -lm, automatically converted to recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mmcu={build.mcu} -o "{build.path}/{build.project_name}.elf" {object_files} "{archive_file_path}" "-L{build.path}" -lm. Consider upgrading this core.
"avr-g++" -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections  -w -x c++ -E -CC -mmcu=attiny85 -DF_CPU=16000000L -DARDUINO=10609 -DARDUINO_AVR_ATTINY85AT16P -DARDUINO_ARCH_AVR  "-IC:\Users\Alan\Documents\Arduino\hardware\tiny\avr\cores\tiny" "C:\Users\Alan\AppData\Local\Temp\build6b670ed53e9cccc2da9fe5b6ba847dfa.tmp\sketch\sketch_feb07a.ino.cpp" -o "nul"
"avr-g++" -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections  -w -x c++ -E -CC -mmcu=attiny85 -DF_CPU=16000000L -DARDUINO=10609 -DARDUINO_AVR_ATTINY85AT16P -DARDUINO_ARCH_AVR  "-IC:\Users\Alan\Documents\Arduino\hardware\tiny\avr\cores\tiny" "C:\Users\Alan\AppData\Local\Temp\build6b670ed53e9cccc2da9fe5b6ba847dfa.tmp\sketch\sketch_feb07a.ino.cpp" -o "nul"
"avr-g++" -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections  -w -x c++ -E -CC -mmcu=attiny85 -DF_CPU=16000000L -DARDUINO=10609 -DARDUINO_AVR_ATTINY85AT16P -DARDUINO_ARCH_AVR  "-IC:\Users\Alan\Documents\Arduino\hardware\tiny\avr\cores\tiny" "C:\Users\Alan\AppData\Local\Temp\build6b670ed53e9cccc2da9fe5b6ba847dfa.tmp\sketch\sketch_feb07a.ino.cpp" -o "C:\Users\Alan\AppData\Local\Temp\build6b670ed53e9cccc2da9fe5b6ba847dfa.tmp\preproc\ctags_target_for_gcc_minus_e.cpp"
exec: "avr-g++": executable file not found in %PATH%
Error compiling for board ATtiny85 @ 16 MHz  (internal PLL; 4.3 V BOD).
Title: Re: ATtiny and tone()
Post by: silly_cone on Feb 07, 2017, 06:34 pm
Okay, I found instructions here (http://www.technoblogy.com/show?190D) for getting the compiler to work.  It now compiles without any errors, but I am still getting no tone.

The closest I can get is to just use PWM for the rough sounding tone.  If I set the clock speed to 1 MGHz, and the frequency to 2,000 then I get an unsteady output of clicks, but if I change the clock speed and keep everything else stable, I don't even get that.  If I keep the clockspeed at 1MGHz and set the output frequency to 10,000, then I get a buzzing, but not a clean tone.  Raising the frequency above 10,000, even as high as 50,000 makes no further change in the sound.
Title: Re: ATtiny and tone()
Post by: DrAzzy on Feb 10, 2017, 08:11 pm
Sorry for the late reply here - I was sick in bed all week.

The compiler output indicates that you're not using my core - you may have it installed, but you also have at least one other core for the ATTiny, and you've selected a board definition from that core, not from mine.

The name of the board def supplied by my core for the Tiny85 series is "ATtiny25/45/85", and it will be under the ATTinyCore heading - you can then select the clock speed and which specific chip you're using from the tools menu.

If you use my core, Tone() should work.
Title: Re: ATtiny and tone()
Post by: silly_cone on Feb 13, 2017, 05:39 pm
Hi DrAzzy, I've switched a lot of things around a few times, so to save you from having to read everything in detail, I'll sum it up for you: 

I tried using the latest version of your core at multiple clock speeds.  Everything compiled with no issues, but there was simply no tone.  I tried using several pins, and several attiny's, all with the same effect.  I even attempted multiple piezos of different types.  The piezos work when hooked up to a nano, but the attiny's won't work.  To verify that the pins were working, I tested them with PWM on an LED, which worked fine.  I'm pretty positive that your core and no others were at play because I uninstalled them from the boards manager and preferences before loading yours, then restarted arduino before compiling.

Then I switched to a different core that I linked to in an above post, which also did not work.  That was the core that produced the compiler errors that, you are correct, indicate that your core was not being used.
Title: Re: ATtiny and tone()
Post by: silly_cone on Feb 15, 2017, 03:31 pm
after many frustrating hours of repeated failure, I've decided the only way this is going to work is to use the TinyTone() function that can be found here (http://www.technoblogy.com/show?KVO). 

I'm beyond clueless why neither DrAzzy's core nor the Tiny Core linked to by the OP work in my setup, but they don't.  They fail at every clock speed, every frequency, on every pin, every chip, and every buzzer.
Title: Re: ATtiny and tone()
Post by: DrAzzy on Feb 15, 2017, 05:02 pm
I am too. I haven't had a chance to look into this, I haven't been feeling well the past week or so.
Title: Re: ATtiny and tone()
Post by: kprims on Feb 15, 2017, 06:45 pm
My limited tests with attiny85 using a $.95 development board and ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager.

Code: [Select]
void setup() {
 pinMode(4, OUTPUT);
}

void loop() {
  tone(4, 2000);
}


Had to hook up buzzer to vcc and other lead to pin 4. If no tone, reverse leads.

Next sketch.

Code: [Select]
#define PIEZO 1

void setup(){
pinMode(PIEZO, OUTPUT);
}

void loop(){
tone(PIEZO,500);
delay(1000);
noTone(PIEZO);
delay(1000);
}


On this sketch it works for me with leads on gnd and pin1. Again, you may have to reverse leads to get tone.
Title: Re: ATtiny and tone()
Post by: silly_cone on Feb 15, 2017, 08:52 pm
SONNOFABITCH!!!!!!  It NEVER occurred to me to reverse the leads!  I've always used piezos with the the 2nd lead connected to GND, not VCC.  Even with this same buzzer and same function on a nano, the leads don't need reversed.  But for the attiny for some reason they do!?

I can't believe I've been stumped for like a week over backwards leads!!!!!

Thank you profusely for making this as easy as it should've been all along!!!!!!!!   :smiley-red:
Title: Re: ATtiny and tone()
Post by: DrAzzy on Feb 15, 2017, 09:17 pm
*adds a piezo buzzer to his ebay shopping cart*

That's bizzaro - I've never worked with those things before (obviously - and sort of strangely, considering that I have literally hundreds of drawers of parts, plus a 2 cubic foot bin of parts in static bags because I ran out of drawers) - I know most are polarized. But if it works on the nano in either polarity, it should work on a tiny in either polarity.... Unless you just got lucky when you hooked it up to the nano?

Also kprims, thanks loads, this is one thing I don't have to do.
Title: Re: ATtiny and tone()
Post by: silly_cone on Feb 15, 2017, 09:56 pm
okay, so polarity was NOT the problem.  I did some more testing and narrowed it down to the need for a minimum delay of 1 millisecond for the tone function to work.

This code produces a tone:
Code: [Select]

void setup(){
pinMode(4, OUTPUT);
}

void loop(){
    tone(4,2000);
    delay(1);
}


this code produces nothing except tears and anger:
Code: [Select]

void setup(){
pinMode(4, OUTPUT);
}

void loop(){
    tone(4,2000);
    //delay(1);
}


I didn't think the polarity issue made sense considering that the buzzer worked fine on the nano regardless of polarity, but who was I to question success?  Then I made some code changes and got nothing, so I knew it had to be something different.  Still not sure why the delay(1) is needed on the attiny85 and not the nano, but it works, so I'm happy.

Here is the specific buzzer I'm using, FYI: CPT-9019S-SMT (http://www.digikey.com/product-detail/en/cui-inc/CPT-9019S-SMT-TR/102-3761-1-ND/6012450)
Title: Re: ATtiny and tone()
Post by: kprims on Feb 15, 2017, 10:59 pm
This is getting beyond my pay grade. :-)

But just to tag along. Both types of piezo's I have will work with the original sketch. I do have a small 8 ohm 2w speaker that will not work with  the original  sketch, but will work with the delay.
Title: Re: ATtiny and tone()
Post by: DrAzzy on Feb 15, 2017, 11:10 pm
Aaaaha!

Thanks for that. I think I know what the problem is, it's gotta be resetting the timer count when tone() gets called, so you get considerably different output, it's not going to be fun to fix, since it's #ifdef hell and timer1 on the tiny85 is weird. I mean, I can't imagine what else it could be, but that explanation totally makes sense.

I suspect your tone will sound subtly different with delay(1) vs delay(10) there!

I'll make an issue for it. https://github.com/SpenceKonde/ATTinyCore/issues/111
Title: Re: ATtiny and tone()
Post by: silly_cone on Feb 15, 2017, 11:14 pm
well glad I could be of assistance.  :-)

I'm glad there's a rational explanation; I was starting to feel l like I was going a little crazy.  :P
Title: Re: ATtiny and tone()
Post by: silly_cone on Nov 08, 2017, 11:10 pm
Alright, back to this again...

Anyone have any insight into why analogWrite would stop working on PB4 when tone() function is used in the sketch?

analogWrite still works on PB0 and PB1, but if I use the tone() command anywhere, even if its ended by a call to noTone(), analogWrite on PB4 is still dead.
Title: Re: ATtiny and tone()
Post by: Coding Badly on Nov 08, 2017, 11:16 pm

Which tone?  You have used at least three variations.

Title: Re: ATtiny and tone()
Post by: DrAzzy on Nov 08, 2017, 11:26 pm
Alright, back to this again...

Anyone have any insight into why analogWrite would stop working on PB4 when tone() function is used in the sketch?

analogWrite still works on PB0 and PB1, but if I use the tone() command anywhere, even if its ended by a call to noTone(), analogWrite on PB4 is still dead.
Which core is it? Is it my core?

If it's my core, please make an issue here: https://github.com/SpenceKonde/ATTinyCore/issues (https://github.com/SpenceKonde/ATTinyCore/issues) - my guess from the problem description is that it's an x5 series, but please confirm that. I also *love* code that reproduces problems, even really simple ones.
Title: Re: ATtiny and tone()
Post by: silly_cone on Nov 09, 2017, 03:10 pm
That's right, its an ATTiny85. 


This code results in proper output signal on PB4:

Code: [Select]
void setup() {
  pinMode(4, OUTPUT);
}

void loop() {

  //tone(2, 5000);
  delay(1);
  //noTone(2);
  delay(1);
 
  for (int i=150; i>85; i--){
    analogWrite(4, i);
  }
  delay(2000);
  digitalWrite(4, LOW);
  delay(2000);
}


This code results in nothing from PB4. but interestingly, it produces a ~25 ohm connection to ground that isn't present otherwise.

Code: [Select]
void setup() {
  pinMode(4, OUTPUT);
}

void loop() {

  tone(2, 5000);
  delay(1);
  noTone(2);
  delay(1);
 
  for (int i=150; i>85; i--){
    analogWrite(4, i);
  }
  delay(2000);
  digitalWrite(4, LOW);
  delay(2000);
}
Title: Re: ATtiny and tone()
Post by: silly_cone on Nov 09, 2017, 03:45 pm
And yes, I'm using your core.  
Title: Re: ATtiny and tone()
Post by: silly_cone on Nov 09, 2017, 07:05 pm
Update:  Fixed; I was able to get around the problem using the TimerFreeTone library.
Title: Re: ATtiny and tone()
Post by: DrAzzy on Nov 09, 2017, 10:30 pm
This is a bug in my core. noTone() does not properly restore the state of TCCR1* registers.
Title: Re: ATtiny and tone()
Post by: DrAzzy on Oct 09, 2018, 06:06 am
Issue with calling tone() repeatedly with the same parameters in a tight loop fixed for ATTinyCore 1.2.0
Title: Re: ATtiny and tone()
Post by: hpschawla on Mar 05, 2019, 03:37 am
check this webpage out = TinyTone() = http://www.technoblogy.com/show?KVO
It helped my finish my ATTiny85 project!