Randomly choosen LED blinking

I you read everything before, I already managed to get an random number, but the only thing now, is to get it working in this code


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

According to the error you posted your issue is populating the variable “random_number”.

Yeah, but when I put

                    int random_number = random_gen.Next(5);  

at the top, it only gets a random number the first time I click the button.

So, I put the code here:


                if (targetBonus_but.Read() == false)
                {

                   random_number = random_gen.Next(5); 

                    target_LED[0].Write(true);      
                    target_LED[1].Write(true);      
                    target_LED[2].Write(true);     
                    target_LED[3].Write(true);      
                    target_LED[4].Write(true);     
                    Thread.Sleep(500);
                    target_LED[0].Write(false);    
                    target_LED[1].Write(false);     
                    target_LED[2].Write(false);     
                    target_LED[3].Write(false);      
                    target_LED[4].Write(false);     
                    Thread.Sleep(500);
                    target_LED[0].Write(true);     
                    target_LED[1].Write(true);  
                    target_LED[2].Write(true);   
                    target_LED[3].Write(true);      
                    target_LED[4].Write(true);     
                    Thread.Sleep(500);
                    target_LED[0].Write(false);      
                    target_LED[1].Write(false);     
                    target_LED[2].Write(false);    
                    target_LED[3].Write(false);      
                    target_LED[4].Write(false);      
                    Thread.Sleep(500);
 
                    target_LED[random_number].Write(true); 
                }

After the blinking a random LED is on.

but the random_number is also needed here



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

How can I do that?

You want it to be the same random number? Declare it outside your methods.

int random_number;
Random random_gen = new Random();

void method1()
{
                   random_number = random_gen.Next(5); 
if (targetBonus_but.Read() == false)
                {
                    target_LED[0].Write(true);      
                    target_LED[1].Write(true);      
                    target_LED[2].Write(true);     
                    target_LED[3].Write(true);      
                    target_LED[4].Write(true);     
                    Thread.Sleep(500);
                    target_LED[0].Write(false);    
                    target_LED[1].Write(false);     
                    target_LED[2].Write(false);     
                    target_LED[3].Write(false);      
                    target_LED[4].Write(false);     
                    Thread.Sleep(500);
                    target_LED[0].Write(true);     
                    target_LED[1].Write(true);  
                    target_LED[2].Write(true);   
                    target_LED[3].Write(true);      
                    target_LED[4].Write(true);     
                    Thread.Sleep(500);
                    target_LED[0].Write(false);      
                    target_LED[1].Write(false);     
                    target_LED[2].Write(false);    
                    target_LED[3].Write(false);      
                    target_LED[4].Write(false);      
                    Thread.Sleep(500);
 
                    target_LED[random_number].Write(true); 
                }
}

void method2()
{
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.
                }
}

@ Skewworks

I have tried this at a lot different ways, but I really don’t know how too put it in my code.

At the top of the code is this

using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;

after that


namespace flipperkast_v1
{
    public class Program
    {
        public static void Main()
        {

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

            InputPort[] target_but = new InputPort[5];
            target_but[0] = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di11, true, Port.ResistorMode.PullUp);
            target_but[1] = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di12, true, Port.ResistorMode.PullUp);
            target_but[2] = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di7, true, Port.ResistorMode.PullUp);

            OutputPort SG1 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di20, false);
            OutputPort SG2 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di21, false);
            OutputPort SG3 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di22, false);
            OutputPort SG4 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di23, false);
            OutputPort SG5 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di24, false);
            OutputPort SR1 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di48, false);
            OutputPort SR2 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di49, false);
            OutputPort SR3 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di50, false);
            OutputPort SR4 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di51, false);
            OutputPort SR5 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di52, false);

            InputPort targetBonus_but = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di40, true, Port.ResistorMode.PullUp);
            InputPort newgame = new InputPort((Cpu.Pin)FEZ_Pin.Digital.LDR, true, Port.ResistorMode.PullUp); 


            OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, true);

            int mode = 0;
            int random_number = random_gen.Next(3); //Return a number 0-2 (LEDs, arrays, beginnen bij 0)

while (true)
{
            void method1()
            {
                   random_number = random_gen.Next(5); 
                    if (targetBonus_but.Read() == false)
            {
                    target_LED[0].Write(true);      
                    target_LED[1].Write(true);      
                    target_LED[2].Write(true);     
                    target_LED[3].Write(true);      
                    target_LED[4].Write(true);     
                    Thread.Sleep(500);
                    target_LED[0].Write(false);    
                    target_LED[1].Write(false);     
                    target_LED[2].Write(false);     
                    target_LED[3].Write(false);      
                    target_LED[4].Write(false);     
                    Thread.Sleep(500);
                    target_LED[0].Write(true);     
                    target_LED[1].Write(true);  
                    target_LED[2].Write(true);   
                    target_LED[3].Write(true);      
                    target_LED[4].Write(true);     
                    Thread.Sleep(500);
                    target_LED[0].Write(false);      
                    target_LED[1].Write(false);     
                    target_LED[2].Write(false);    
                    target_LED[3].Write(false);      
                    target_LED[4].Write(false);      
                    Thread.Sleep(500);
 
                    target_LED[random_number].Write(true); 
                }
}
 
void method2()
{
            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.
                 }
}
}
        }

    }
}

The rest of the code isn’t necessary for this.
If I do it like this, it gives me this errors
"The name ‘random_number’ does not exist in the current context"
“The name ‘score’ does not exist in the current context”
“The name ‘target_but’ does not exist in the current context”
“The name 'target_LED” does not exist in the current context"
“The name 'targetBonus_but” does not exist in the current context"

So every button/led/int is at the wrong place, where do I need to put them?
Please help me out again


The error tells you what the problem is. All your variables are defined outside the scope of where you’re trying to use them and can’t be accessed.

I think what you need to do is go through the examples on the Wiki and the getting started pages to get used to the C# syntax and the like. If your code shown here is complete, you currently define your method1() and method2() code inside a WHILE loop. Finish the while loop properly as well as finishing the main() block, then define the methods outside main(). Then you need to figure out how to make the variables you have created in main() accessible outside main() and in your other methods - which can be as simple as moving the definition outside main() thus making them global.

I really think it’s worth taking a step back and going through the examples so you can get more of the foundation concepts understood.

Then you’d also pick up that target_LED[3] and target_LED[4] are never initialised as well :wink:

I didn’t had the time to take a look at it last months.
But I’m trying now for several days but I’m stuck.

Can somebody please help me with this.

I’m building a small pinball machine (to go), you can take a look here: http://student.aii.avans.nl/cmd/bvgeurts/flipperkast.html
It’s all in dutch, but you can take a look at the pictures if you want.

I have 5 targets (Buttons) and 5 LEDs.
Each target as it’s own LED.

I have one BONUS-target.
If you hit that target, all LEDs start blinking for 2 seconds and after those 2 seconds only 1 (randomly choosen) LED will be ON. The other 4 are OFF at that moment.
If the ball hits the target with the LED that is ON, he will get bonus points.

I managed to get a LED-score system.
I also got the targets and LEDs working.
Thanks to everybody here I fixt the LEDs. So if you hit the BONUS target, all LEDs start blinking and one RANDOM LED will be ON after the blinking.

But how can I fix it that the target with the LED gives the player bonus points. I need another Method, but everything I try gives me errors.

Where do I declare the LED, Buttons (Output- and InputPorts), variables if I have more methods wich all use those LEDs Buttons and variables?

I tried reading the wiki and the manual but I screw it up everytime. haha

You’ll need to show us your code

I tried it in the ways here above and several other ways.
But that didn’t work out, so I deleted those files. :confused:

It is difficult to help you if this is all the information you can provide.

You need to show up what had code is getting what errors.

yep without seeing code we cant do anything. And deleting a previous effort seems destructive, don’t do that next time :slight_smile: each piece of code is a learning experience

I’ll do a few other efforts soon (pretty bus at the moment) and post it if it doesn’t work. :slight_smile:

I tried in a lot of different approaches, but nothing did work.
I thought this way would work:


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

public class HelloWorld
{
    public static void Main()
    {
        OutputPort[] target_LED = new OutputPort[1];
        target_LED[0] = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di26, false);
        InputPort newball_but = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di39, true, Port.ResistorMode.PullUp);
    }

    public HelloWorld()
    {
        if (newball_but.Read() == false)
        {
            target_LED[0].Write(true);
        }
    }
}

I just took one LED and one Button to try if it works.
But what I want is that there is one bonus-button and 5 other buttons with each one with one LED with the same number.
(button1 + led1, button2 + led2, button3+ led3, 
 )

When the bonus-button is hit, a random number will be choosen and the LED with that number goes ON (true).
when the player hits the button with the same number, the score will add 40 points.

The number will be randomly choosen with:


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

the LED with that number will go on with this code:


                if (targetBonus_but.Read() == false)
                {
                    target_LED[random_number].Write(true);
                 }

And I thought I needed this code to give the player 40 points when he hits the button with the LED that is on. (with the same number)


              if (target_LED[random_number].Read() == true && target_but[random_number].Read() == false)
                {
                    score += 40;

                    target_LED[random_number].Write(false);     
                }

And when the player hits a button whit an LED that is OFF, he will get 20 points. For that part I use this code


                if (target_but[0].Read() == false || target_but[1].Read() == false || target_but[2].Read() == false || target_but[3].Read() == false || target_but[4].Read() == false)
                {
                    score += 20;
                    Thread.Sleep(200); 
                } 

But the score doesn’t add 40 in stead of 20 when the player hits the button with the LED that is on (it just adds 20
)
So can someone help me to get this done?

(I’m sorry if my english is bad or someone doesn’t understand me. I’m dutch and tired at the moment. So my brains don’t work that good :wink: )

[quote]And I thought I needed this code to give the player 40 points when he hits the button with the LED that is on. (with the same number)

if (target_LED[random_number].Read() == true && target_but[random_number].Read() == false)
                {
                    score += 40;
 
                    target_LED[random_number].Write(false);     
                }

And when the player hits a button whit an LED that is OFF, he will get 20 points. For that part I use this code

if (target_but[0].Read() == false || target_but[1].Read() == false || target_but[2].Read() == false || target_but[3].Read() == false || target_but[4].Read() == false)
                {
                    score += 20;
                    Thread.Sleep(200); 
                }

But the score doesn’t add 40 in stead of 20 when the player hits the button with the LED that is on (it just adds 20
)[/quote]

So are these two code snippets in one method? Assuming they are, then in both cases, the target_but[x].Read() will be false - so if anything both should be triggered and should add 40 then 20.

I think you need to show us more of your code. As they each stand, they should work. Have you tried to step into your code and debug what is going on, looking at variable values as you go?

Another approach might be to simply have a FOR loop that iterates through the 5; if the button is pressed, then if the LED is lit add 40, else add 20. Something like (written here, not tested)

for (int i=0;i<5;i++)
{
if (target_but[i].Read() == false) //I am assuming you have the button read working in the correct sense
{
if (target_LED[i]==true)
score+=40;
else
score+=20;
}
thread.sleep(200);
}

OkĂ©, I wasn’t thinking that good.
I did this and it works :slight_smile:


                if (target_but[0].Read() == false && target_LED[0].Read() == true)
                {
                    score += 40;
                    target_LED[0].Write(false);
                    Thread.Sleep(200); 
                }

But now I have another problems. The scoresystem normally adds 20 points and with this code above the player gets 40 points (bonus
).
The score is displayed with LEDs, I need a code something like this


if (score == ?20)
                {
                     SG1.Write(true);
                }

I don’t know how to manage this because a “?” doesn’t work at that place.
What I need is something that it doesn’t matter what will be standing before the 20 so the led goes on.

So when the score is 120 the LED SG1 goes on, when the score is 1020 the LED SG1 goes on. And also when the score is only 20 the LED SG1 goes on.
Does somebody know what I can use for that? is there a symbol for it or something?

you mean when the score is a multiple of 20? That can be calculated as when the remainder of (score/20) is zero. You want the modulo operator; specifically, your test will look something like: score%20==0

or if you mean that it would occur at 20, 120, 220, 320, 420, etc. then you would need to do score%100==20

Hi guys!

My project is almost finished, but the programming is totally done.
Take a look at my project here: Pinbox promo. - YouTube

I was building a foldable pinball machine. It was pretty hard to do, but everything works how I wanted it to work. I have a projectlog on my website, but it’s all in Dutch.

This is the website with the Projectlog (I have also pictures on it) http://student.aii.avans.nl/cmd/bvgeurts/flipperkast.html

The only thing I need to do now is making some nice artwork on the playfield.

Thanks for all the help with the coding :wink:

5 Likes

@ PsychoBram - well done, very cool project :slight_smile:

Very nice job! Will there be a write-up with all the juicy details?