When I call the tone function on a Uno, why do I not have to declare the pinmode in setup? Like so:
// Simplest tone
void setup() {
}
void loop() {
tone (8,500); //on pin 8 output 500 Hz
}
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.
Sign up to join this communityBecause the tone(hertz)
code initializes the pinmode, along with the timer/counter control registers to produce the appropriate frequency.
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
{
uint8_t prescalarbits = 0b001;
long toggle_count = 0;
uint32_t ocr = 0;
int8_t _timer;
_timer = toneBegin(_pin);
if (_timer >= 0)
{
// Set the pinMode as OUTPUT
pinMode(_pin, OUTPUT);
...
See Tone.cpp - line 255 for the source.