Hi
I'm working on a project based on the Mega2560 board, since silence is dull I wanted some various squeaks etc, I've built a basic audio output to an 8ohm speaker, crude but it works.
testing in a sketch on its own feeding it an input using:
tone(6, 500, 250);
this works fine
installed into the larger board, the same code is in the startup sequence, and when the board starts it does indeed make a noise so guessing this are not wired up backwards or anything overtly silly.
then I come to the programmes main loop, this is designed to cycle through rapidly without using 'delay()' commands as its reading sensors (connected to a slot car track), and writing to various displays.
I have a state change condition I've tied attaching a noise to using the following:
if (ChangeState)
{
if (TNow - StateChangeTime >= TimeInState)
{
if (StateChangeTone != 0)
{
Serial.print("Pin: ");
Serial.print(SPEAKER);
Serial.print(" Tone: ");
Serial.print(StateChangeTone);
Serial.print("Hz ");
Serial.print(StateChangeToneDuration);
Serial.println("ms");
tone(SPEAKER, StateChangeTone, StateChangeToneDuration);
digitalWrite(DIAGLED, HIGH);
digitalWrite(SPEAKER, LOW);
tone(SPEAKER, StateChangeTone, StateChangeToneDuration);
delay(100);
digitalWrite(DIAGLED, LOW);
}
CurrentState = NextState;
StateChangeTime = TNow;
ChangeState=0;
TimeInState = TimeInNextState;
}
}
the diagnostic LED lights, stays on for the 0.1s delay then goes out, the serial monitor reports the desired tone and duration - the same as the start up noise.
however there is nothing from the speakers.
the rest of the loop is currently not reading sensors (that code has yet to be folded in from another test) but it is updating displays, these are managed via the SPI hardware interface, and work nicely.
the start up tone is played after the SPI.begin() function
things tried so far:
- sticking the diagnostic LED in to see whats going on, I can swap the speaker and LED easily enough, both pins can be set high or set low, neither will generate the PWM based tone.
- swapping to other pins
- increasing and decreasing the duration and frequencies
- sticking the delay() in immediately after the tone command (was wondering if something else in the loop was mucking with those pins, doesn't seem to be)
Q: is there something related to SPI that can shut down the PWM outputs on the Mega? aware I need to leave the pins the SPI stuff connects to free, which I think I'm doing
note there are no "noTone()' commands anywhere.
however, if I set the command to be just
tone(SPEAKER, StateChangeTone);
I get a constant tone output, change to:
tone(SPEAKER, StateChangeTone);
delay(500);
noTone(SPEAKER);
and I'm right back to no sound output at all.
Q: is there something that would be interacting with the timer used by the tone command?