//UART static void receivedDataUsingCOMPort(object sender, SerialDataReceivedEventArgs e) { SecretLabs.NETMF.Hardware.PWM speaker = new SecretLabs.NETMF.Hardware.PWM(Pins.GPIO_PIN_D5); try { if (spUART.BytesToRead > 0) { byte[] buffer = new byte[spUART.BytesToRead]; spUART.Read(buffer, 0, buffer.Length); char[] cReceived = System.Text.Encoding.UTF8.GetChars(buffer, 0, buffer.Length); string strReceived = new string(cReceived); Debug.Print("command string received " + strReceived); if (cReceived[0] == 'N') { led.Write(true); } else if (cReceived[0] == 'F') { led.Write(false); } //MOTION SENSOR - PIEZO BUZZER TURN ON else if (cReceived[0] == 'A') { Debug.Print("Alarm sounding"); for (int i = 0; i < 5; i++) { uint duration1 = 1000, duration2 = 1000; speaker.SetPulse(duration1 * 2, duration1); Thread.Sleep(100); speaker.SetPulse(duration2 * 2, duration2); Thread.Sleep(100); speaker.SetPulse(duration1 * 2, duration1); Thread.Sleep(100); speaker.SetPulse(duration2 * 2, duration2); Thread.Sleep(100); speaker.SetDutyCycle(0); } } //PIEZO BUZZER TURN OFF //else if (cReceived[0] == 'B') // { // Debug.Print("Turned off alarm"); // speaker.SetDutyCycle(0); // } } } catch (Exception ex) { Debug.Print("exception " + ex.Message.ToString()); } }
public static void Main() { int tempInput = 0; while (true) { tempInput = tempSensor.Read(); float volts = ((float)tempInput / 1024.0f) * 3.3f; float temp_celcius = (volts * 100); float temp_fahrenheit = (temp_celcius * 9) / 5 + 32; if (temp_celcius > 24.0f) { speaker.SetPulse((uint)(1000000 / 2217.46f), (uint)(1000000 / 2217.46f) / 2); } else { speaker.SetDutyCycle(0); } Debug.Print("Celcius: " + temp_celcius.ToString() + " - " + "Fahrenheit: " + temp_fahrenheit.ToString()); Thread.Sleep(1000); } }
public DcMotor(MotorHeaders header) { switch (header) { case MotorHeaders.M3: _motorBitA = (int)MotorBits.Motor3A; _motorBitB = (int)MotorBits.Motor3B; _pwm = new PWM(MotorShield.Pwm_0B); break; case MotorHeaders.M4: _motorBitA = (int)MotorBits.Motor4A; _motorBitB = (int)MotorBits.Motor4B; _pwm = new PWM(MotorShield.Pwm_0A); break; default: throw new InvalidOperationException("Invalid motor header specified."); } MotorShield.Instance.LatchState &= (byte)(~(1 << _motorBitA) & ~(1 << _motorBitB)); MotorShield.Instance.LatchTx(); _pwm.SetPulse(100, 0); }
//===========================MOTION SENSOR========================= static void motion_OnInterrupt(uint data1, uint data2, DateTime time) { try { //port.EnableInterrupt(); port.DisableInterrupt(); DateTime startTime = DateTime.Now; // Some execution process DateTime endTime = DateTime.Now; TimeSpan totalTimeTaken = endTime.Subtract(startTime); Debug.Print("Motion detected"); speaker.SetDutyCycle(50); uint duration1 = 1000, duration2 = 800; Debug.Print("Alarm sounding"); for (int i = 0; i < 2; i++) { speaker.SetPulse(duration1 * 2, duration1); Thread.Sleep(200); speaker.SetPulse(duration2 * 2, duration2); Thread.Sleep(200); speaker.SetDutyCycle(0); } sendDataUsingCOMPort("MOTION=" + "MOTION DETECTED FOR " + totalTimeTaken + "s "); //spUART.Flush(); //Thread.Sleep(5000); //uint duration1 = 1000, duration2 = 1000; //speaker.SetPulse(duration1 * 2, duration1); //speaker.SetPulse(duration2 * 2, duration2); //Thread.Sleep(250); //speaker.SetDutyCycle(0); MovementDetected = true; //OutputPort redLED = new OutputPort(Pins.GPIO_PIN_D9, false); //for (int i = 0; i < 3; i++) // { // redLED.Write(true); // Thread.Sleep(300); // redLED.Write(false); //} Thread.Sleep(5000); port.EnableInterrupt(); } catch (Exception ex) { Debug.Print(ex.Message); } }
private static void SetServoPosition(int position) { position = (position % 180) + 10; servo.SetPulse(20000, 500 + (uint)(position * 11.11)); }