1

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

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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