Hey I need a very simple frequency generator for a project I'm working on. I just need 100Hz on a pin. Thought tone would be nice cause i can change freqency really fast without extra work.
void setup() {
// put your setup code here, to run once:
pinMode(0, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
tone(0, 100);
}
This works perfectly when I upload this exact same no changes sketch to a arduino nano.
On arduino I'm on D0/RX pin. Literally all I change is the board type.
To test my Attiny85 circuit I uploaded this sketch. It works perfectly at around 100Hz.
I've got to be missing something really easy. Anyone have any ideas? Even though the last sketch works I'd rather use tone because eventually I'd like to look up a sensor value and adjust the freqency out between 75Hz and 200Hz without the delays and a lot of extra code.
No change. Could be outputting something (have no way to test, buzzer or scope) but for sure it is not outputting something between 50hz-200hz. manually pulsing it with delays gives expected results.
xl97:
I thought the question was pretty valid. (ie: changing tone in loop()).
The standard tone works when called in quick succession because the timer is not stopped between calls nor is the count reset. Essentially the timer is reconfigured to be exactly the same as it was. This causes pops / ticks / pauses when when the frequency is changed.
I found the pops / ticks / pauses to be intolerable. tone in the Tiny Core stops the timer, resets the count, configures the timer, then starts it. While not completely eliminating the artifacts it does reduce them. If tone is called in quick succession, as in @nosredna000's example, the timer never has a chance to do the first pin toggle.
In other words, I traded quality for reliability. (That trade was not strictly necessary but to get both would have meant significantly more code.)
Your misunderstanding me. I cannot get Tone to output a frequency if I call it in setup or loop.. If I test the circuit and hardware by manually pulseing it with pin high/low delay I get the exact results that the math works out to. For instance on for delay(10) off for delay(10) gives 50 Hz on the nose
This (using delays) doesn't serve my purpose very well because the circuit needs to be working on other things and not halted like with delay. I'd like tone to work.
I'm not following you on what the solution should be.
void loop(){
currentMillis = millis();
if ((currentMillis- previousMillis) >= halfPeriod){ // maybe include checking a flag to decide if on or off
previousMillis = currentMillis + halfPeriod; // with unsigned long halfPeriod = 10; declared earlier
PIND = PIND | 0b00000100; // toggle by writing to input register, D2 for example
}
// do whatever else is going on
}
...with each pin on an ATtiny85 processor. There are thousands of people using that core. Of those thousands, you are the only one claiming tone does not work.
I'm not following you on what the solution should be.
You have been given a complete accurate description of why tone will not work in your original example. You have been given a complete accurate example that does work.
Well tried tone again and again no dice. It is either outputing a frequency that is not what you set it to or the signal flat out isn't like digital on/off.
Went back and re-wrote everything like blink with no delay and that works just like my original basic example.
too bad tone wouldn't work for me. I thought it would be a easy square wave function generator.
I have used tone on an ATTiny and it worked. I wonder if you have a bad core. I haven't tried it lately but I had made several musical projects with an ATTiny85 and speaker using tone.
Try the core from atmel. The latest ide lets you manage different boards.
If you are still stuck, I'll try it again on an ATTiny85 with the core I have.
Hey Shawn, Are you fairly certain the frequency is right? I'm curious if you try the same tone frequency on a nano or uno and the one on a ATtiny85 delievers the same audible note?
Mind sharing a simple piece of code and I'll test here.
So far
void setup() {
// put your setup code here, to run once:
pinMode(0, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
tone(0, 100);
}
and
void setup()
{
tone(0, 100);
}
void loop()
{
}
}
does not give the same results for me as the same code loaded on a nano.
I'm currently using this board type. The links for the two I've got loaded up (neither work) is in my first post. Where do you get the core from atmel??
If the problem is that the frequency is wrong, it's probably a clock fuse problem. If your board is set to attiny85 8mhz internal, you need to burn the bootloader which just sets the fuses. Otherwise set your board to attiny85 1mhz internal.