如何使用Windows 10 IoT uwp c#运行LED

时间:2016-12-25 21:26:28

标签: c# uwp windowsiot

我在使用Raspberry PI 3 Windows IOT创建运行LED时遇到问题,但是随机LED闪烁确实有效。  两者的代码几乎相同,只有不同的运行LED由变量计数和随机LED控制  用随机生成器生成。

正在运行的led代码位于此文本下方。我将只得到第一个导致光,其余的LED保持黑暗。有人能看出这段代码有什么问题吗?  但随机闪烁导致工作,这在运行的LED代码和两个代码几乎相同之后,但仍然运行的LED代码不起作用。

运行LED代码

private void Timer_Tick(object sender, object e)
 {
 int ledCheck = 1, ledCheck2 = 1;
 int ledValue = 0, ledValue2 = 0; 
 //ledValue2 = 1;
 int checkValue = 0;


 if (ledCheck2 == 1)
 {
 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);
 ledCheck2 = 0;

 }
 ledValue2 = ledValue2 + 1;
 TextBlock1.Text = ledValue2.ToString();
 if (ledValue2 == 1)
 {
 pinValue1 = GpioPinValue.High;

 }

 if (ledValue2 == 2)
 {
 pinValue2 = GpioPinValue.High;

 }

 if (ledValue == 3)
 {
 pinValue3 = GpioPinValue.High;

 }

 if (ledValue2 == 4)
 {
 pinValue4 = GpioPinValue.High;

 }

 if (ledValue2 == 5)
 {
 pinValue5 = GpioPinValue.High;

 }

 if (ledValue2 == 6)
 {
 pinValue6 = GpioPinValue.High;

 }

 if (ledValue2 == 7)
 {
 pinValue7 = GpioPinValue.High;

 }
 if (pinValue1 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.High;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);

 }


 if (pinValue2 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.High;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);
 }

 if (pinValue3 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.High;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);

 }



 if (pinValue4 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.High;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);
 }



 if (pinValue5 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.High;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);

 }



 if (pinValue6 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.High;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);


 }


​
 if (pinValue7 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.High;
 pin7.Write(pinValue7);


 }
 TextBlock1.Text = "TEST " + ledValue2.ToString();
 break;
 }

随机指示灯闪烁

private void Timer_Tick(object sender, object e)
 {
 int ledCheck = 1, ledCheck2 = 1;
 int ledValue = 0, ledValue2 = 0; 
 //ledValue2 = 1;
 int checkValue = 0;

 if (ledCheck == 1)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);
 ledCheck = 0;

 }


 Random rnd = new Random();
 ledValue = rnd.Next(1, 8);

 if (ledValue != checkValue)
 {
 checkValue = ledValue;
 if (ledValue == 1)
 {
 pinValue1 = GpioPinValue.High;

 }

 if (ledValue == 2)
 {
 pinValue1 = GpioPinValue.High;

 }

 if (ledValue == 3)
 {
 pinValue3 = GpioPinValue.High;

 }

 if (ledValue == 4)
 {
 pinValue4 = GpioPinValue.High;

 }

 if (ledValue == 5)
 {
 pinValue5 = GpioPinValue.High;

 }

 if (ledValue == 6)
 {
 pinValue6 = GpioPinValue.High;

 }

 if (ledValue == 7)
 {
 pinValue7 = GpioPinValue.High;

 }

 if (pinValue1 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.High;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);

 }


 if (pinValue2 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.High;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);
 }

 if (pinValue3 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.High;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);

 }



 if (pinValue4 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.High;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);
 }



 if (pinValue5 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.High;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);

 }



 if (pinValue6 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.High;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.Low;
 pin7.Write(pinValue7);


 }



 if (pinValue7 == GpioPinValue.High)
 {

 pinValue1 = GpioPinValue.Low;
 pin.Write(pinValue1);

 pinValue2 = GpioPinValue.Low;
 pin2.Write(pinValue2);

 pinValue3 = GpioPinValue.Low;
 pin3.Write(pinValue3);

 pinValue4 = GpioPinValue.Low;
 pin4.Write(pinValue4);

 pinValue5 = GpioPinValue.Low;
 pin5.Write(pinValue5);

 pinValue6 = GpioPinValue.Low;
 pin6.Write(pinValue6);

 pinValue7 = GpioPinValue.High;
 pin7.Write(pinValue7);


 }
 TextBlock1.Text = "NOT Same value!";

 }
 else
 {
 TextBlock1.Text = "Same value!";
 }
 } 

1 个答案:

答案 0 :(得分:1)

  

我将只得到第一个导致光线,其余的LED保持黑暗。   有人能看出这段代码有什么问题吗?

因为每次输入Timer_Tick()ledValue2变量都会在此行初始化为0

 int ledValue = 0, ledValue2 = 0;

您需要从ledValue2移出Timer_Tick()变量。例如,您可以将它放在MainPage类的开头。并在此代码行ledValue2 = ledValue2 + 1之后添加以下代码。

        if (ledValue2 > 7)
        {
            ledValue2 = 0;
        }