/// <summary> /// Initializes a new instance of the <see cref="ProximitySensor"/> class /// </summary> /// <param name="proximitySensorType"> /// </param> /// <param name="analogInput"> /// The analog input to which the proximity sensor is attached /// </param> /// <exception cref="ArgumentNullException"> /// Thrown when the given <see cref="AnalogInput"/> is null /// </exception> /// <remarks> /// This sensor is only provides valid results for objects farther than 3" away or objects closer than /// <see cref="MaximumReadableDistance"/> from the sensor. Passing a trigger which attempts to obtain /// distances outside of this range will result in a large number of false positives. /// </remarks> public ProximitySensor(ProximitySensorType proximitySensorType, AnalogInput analogInput) { if (analogInput == null) { throw new ArgumentNullException("analogInput"); } this.proximitySensorType = proximitySensorType; MaximumReadableDistance = GetMaximumReadableDistance(analogInput); new Thread(() => { while (true) { if (IsEnabled && ObjectDetectionTrigger != null) { var distance = new Distance(analogInput.ReadRaw(), proximitySensorType); if (ObjectDetectionTrigger(MaximumReadableDistance, distance)) { if (ObjectDetected != null) { ObjectDetected(this, new ObjectDetectedEventArgs(distance, DateTime.UtcNow)); } } } Thread.Sleep(10); } }).Start(); }
public Int32 Read() { if (_disposed) { throw new System.ObjectDisposedException(); } // retrieve our 10-bit ADC reading (based on a range of 0 to AREF) // convert our reading to the desired range and return it to the user. return((Int32)((((Int64)_analogInput.ReadRaw() * (_maxValue - _minValue)) / TEN_BIT_ADC_MAX_VALUE) + _minValue)); }
public void Run() { AnalogInput probe = new AnalogInput(Cpu.AnalogChannel.ANALOG_4); Boolean plateState = false; OutputPort HotPlate = new OutputPort(Pins.GPIO_PIN_D0, plateState); OutputPort LED = new OutputPort(Pins.ONBOARD_LED, plateState); while (true) { plateState = probe.Read() <= .95; HotPlate.Write(plateState); LED.Write(plateState); Debug.Print(probe.ReadRaw().ToString() + "\t" + probe.Read().ToString() + "\t" + plateState); System.Threading.Thread.Sleep(1000); } }
public static void Main() { // write your code here BlueSerial ser = new BlueSerial(); AnalogInput input = new AnalogInput(AnalogChannels.ANALOG_PIN_A0); OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); UInt16 j = 0; int rawValue = 0; UInt16 ll = 0; while (true) { // 0 e 1023 (ADC a 10 bit) rawValue = input.ReadRaw(); Debug.Print(rawValue.ToString()); ll = (UInt16)rawValue; // ritorna un valore tra 0 ed 1 che va moltiplicato per // la ARef per ottenere il valore in Volt della tensione //double volt = input.Read() * 3.3; if (ll > 4000) { led.Write(true); } else { led.Write(false); } ser.Print(ll); Thread.Sleep(5); } }
/// <summary> /// Determines the maximum distance the proximity sensor can detect an object /// </summary> /// <param name="analogInput"> /// The analog input to which the proximity sensor is attached /// </param> /// <returns> /// The maximum distance the proximity sensor can detect an object /// </returns> private Distance GetMaximumReadableDistance(AnalogInput analogInput) { var calibrationStartTime = DateTime.Now; var peakDigitalVoltage = 0; while (DateTime.Now <= calibrationStartTime.AddSeconds(3)) { var digitalVoltage = analogInput.ReadRaw(); peakDigitalVoltage = digitalVoltage > peakDigitalVoltage ? digitalVoltage : peakDigitalVoltage; } return new Distance(peakDigitalVoltage, proximitySensorType); }
public static void Main() { // write your code here BlueSerial ser = new BlueSerial(); AnalogInput input1 = new AnalogInput(AnalogChannels.ANALOG_PIN_A0); // 2 kN loadcell AnalogInput input2 = new AnalogInput(AnalogChannels.ANALOG_PIN_A1); // 200 kN loadcell OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); UInt16 j = 0; int rawValue1 = 0; int rawValue2 = 0; float rawAve1kN = 0; float rawAve2kN = 0; int rawAve1 = 0; int rawAve2 = 0; int i = 0; UInt16 uintIn1 = 0; UInt16 uintIn2 = 0; while (true) { rawValue1 = 0; rawValue2 = 0; rawAve1 = 0; rawAve2 = 0; for (i = 1; i <= 50; i++) // Take an average of 50 samples { rawValue1 = input1.ReadRaw(); rawAve1 = rawAve1 + rawValue1; rawValue2 = input2.ReadRaw(); rawAve2 = rawAve2 + rawValue2; Thread.Sleep(2); } rawAve1 = rawAve1 / (i-1); rawAve2 = rawAve2 / (i-1); rawAve1kN = (float)((rawAve1 - 2071) / -943.20518975); // 2 kN loadcell calibration under "loadcellcalibration.xlsx" rawAve2kN = (float)((rawAve2 - 2064) / -13.75116883); // 200 kN loadcell calibration under "loadcellcalibration.xlsx" // Print some values to the output window. This is primative, but works for now. Debug.Print("Value 1 Ave: " + rawAve1.ToString() + " Value 1 Raw: " + rawValue1.ToString() + " 2kN Load Cell Value 1 kN: " + rawAve1kN.ToString() + " Value 2 Ave: " + rawAve2.ToString() + " Value 2 Raw: " + rawValue2.ToString() + " 200kN Load Cell Value 2 kN: " + rawAve2kN.ToString()); uintIn1 = (UInt16)rawAve1; uintIn2 = (UInt16)rawAve2; // Send the data via bluetooth string junk = (uintIn1.ToString() + " " + uintIn2.ToString() + "\n"); byte[] data = new byte[4] { (byte)(uintIn1 & 0xFF), (byte)((uintIn1 >> 8) & 0xFF), (byte)(uintIn2 & 0xFF), (byte)((uintIn2 >> 8) & 0xFF)}; // Small loadcell first, then large loadcell // ser.Print(strain1); // Thread.Sleep(5); ser.Print(junk); //ser.Print(data); Thread.Sleep(5); } }
public static int getv(int socket, int pin) { AnalogInput ai = new AnalogInput((Cpu.AnalogChannel)pin); return ai.ReadRaw(); }
public static void Main() { LCD.ByteMap = Bitmap; LCD.Refresh(); //LCD.DrawString("Start initial."); //LCD.Refresh(); //start the LCD sheild //Nokia_5110 LCD = new Nokia_5110(false, Pins.GPIO_PIN_D5, Pins.GPIO_PIN_D7, Pins.GPIO_PIN_D6, Pins.GPIO_PIN_D4); LCD.BacklightBrightness = 100; //set ip static NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0]; networkInterface.EnableStaticIP("192.168.1.199", "255.255.255.0", "192.168.1.1"); //the server: //Listener webServer = new Listener(RequestReceived); AnalogInput keys_port = new AnalogInput(Cpu.AnalogChannel.ANALOG_0); AnalogInput test_port = new AnalogInput(Cpu.AnalogChannel.ANALOG_2); OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); //LCD.DrawString("Done init."); //LCD.Refresh(); while (true) { // Blink LED to show we're still responsive led.Write(!led.Read()); Thread.Sleep(1000); LCD.Clear(); LCD.DrawString(0, 0, "========"); LCD.DrawString(0, 1, "AD Port 0:"); LCD.DrawString(0, 2, keys_port.ReadRaw().ToString()); LCD.DrawString(0, 3, "AD Port 2:"); LCD.DrawString(0, 4, test_port.ReadRaw().ToString()); LCD.DrawString(0, 5, "========"); LCD.Refresh(); } }