How to make running leds with Windows 10 IoT uwp c#

122 views Asked by At

I have problem to create running leds with Raspberry PI 3 Windows IOT, but random led blink does work. The code for both are almost the same, only diffrent runnings leds is controlled by variable count and random led is generated with random generator.

The running led code is below this text. I will get only the first led to light, and rest of leds stays dark. Can someone see what's wrong with this code? But the random blink led works, which comes after this running leds code and both codes almost identically, but still the running leds code does not work.

Running leds code

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;
 }

Random led blink

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

There are 1 answers

0
Rita Han On

I will get only the first led to light, and rest of leds stays dark. Can someone see what's wrong with this code?

Because every time enter Timer_Tick() the ledValue2 variable is initialized to be 0 at this line:

 int ledValue = 0, ledValue2 = 0;

You need move ledValue2 variable out of Timer_Tick(). For example, you can put it at the start of MainPage class. And add the following piece of code after this code line ledValue2 = ledValue2 + 1.

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