Пример #1
0
        public static void TestAnalogCallback(ArduinoBoard board)
        {
            int analogPin        = GetAnalogPin1(board);
            var analogController = board.CreateAnalogController(0);

            board.SetAnalogPinSamplingInterval(TimeSpan.FromMilliseconds(10));
            var pin = analogController.OpenPin(analogPin);

            pin.EnableAnalogValueChangedEvent(null, 0);

            pin.ValueChanged += (sender, args) =>
            {
                if (args.PinNumber == analogPin)
                {
                    Console.WriteLine($"New voltage: {args.Value}.");
                }
            };

            Console.WriteLine("Waiting for changes on the analog input");
            while (!Console.KeyAvailable)
            {
                // Nothing to do
                Thread.Sleep(100);
            }

            Console.ReadKey();
            pin.DisableAnalogValueChangedEvent();
            pin.Dispose();
            analogController.Dispose();
        }
Пример #2
0
        public static void TestAnalogIn(ArduinoBoard board)
        {
            // Use Pin 6
            const int gpio             = 6;
            int       analogPin        = GetAnalogPin1(board);
            var       gpioController   = board.CreateGpioController();
            var       analogController = board.CreateAnalogController(0);

            var pin = analogController.OpenPin(analogPin);

            gpioController.OpenPin(gpio);
            gpioController.SetPinMode(gpio, PinMode.Output);

            Console.WriteLine("Blinking GPIO6, based on analog input.");
            while (!Console.KeyAvailable)
            {
                ElectricPotential voltage = pin.ReadVoltage();
                gpioController.Write(gpio, PinValue.High);
                Thread.Sleep((int)(voltage * 100).Volts);
                voltage = pin.ReadVoltage();
                gpioController.Write(gpio, PinValue.Low);
                Thread.Sleep((int)(voltage * 100).Volts);
            }

            pin.Dispose();
            Console.ReadKey();
            analogController.Dispose();
            gpioController.Dispose();
        }