Netduino (plus) 2 project
I'm a beginner, just started coding and was playing with loops, button, LED and piezo element. I created this to understand the code and provide this for beginners like me who have some kind of security system in mind.
It uses the on board LED and button with a piezo element connected on pin 5 (red = +) and GRND (-).
It requires for you to add the PWM.dll to your project reference (right-click on your folder called 'References' and choose 'Add Reference...'). It can be used in combination with a 10K resistor to reduce the volume (I did not use a resistor).
When pressing the button a low frequency sound followed by a higher frequency sound will be heard. The later is looped (counted) and creates the (activation) delay. After the delay (10 loops) the on board LED will go on, indicating the system is activated. You could write additional code there where the LED goes on (for example an output port that enables a relay allowing power to an external siren when a sensor detects an intruder.
You could use an RFID card reader, code- or key lock instead of the on board button.
Its a simple code, just copy and paste and make sure to add the pwm.dll reference to your project.
using System;using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;namespace NetduinoApplication1{ public class Program{ static SecretLabs.NETMF.Hardware.PWM speaker = new SecretLabs.NETMF.Hardware.PWM(Pins.GPIO_PIN_D5); // Define the speaker static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); // Define the LED static InputPort button1 = new InputPort(Pins.ONBOARD_BTN, false, Port.ResistorMode.Disabled); // Define the button public static void Main() { while (true) { if (button1.Read()) //Check if the button is pressed and do the following when pressed { //Period is 1/frequency //1/1,000,000 second : 1000000 --> 1 sec // Duration for Sound over PWM (Period / 2) uint C = 1000000 / 600; //600 Hz tone uint D = 1000000 / 660; //660 Hz tone speaker.SetPulse(C, C / 2); //Play Thread.Sleep(800); // Duration speaker.SetPulse(0, 0); // Turn Off Thread.Sleep(75); //Pause int MyVar; // Create variable "MyVar" MyVar = 0; // MyVar is 0 while (MyVar < 10) // While MyVar is less than 10, do the following loop { // start the counting loop for MyVar MyVar++; // Increase the value from MyVar by + 1 speaker.SetPulse(D, D / 2); //Play led.Write(true); //LED on Thread.Sleep(1500); // Duration speaker.SetPulse(0, 0); // Turn Off led.Write(false); // LED off Thread.Sleep(2000); //Pause } // Close the counting loop from MyVar // Write your code here // This code will run directly after the MyVar loop (for example to enable your security system) led.Write(true); //LED on } // Close the button loop // Write your code here // Here you can write other code that has to run even if the button not has been pressed // For example a listener for a button to deactivate your security system } // Close the while loop } // Close the Main loop}}