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)
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…
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.
}
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
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’”
@ 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);
@ 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: GHI Electronics – Where Hardware Meets Software
@ James It’s allready working again. I just copied the whole code and pasted it in a new project…
But here is (hopefully) my last question.
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”