@ 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!