Code Sample - Gadgeteer using eblock Piezos

FYI, just posted another code sample…this one using eblock piezos with Gadgeteer:

[url]http://code.tinyclr.com/project/407/gadgeteer-sounds-using-eblock-piezos/[/url]

I know this one will (hopefully) soon be obsoleted by whoever wins the GHI Gadgeteer piezo contest, but for now, this is the easiest way to get noises from your FEZ Spider. :slight_smile:

Thanks for sharing

My pleasure…hope someone finds it helpful!

Hello,

I did not see any code when following this link, only another list of forum topics?

At any rate I haven’t seen any examples that do anything as simple as what I need. Don’t laugh but I need to get a piezo to simply beep each time I click a button module as a confirmation of the button click.

I am using a spider kit with a button on socket 6 and eblock on socket 8, and piezo is on pin 9 of the eblock.

Any suggestions?

Thanks!

Hello,

I did not see any code when following this link, only another list of forum topics?

At any rate I haven’t seen any examples that do anything as simple as what I need. Don’t laugh but I need to get a piezo to simply beep each time I click a button module as a confirmation of the button click.

I am using a spider kit with a button on socket 6 and eblock on socket 8, and piezo is on pin 9 of the eblock.

Any suggestions on code examples would great.

Thanks!

Those are not topics, but code posts.

We will update the links correctly but you can search for it now if you like.

Ok I searched and found your code example for using two piezos and pots. I am an absolute beginner at programming trying to build a prototype for a device using the Spider kit so even the very simple things are way over my head! I tried to peice together getting my peizo to beep by pressing the button from your example but I get nothing but errors.

Is there any simple example you could provide for what I am trying to accomplish?

Thanks!

have you read the beginners ebook in the support/resources section of this site?

@ jaywrs - Start as small as possible. If you’re using two piezos, drop back to one piezo.

The key pieces are as follows:

[ol]Connect the eBlock expansion module to a socket that supports PWM (PWM = Pulse Width Modulation, which is how you drive the piezo…the mainboard sockets supporting PWM are marked with a P)
In order to be able to drive the Piezo, you need to make sure that the Piezo eblock is plugged into one of the PWM pins. each of the available pins (3-9) are marked on the eblock expansion module. If you check out the socket map for Gadgeteer (GHI Electronics – Where Hardware Meets Software), you’ll see that for socket P, the PWM pins are 7, 8, and 9, so you MUST plug the Piezo eblock into one of these three sockets on the module.
Once you have the module and eblock connected physically, you need to create a new Gadgeteer project, and drag the eblock expansion module from the toolbox onto the design surface, and connect it to the port you connected it to in step 1. You can simply click on the socket on the module picture, and drag it to the correct socket number on the mainboard.
Next, open Program.cs, and just below the class declaration, add:

static GT.Interfaces.PWMOutput Piezo1;

Then, inside the ProgramStarted method, add the following line to initialize the Piezo eblock (make sure your code matches the pin number where the Piezo eblock is physically plugged in):

Piezo1 = eBlockExpansion.SetupPWMOutput(GT.Socket.Pin.Nine);

Finally, add a call to the PWM Set method to sent a PWM signal to the Piezo, like so (this code will send the note A to the piezo, at 50% duty cycle. Be aware that the note will continue to play until you power off the board, or you send another Set command with the duty cycle set to 0.):

Piezo1.Set (440, 50); 

Run the project to test.[/ol]

That should at least get you to the point where you can verify that the piezo is connected properly and working. Then you can gradually add in the button, potentiometer, timer, etc.

Direct link to the project, in case someone else is looking for it, is:

http://www.tinyclr.com/codeshare/entry/345

Ok I’ve got the Piezo to make a constant tone. I’ve tried to add in the button and timer based on some things I’ve seen in other timer and piezo examples but I’m not getting things in the correct spot or something because I get several errors. If I am thinking correctly in order to get a single beep each time I click my spider button module I will need to have a button pressed event handler that starts the timer event (around 2-300 ms) and then have the piezo.set(4000,50) " while timer running is true" ?

I am an absolute beginner so take it easy on me!

Thanks

@ jaywrs - If you’ve got the piezo making noise, that’s a great start, and confirms that you have it wired correctly.

Skip the timer initially, and just handle the button pressed and button released events. Once you’ve connected your button, and dropped it on the design surface and wired it to the correct socket, you should just be able to type (inside the ProgramStarted method):

button.buttonPressed

then type += and hit the tab key twice, which should automatically add the event handler for you. Then you can replace the default code:

throw new NotImplementedException();

with the code that turns the piezo on:

Piezo1.Set (440, 50);

and do the same for the button.buttonReleased event, using this code:

Piezo1.Set (440, 0);

That should turn the piezo on when you press the button, and turn it off when you release the button.

Once you’ve verified that that works, you can try adding the timer. First, add the variable declaration to the top of the class definition (between the class declaration and the beginning of the ProgramStarted method):

static GT.Timer timer;

Then, in ProgramStarted, initialize the timer:

timer = new GT.Timer(500);
timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);

The above timer will fire the Tick event every 500ms, you may want to change that. If you use the same trick of typing timer.Tick += and then tap the Tab key twice, Visual Studio will automatically create the event handler for you.

Next, change the code inside the button_ButtonPressed event handler to the following:

            if (!timer.IsRunning)
            {
                timer.Start();
            }
            else
            {
                timer.Stop();
            }

            button.ToggleLED();

(note that I typically toggle the button LED if I’m using the button to trigger a timer, so that I can tell visually whether the timer is running or not)

Finally, add the desired code to your timer. You might for example, add a boolean variable to your class called _isPlaying, and based on its value, turn on or off the piezo (don’t forget to toggle the value of the variable each time). I leave that last part as an exercise for the new Gadgeteer.

Good luck, and have fun!