Tone

From Pinguino-Wiki
Jump to: navigation, search

Contents

Name

tone

This command is not currently supported by the Pinguino IDE

Description

Generates a square wave of the specified frequency (50% duty cycle) on a pin.
A speaker or buzzer can be connected to the pin to play tones.
Duration optional, if not specified the wave continues until a call to noTone().
Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).

      Board                    Min freq(Hz)     Max freq(Hz)
 ---------------             ---------------   --------------
Pinguino                           31              65535
Uno, Mega, Leonardo                31              65535
Most other AVR boards              31              65535
Zero                               41             275000
Gemma - Not implemented            -                 -
Due   - Not implemented            -                 -

Syntax

tone(pin, frequency)
tone(pin, freq_hz, duration_ms)
tone(u8 pin, u8 frequency, u16 duration)
pin
IDE pin to generate the tone

Tone and sound use PWM, the device must be connected to a PWM pin.

 Pinguino x5(k)50    : 11, 12          (CCP1, CCP2)
 Pinguino 47J53      : 4, 5, 6, 7, 17  (CCP4, CCP5, CCP6, CCP7, CCP8)
 PIC32 Olimex Micro  : 10, 11, 12, 13  (OC2, OC3, OC4, OC5)
 Other chips are unknown
frequency
frequency in hertz of the tone
duration
tone duration in milliseconds (optional)

if no duration is given, tone plays until noTone() command

Example 1

tone(3, 1000); // pin=3, tone=1000hz, continuous tone
tone(3, 440, 250); // pin=3, tone=440hz, duration 250ms (1/4sec)
tone(3, 150, 250);
tone(3, 1000);
noTone(3);

Example 2

Twinkle Twinkle Little Star
C C G G A A G
F F E E D D C
G G F F E E D
G G F F E E D
C C G G A A G
F F E E D D C
/* +---------------------------------------------------------------------+
   | tone_test.pde             by Jan Zumwalt         rev 2015.12.17     |
   +---------------------------------------------------------------------+
   | Copyright: COPYRIGHT by Jan Zumwalt                                 |
   |            Licensed under the GNU public license.                   |
   | Build    : Olimex Pic32 Micro dev brd w/Pinguino.cc IDE (V11)       |
   | Purpose  : This program demonstrates the tone command.              |
   +---------------------------------------------------------------------+  */
 
spkr = 10; // speaker pin
int C1 =; 
int D1 =; 
int E1 =; 
int F1 =; 
int G1 =; 
int A1 =; 
int B1 =; 
 
setup() {
  // nothing to do
}
 
loop() {
  // Twinkle Twinkle Little Star
  stanzaC();  // 1st stanza
  stanzaF();  // 2nd stanza
  stanzaG();  // 3rd stanza
  stanzaG();  // 4th stanza
  stanzaC();  // 5th stanza
  stanzaF();  // 6th stanza
  notone();   // music off
  delay(3000); // wait three seconds then play again
}
 
stanzaC() {
  tone(spkr,C1, 250);
  tone(spkr,C1, 250);
  tone(spkr,G1, 250);
  tone(spkr,G1, 250);
  tone(spkr,C1, 250);
  tone(spkr,F1, 250);
}
 
stanzaF() {
  tone(spkr,F1, 250);
  tone(spkr,F1, 250);
  tone(spkr,E1, 250);
  tone(spkr,E1, 250);
  tone(spkr,D1, 250);
  tone(spkr,D1, 250);
  tone(spkr,C1, 250);
}
 
stanzaG() {
  tone(spkr,G1, 250);
  tone(spkr,G1, 250);
  tone(spkr,F1, 250);
  tone(spkr,F1, 250);
  tone(spkr,E1, 250);
  tone(spkr,E1, 250);
  tone(spkr,D1, 250);
}

Bugs

See Also

Sound Library, 
sound
tone
play
noTone

Comments

Notes and frequency

10 octave reference chart 9 octave reference chart using 440hz "A" note.

Personal tools