Working tone() substitute for Leostick

A shrunk down Leonardo-compatible board, thumb drive sized with native USB support. [Product info]
Post Reply
lawsy
Posts: 4
Joined: Tue Sep 25, 2012 9:02 am

Working tone() substitute for Leostick

Post by lawsy » Sun Oct 07, 2012 2:08 am

This is a function that replicates the Arduino tone() function for playing sounds on the built in piezo buzzer. The Arduino built in tone() function crashes the Leostick.

It is based heavily on the code found here:
viewtopic.php?f=27&t=1634

Place this somewhere in your sketch:

Code: Select all

void Tone(int buzzer, int Freq, int duration){  
  int uSdelay = 1000000 / (Freq * 2);
  unsigned long ending = millis() + duration;
  while(millis() < ending){
    digitalWrite(buzzer, HIGH);
    delayMicroseconds(uSdelay);
    digitalWrite(buzzer, LOW);
    delayMicroseconds(uSdelay);
  }
} 
Call in with Tone(x, y, z);

Note the capital T on Tone. x should be the number of the piezo pin (11 by default). y should be the note you want to play, and z is the duration in milliseconds.

The three arguments match the original tone() function.

Example use:

Code: Select all

int buzzer = 11;

void setup(){
pinMode(buzzer, OUTPUT);
}

void loop(){
Tone(buzzer, 500, 500); // play a mid range note for half a second
delay(2000); // wait two seconds
}

roguewave
Posts: 5
Joined: Sat Oct 13, 2012 3:01 pm

Re: Working tone() substitute for Leostick

Post by roguewave » Sat Oct 13, 2012 3:23 pm

Thank you for this. I might just add it as a library so I can include it and reference the method as if it's tone(), but obviously just capitalizing the T now. Nice work!

JonathanM
Posts: 6
Joined: Sun Apr 21, 2013 11:40 am

Re: Working tone() substitute for Leostick

Post by JonathanM » Sun Apr 21, 2013 11:46 am

Wrote myself some code that allowed me to use the buzzer while still running loop code.

Used this as I needed to trigger the buzzer from external events (like a button) for user feedback.

However I didn't want to stop/block other operations.

Note: Doing this will mean the buzzer sound will be affected by other code being run in your loop

Global Variables

Code: Select all

// BUZZER VARIABLES
int buzzer_running = 0;
int buzzer_duration = 0;
int buzzer_delay = 0;
int buzzer_state = 0;
unsigned long buzzer_start_millis = 0;
unsigned long buzzer_offset_millis = 0;

// GENERIC MILLIS VARIABLES
unsigned long current_millis = 0;
unsigned long interval_millis = 0;
Buzzer Loop Function (call on every run of loop)

Code: Select all

// BUZZER LOOP
void buzzer_loop() {
	// CHECK IF THE BUZZER IS RUNNING
	if(buzzer_running)
	{	// GET THE CURRENT MILLIS
		current_millis = millis();
		// IS THIS THE FIRST RUN OF CODE ?
		if(!buzzer_start_millis)
		{	buzzer_start_millis = buzzer_offset_millis = current_millis;
			// GREEN HIGH AT START OF BUZZER CODE
			digitalWrite(LED_GREEN, HIGH);	}
		else
		{	// CHECK THE BUZZER SHOULD STILL BE RUNNING
			interval_millis = (current_millis - buzzer_start_millis);
			if(interval_millis >= buzzer_duration)
			{	// BUZZER SHOULD BE OFF NOW
				digitalWrite(BUZZER,	LOW);
				buzzer_start_millis = buzzer_running = buzzer_state = 0;
				// GREEN LOW AT END OF BUZZER CODE
				digitalWrite(LED_GREEN, LOW);
				// TURN THE BUZZER OFF
				digitalWrite(BUZZER,	buzzer_state);
				return;	}	}
		// CHECK THE DIFFERENCE IN OFFSET
		interval_millis = (current_millis - buzzer_offset_millis);
		if(interval_millis >= buzzer_delay)
		{	// FLIP THE BUZZER OUTPUT IO
			buzzer_state = !buzzer_state;
			digitalWrite(BUZZER,	buzzer_state);
			buzzer_offset_millis = current_millis;	}	}	}
Call in your code when you want to turn the buzzer on

Code: Select all

// BUZZER
void buzzer(int frequency,int duration)
{	buzzer_delay = (1000/(frequency*2));
	buzzer_duration = duration;
	buzzer_running = 1;	}

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: Working tone() substitute for Leostick

Post by angusgr » Fri May 10, 2013 12:14 am

Just a quick note that if you grab the latest LeoStick board profile, the tone() function will work on LeoStick V2.0.

If you have V1.0 it unfortunately still doesn't work, so this code is an excellent substitute, or you can upgrade the bootloader to V2.0 (there's a link on the Getting Started page, too.)

Post Reply