Take the 2-minute tour ×
LEGO® Answers is a question and answer site for LEGO® and building block enthusiasts. It's 100% free, no registration required.

My problem is that the robot, which should just remain in the program until the Escape Button is pressed, just stays in the while-loop. The current code looks like this:

using System;  
using MonoBrickFirmware;  
using MonoBrickFirmware.Movement;  
using MonoBrickFirmware.Display;  
using MonoBrickFirmware.Sensors;  
using MonoBrickFirmware.UserInput;  
using System.Threading; 

namespace ColorSensorExample  
{  
    class R_Main  
    {  
        public static void Main (string[] args)
        {  
            R_Main main = new R_Main ();
        }

        public R_Main()
        {

            var sensor = new EV3ColorSensor (SensorPort.In2);  
            ButtonEvents buts = new ButtonEvents ();  
            buts.EscapePressed += new Action(onEscape);
            while (true) 
            {

            }
        }

        private void onEscape()
        {
            EventWaitHandle stopped = new ManualResetEvent (false);
            stopped.Set ();  
        }             
    } 
}

But it does work with the example from the Monobrick homepage:

using System;  
using MonoBrickFirmware;  
using MonoBrickFirmware.Movement;  
using MonoBrickFirmware.Display;  
using MonoBrickFirmware.Sensors;  
using MonoBrickFirmware.UserInput;  
using System.Threading;  
namespace ColorSensorExample  
{  
class MainClass  
{  
  public static void Main (string[] args)  
  {  
        EventWaitHandle stopped = new ManualResetEvent(false);  
        ColorMode[] modes = {ColorMode.Color, ColorMode.Reflection,   
                             ColorMode.Ambient, ColorMode.Blue};  
        int modeIdx = 0;  
        var sensor = new ColorSensor(SensorPort.In1);  
        ButtonEvents buts = new ButtonEvents ();  

        buts.EscapePressed += () => {   
            stopped.Set();  
        };  
        buts.UpPressed += () => {   
            LcdConsole.WriteLine("Sensor value: " + sensor.ReadAsString());  
        };  
        buts.DownPressed += () => {   
            LcdConsole.WriteLine("Raw sensor value: " + sensor.ReadRaw());  
        };  
        buts.EnterPressed += () => {   
            modeIdx = (modeIdx+1)%modes.Length;  
            sensor.Mode = modes[modeIdx];  
            LcdConsole.WriteLine("Sensor mode is: " + modes[modeIdx]);  
        };    
        stopped.WaitOne();  
    }  
  }  
}
share|improve this question
add comment

1 Answer

Note: I have no particular knowledge of how work.

You're never escaping the while (true) loop. The example you're pointing too use an event-based model, and it looks like you prefer a loop. This is what you want:

class R_Main  
{  
    public static void Main (string[] args)
    {  
        R_Main main = new R_Main ();
    }

    public R_Main()
    {

        var sensor = new EV3ColorSensor (SensorPort.In2);  
        bool continue = true;
        ButtonEvents buts = new ButtonEvents ();  
        buts.EscapePressed += () => {continue = false;};
        while (continue) 
        {

        }
    }         
} 

But depending on the internals of this might not work at all.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.