I am experimenting with the Tiny85 and have a simple circuit consisting of the chip in a SparkFun Tiny AVR Programmer and a Mini Speaker connected between PB3 (pin 2) and ground.
Using tone() / delay() / noTone() I've been making it beep and boop for the last few days, but suddenly, that doesn't work any more. I wrote the simplest sketch I could think of to try and figure out what is going on:
#define SPK 3
void setup() {
pinMode(SPK, OUTPUT);
}
void loop() {
// digitalWrite( SPK, HIGH ); delay( 1 ); // prduces a tone
// digitalWrite( SPK, LOW ); delay( 1 );
tone( SPK, 1000, 250 ); // silence !
delay( 250 );
}
By toggling the pin with digitalWrite(), I can get a continuous tone on the speaker. If I comment that and use the tone() function, I get silence.
I can't think why tone() which was working fine, suddenly stopped functioning as expected. With the Tiny85 in a bread-boarded circuit, debugging options are few.
I tried a different Tiny85 chip, and I tried changing SPK to all the other outputs PB0-5 with the same result. I can produce a tone by toggling the pin but not by using the tone() function.
Can someone suggest a cause / cure? Am I missing something obvious?
Which ATTiny core are you using? If you're using DrAzzy/Spence Konde's Tinycore then try rolling it back a release or two (using the Boards Manager). I know he recently changed something to do with timer initialisation that was breaking one of the PWM channels, maybe he's accidentally broken tone().
I've got these installed:
ATTinyCore by Spence Konde version 1.2.3
attiny by David A. Mellis version 1.0.2
Not sure which is being used.
In any case, these were installed weeks ago and have not been changed by me. The tone() function was working fine up until yesterday, when it suddenly decided it wasn't going to work any more.
Investigating some more...
netfoot:
I've got these installed:
ATTinyCore by Spence Konde version 1.2.3
attiny by David A. Mellis version 1.0.2
Not sure which is being used.
It'd be a good idea to figure out which one you're using so that people can help you. I'd recommend getting rid of the David Mellis one- it hasn't been actively updated in years.
When using Spence's core the best pin to use tone() with is pin 4- try that and see what happens, if it works on that then we're making progress.
For the sake of completeness can you post the sketch that is giving you a tone using digitalWrite() to toggle the pin?
BJHenry:
It'd be a good idea to figure out which one you're using so that people can help you.
It seems I was using attiny on one computer and ATTinyCore on the other. I have eliminated the David Mellis core so both machines are now identically configured.
For the sake of completeness can you post the sketch that is giving you a tone using digitalWrite() to toggle the pin?
The OP gave a sketch with the working code commented out, and the non-working code commented in.
More testing...
The machine that had attiny on it was causing the trouble. Eliminating that and using ATTinyCore on both machines has solved the problem.
I'm glad to hear you got it working. Thanks for letting us know 