private void Start() { servo = new RCServo(); servo.Open(); servo.Attach += OnAttachServo; sensorDistance = new DistanceSensor(); sensorDistance.Open(); sensorDistance.Attach += OnAttachDistanceSensor; }
static void Main(string[] args) { var greenButton = new DigitalInput(); greenButton.IsHubPortDevice = true; greenButton.HubPort = 1; var greenLed = new DigitalOutput(); greenLed.IsHubPortDevice = true; greenLed.HubPort = 3; var redButton = new DigitalInput(); redButton.IsHubPortDevice = true; redButton.HubPort = 0; var redLed = new DigitalOutput(); redLed.IsHubPortDevice = true; redLed.HubPort = 2; var sonar = new DistanceSensor(); sonar.HubPort = 4; sonar.Open(1000); redButton.Open(1000); redLed.Open(1000); greenButton.Open(1000); greenLed.Open(1000); greenButton.StateChange += (o, e) => { greenLed.State = e.State; }; redButton.StateChange += (o, e) => { redLed.State = e.State; if (e.State) { var sound = new SoundPlayer(@"C:\windows\media\tada.wav"); sound.Play(); var random = new Random(); Console.WriteLine("The next random number is {0}!", random.Next(0, 10)); } }; sonar.DistanceChange += (o, e) => { if (e.Distance > 100) { greenLed.State = true; redLed.State = false; } else { greenLed.State = false; redLed.State = true; } }; Console.WriteLine("Press enter to exit."); Console.ReadLine(); redButton.Close(); redLed.Close(); greenButton.Close(); greenLed.Close(); }
static void Main(string[] args) { Log.Enable(LogLevel.Info, "phidgetlog.log"); DistanceSensor distanceSensor0 = new DistanceSensor(); distanceSensor0.HubPort = 3; //distanceSensor0.DistanceChange += DistanceSensor0_DistanceChange; distanceSensor0.Attach += DistanceSensor0_Attach; distanceSensor0.Detach += DistanceSensor0_Detach; distanceSensor0.Error += DistanceSensor0_Error; DistanceSensor distanceSensor1 = new DistanceSensor(); distanceSensor1.HubPort = 4; //distanceSensor0.DistanceChange += DistanceSensor0_DistanceChange; distanceSensor1.Attach += DistanceSensor0_Attach; distanceSensor1.Detach += DistanceSensor0_Detach; distanceSensor1.Error += DistanceSensor0_Error; //VoltageRatioSensorType voltageRatioSensor = new VoltageRatioSensorType(); VoltageRatioInput voltageRatioInput = new VoltageRatioInput(); voltageRatioInput.HubPort = 0; voltageRatioInput.IsHubPortDevice = true; voltageRatioInput.Channel = 0; voltageRatioInput.VoltageRatioChange += VoltageRatioInput_VoltageRatioChange; try { InitizlizeMqttClient(); distanceSensor0.Open(5000); distanceSensor0.DataInterval = 2000; distanceSensor0.SonarQuietMode = false; distanceSensor1.Open(5000); distanceSensor1.DataInterval = 2000; distanceSensor1.SonarQuietMode = false; voltageRatioInput.Open(5000); //voltageRatioInput.SensorType = VoltageRatioSensorType.PN_1129; voltageRatioInput.DataInterval = 100; //Console.ReadLine(); Task.Run(() => { while (true) { Thread.Sleep(2000); checkHandWash(distanceSensor1); checkToiletVisit(distanceSensor0); } }); //Wait until Enter has been pressed before exiting Console.ReadLine(); distanceSensor0.Close(); } catch (PhidgetException ex) { Console.WriteLine(ex.ToString()); Console.WriteLine(""); Console.WriteLine("PhidgetException " + ex.ErrorCode + " (" + ex.Description + "): " + ex.Detail); Console.ReadLine(); } }