Main Site Documentation

Randomly choosen LED blinking


#1

Hi,

I couple of weeks ago I bought my first Fez Panda II. In the past I only worked with an Ardruino at school.
But for my new project, I needed more output pins so I got my Panda.

But I’m struggling with the coding now…

I have 5 LEDs and 6 Buttons. Every LED belongs to 1 button. The button that is over, is the bonus button.
If you push the Bonusbutton the 5LEDs start blinking. 4 LEDs stop blinking after 2 seconds and 1LED stays blinking.

If you push the button that belongs to that blinking LED, the score will +10.

I managed too get the LEDs blinking, but I can’t figure out how one LED will randomly be choosen and stays blinking…
Can someone help me with that please?

(sorry, I’m not sure if this is in the right forum)


#2

You are on the right forum but we need to see some code

Welcome to the community.


#3
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;

namespace FEZ_Panda_II_Simple_Input_Output
{
    public class Program
    {


        public static void Main()
        {
            //output ports   
            //LEDs
            OutputPort Target1_LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di26, false);
            OutputPort Target2_LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di27, false);
            OutputPort Target3_LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di28, false);
            OutputPort Target4_LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di29, false);
            OutputPort Target5_LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di30, false);

            //input
            InputPort Target1_but = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di20, true, Port.ResistorMode.PullUp);
            InputPort Target2_but = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di21, true, Port.ResistorMode.PullUp);
            InputPort Target3_but = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di22, true, Port.ResistorMode.PullUp);
            InputPort Target4_but = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di23, true, Port.ResistorMode.PullUp);
            InputPort Target5_but = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di24, true, Port.ResistorMode.PullUp);
            InputPort TargetBonus_but = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di40, true, Port.ResistorMode.PullUp);


            //mode  
            int score = 0;

            //infinity loop  
            while (true)
            {

                //Bonus target 
                if (TargetBonus_but.Read() == false)
                {
                    Target1_LED.Write(true);   
                    Target2_LED.Write(true);   
                    Target3_LED.Write(true);      
                    Target4_LED.Write(true);    
                    Target5_LED.Write(true);    
                    Thread.Sleep(500);
                    Target1_LED.Write(false);   
                    Target2_LED.Write(false);   
                    Target3_LED.Write(false);      
                    Target4_LED.Write(false);    
                    Target5_LED.Write(false);    
                    Thread.Sleep(500);
                    Target1_LED.Write(true);   
                    Target2_LED.Write(true);   
                    Target3_LED.Write(true);      
                    Target4_LED.Write(true);    
                    Target5_LED.Write(true);    
                    Thread.Sleep(500);
                    Target1_LED.Write(false);   
                    Target2_LED.Write(false);   
                    Target3_LED.Write(false);      
                    Target4_LED.Write(false);    
                    Target5_LED.Write(false);    
                    Thread.Sleep(500);
                }

That is what I’ve got right so far. (Yeah, I’ve got more, but thats not necessary now.)

I think I need something like this:


                if (TargetXX_LED.Read() == true && TargetXX_but.Read() == false) //the XX needs to be a random number of the LEDs AND button so, LED1, 2, 3, 4 or 5 and the button with the same number..
             {
                score += 10;
                targetXX_LED.Write() == false;   // will set the LED off when the button is pressed.
              }

But I just don’t know how to get a random number (1 - 5) at the XX position…


#4

Code looks alright. So you need a random number?

it is something like
Random rn = new Random();

then every time you need a number, you would use

int number = rn.Next();


#5

I need a random number at the place of the XX at this part:
“TargetXX_LED.Read()”

How can I do that?

  • Edit: Oh, and the random number only can be 1, 2, 3, 4 OR 5…

#6

I don’t think you’re going to be able to have XX substituted into an object’s name exactly as you say.

What about an array of objects and use the random number as the index?

target_LED[0] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di26, false);
....
if (Target_LED[randomnumber].Read() == true && Target_but[randomnumber].Read() == false) 
             {
                score += 10;
                target_LED[randomnumber].Write() == false;   // will set the LED off when the button is pressed.
              }


#7

@ Brett - You mean target_LED[randomnumber].Write(false); :slight_smile:


#8

@ Gus, no I don’t I just copied the code from above :slight_smile: but yeah that’s a great point if you want to change the LED state you need to do what Gus says !


#9

Haha, it needs to be Read, because it check if the LED is on and if the button is pushed :slight_smile:

But thanks for the help, but it doesn’t work.
Or better said, I don’t know how too use it.

Do I need to change all my

 OutputPort Target1_LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di26, false);

to

target_LED[0] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di26, false);

But with an other number between the [ & ]'s and the pin (Di26)…

When I do that, it gives me the errors:
“The name ‘Target_LED’ does not exist in the current context”


#10

read up on arrays. You need to create an array of these objects first, and then assign them.

BTW this is what Gus was talking about.

target_LED[randomnumber].Write() == false

The write() needs to be write(true) or write(false) to change the value. You also should know that “==” is the equivalence check, not an assignment


#11

This might be obvious, but C# is case sensitive. You declare target_LED, but the error message is for Target_LED


#12

Haha oops, my fault!

I know something about arrays thanks too Flash (ActionScript3), but I’m not an hero with arrays…

So if I do


object[] target_LED = new object[5];
            target_LED[1] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di26, false);
            target_LED[2] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di27, false);
            target_LED[3] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di28, false);
            target_LED[4] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di29, false);
            target_LED[5] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di30, false);

and


         if (target_LED[1].Read() == true) 
             {
                score += 10;
              }

It gives me errors like:
“Error ‘object’ does not contain a definition for ‘Write’ and no extension method ‘Write’ accepting a first argument of type ‘object’ could be found (are you missing a using directive or an assembly reference?)”

(I just took target_LED[1] too test it before I add the randomnumber.)

And GMod(Errol) I saw that the names weren’t the same, I changed it in my code allready :slight_smile:


#13

Try create array of output ports not just objects or cast each element before you try to access OutputPort methods.

OutputPort target_LED = new OutputPort [5];

or

((OutputPort )target_LED[1]).Read()

#14

then it gives me the errors:
“Error Cannot implicitly convert type ‘Microsoft.SPOT.Hardware.OutputPort[]’ to ‘Microsoft.SPOT.Hardware.OutputPort’”
“Error Cannot apply indexing with [] to an expression of type ‘Microsoft.SPOT.Hardware.OutputPort’”


#15

Ooops

like this:

OutputPort[] target_LED = new OutputPort [5];

#16

@ PsychoBram - Arrays (and everything except count/length methods) generally begin indexing at 0, not 1.

Random random_gen = new Random();

OutputPort[] Target_LED = new OutputPort[5];
Target_LED[0] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di26, false);
Target_LED[1] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di27, false);
Target_LED[2] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di28, false);
Target_LED[3] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di29, false);
Target_LED[4] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di30, false);

//Randomly turn on one of the LED's above
int random_number = random_gen.Next(5); //Return a number 0-4
Target_LED[random_number].Write(true);

#17

Thank you all for the help.

I tested it for the last time with the code from Architect, but without a randomnumber, that was working.

But now when I added the random-part, it gives me this error:
“Device not found or cannot be opened - USB:USBizi_USBizi”

I’m looking for a way too solve that, it has nothing to do with the code.

@ James I changed the arrays, starting at 0. Thanks.


#18

@ PsychoBram - This is not caused by the code (unless you previously deployed that code), however is it possible you have a loop in your program that does not let the thread sleep? If you do, this is called a ‘tight loop’ and can cause communication errors. To resolve this issue, you can manually update the firmware by following the steps listed here: http://wiki.tinyclr.com/index.php?title=Firmware_Update_USBizi


#19

@ James It’s allready working again. I just copied the whole code and pasted it in a new project… :slight_smile:

But here is (hopefully) my last question. :slight_smile:
And after that, I hope it’s working like I wat it too.

If I use this


                if (target_LED[random_number].Read() == true && target_but[random_number].Read(false))
                {
                    score += 20;
                    target_LED[random_number].Write(false);   // will set the LED off when the button is pressed.
                    
                }

it gives me this error:
“Error The name ‘random_number’ does not exist in the current context”


#20

Obviously use need to make a random number. :wink:

Random rnd = new Random()
int random_number = rnd.Next(5);