Grove Buzzer

Digital on / off buzzer

Hello World

Import programSeeed_Grove_Buzzer

Seeed Grove Buzzer example. The buzzer buzzes on for .5 second and then off for .5 second.

Library

Import programSeeed_Grove_Buzzer

Seeed Grove Buzzer example. The buzzer buzzes on for .5 second and then off for .5 second.

Datasheet

http://www.seeedstudio.com/wiki/File:Grove-Buzzer_V1.1_eagle.zip

Notes

A simple buzzer. Its either fully on or fully off. Controlled via a digital pin.

Examples

Busy Wait Buzzer

This example buzzes the buzzer using the wait() function which means you cant do anything else while waiting.

busywait_buzzer.cpp

#include "mbed.h"

DigitalOut buzzer(D8);

int on = 1, off = 0;

int main() {    
    while(1){
        buzzer = on;
        wait(.5);
        buzzer = off;
        wait(.5);
    }
}

Timer Countdown Driven Buzzer

This example uses a Ticker to implement a countdown timer. The Ticker class lets you set it and forget it. Once you set up the ticker and give it a callback function to call when its done ticking you don't have to mess with it again. Best of all you can do something else while its ticking down, like count the ninjas on your ceiling, or be awesome.

timer_buzzer.cpp

#include "mbed.h"

DigitalOut buzzer(D8);
Ticker buzzer_timer;

int on = 1, off = 0; 

void timer_handler(){
    buzzer = buzzer ^ 1; // invert the buzzer value    
}

int main(){
    buzzer = off; // initialize buzzer
    buzzer_timer.attach(&timer_handler, 0.5); // Every .5s call the timer_handler function
    
    while(1){
        // do something else    
    }
}

You need to log in to post a discussion