我每次霍尔传感器检测到磁铁时都会尝试增加一个变量但是它不起作用。我是野外windows iot c#with raspi 3的新手。
所以我的代码看起来像:
public sealed partial class MainPage : Page
{
private int count = 5;
private const int SENSOR_PIN = 5; //SENSOR PIN
private GpioPin sensorPin;
public MainPage()
{
this.InitializeComponent();
InitGPIO();
}
private void InitGPIO()
{
var gpio = GpioController.GetDefault();
if (gpio == null)
{
GpioStatus.Text = "No Gpio Pins!";
return;
}
sensorPin = gpio.OpenPin(SENSOR_PIN);
sensorPin.SetDriveMode(GpioPinDriveMode.Input);
sensorPin.ValueChanged += sensorPin_ValueChanged;
GpioStatus.Text = "GPIO pins initialized correctly.";
}
//INTERRUPT HANDLER
private void sensorPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
{
// Increment
if (e.Edge == GpioPinEdge.FallingEdge)
{
count++;
}
}
}
}
这是一个简单的测试来缩小问题范围。
将GPIO5和GPIO6连接在一起如下:
在这里,我使用GPIO6模拟霍尔传感器。每次单击该按钮,GPIO6输出值都会改变。以下是您可以测试的代码示例,以查看是否可以触发sensorPin_ValueChanged
处理程序。
public sealed partial class MainPage : Page
{
private int count = 5;
private const int SENSOR_PIN = 5; //SENSOR PIN
private GpioPin sensorPin;
private GpioPin OutputPin;
private const int OPinNum = 6;
public MainPage()
{
this.InitializeComponent();
InitGPIO();
}
private void InitGPIO()
{
var gpio = GpioController.GetDefault();
if (gpio == null)
{
GpioStatus.Text = "No Gpio Pins!";
return;
}
sensorPin = gpio.OpenPin(SENSOR_PIN);
sensorPin.SetDriveMode(GpioPinDriveMode.Input);
sensorPin.ValueChanged += sensorPin_ValueChanged;
GpioStatus.Text = "GPIO pins initialized correctly.";
OutputPin = gpio.OpenPin(OPinNum);
OutputPin.SetDriveMode(GpioPinDriveMode.Output);
}
//INTERRUPT HANDLER
private void sensorPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
{
// Increment
if (e.Edge == GpioPinEdge.FallingEdge)
{
count++;
}
}
// Simulate Hall Sensor
private void Button_Click(object sender, RoutedEventArgs e)
{
if(OutputPin.Read() == GpioPinValue.Low)
OutputPin.Write( GpioPinValue.High);
else
OutputPin.Write(GpioPinValue.Low);
}
}
XAML代码:
<StackPanel VerticalAlignment="Center" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Name="GpioStatus" />
<Button Content="Change output value" Click="Button_Click"/>
</StackPanel>