using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace KidBit { public class Program { //IO pins static InputPort fakeConnection = new InputPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.PullUp); static OutputPort alarmLed = new OutputPort(Pins.GPIO_PIN_D6, false); static OutputPort heartBeatLed = new OutputPort(Pins.ONBOARD_LED, false); static PWM piezo = new PWM(Pins.GPIO_PIN_D5); //variables static int i = 0; //counter static int critical = 4; //number of fails to trigger alarm static bool fc = false; static uint tone1 = 1000000 / 212; //to adjust tone, only modify denominator static uint tone2 = 1000000 / 300; //to adjust tone, only modify denominator public static void Main() { /* working on interrupt for LED and Piezo InterruptPort failure = new InterruptPort(fc, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth); failure.OnInterrupt += failure_OnInterrupt; */ piezo.SetPulse(0, 0); //Silence piezo at startup alarmLed.Write(false); //turn off Alarm LED at startup heartBeatLed.Write(false); //initialize Heart Beat LED while (true) //While loop for entire program { heartBeatLed.Write(!heartBeatLed.Read()); //Heart Beat LED confirms program is running if (fakeConnection.Read() == true) //true = within range, false = out of range { i = 0; //remotely written overriding count below } i++; //Increase counter by 1 if (i > critical) //if connection is broke, counter will go above 1, when counter hits value in critical, set alarm { while (i > critical) //set alarm LED { alarmLed.Write(!alarmLed.Read()); //flash LED every loop (toggle LED state) piezo.SetPulse(tone1, tone1 / 2); //set Piezo tone1 tone 1 Thread.Sleep(100); //sleep for 100 ms piezo.SetPulse(tone2, tone2 / 2); //change piezo to tone 2 if (fakeConnection.Read() == true) //Check to see if we're still in an alarm state { i = 0; //if out of alarm state, reset i } else { Thread.Sleep(100); //Still in alarm state, thread sleep for another 100 ms for a total of 200 ms } } piezo.SetPulse(0, 0); //out of alarm state, shut off piezo alarmLed.Write(false); //out of alarm state, turn off LED } Thread.Sleep(200); //main loop sleep for 200 ms } } } }