2
$\begingroup$

In my program, I'm need to detect if the NXT touch sensor is pressed.

var nxt = new Brick<Sensor, Sensor, Sensor, Sensor>("usb");
nxt.Connection.Open();
nxt.Sensor1 = new TouchSensor();
nxt.Sensor1.Reset(false);
nxt.Sensor1.Initialize();
Console.WriteLine(nxt.Sensor1);

When I start the program, the sensor value always reads 0. But I discovered that if I go into the "View" menu in the NXT and see the touch sensor value, the program value reads 1. I can't do that for my setup. Also, I can't use Bluetooth; my computer doesn't have it. Can someone help me?

EDIT: my full code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MonoBrick.NXT;
using MonoBrick;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;

namespace MonoBrick
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                var nxt = new Brick<Sensor, Sensor, Sensor, Sensor>("usb");
                nxt.Connection.Open();
                nxt.Sensor1 = new TouchSensor();
                nxt.Sensor1.Reset(false);
                nxt.Sensor1.Initialize();
                Console.WriteLine(nxt.Sensor1.ReadAsString());
                nxt.Beep(500);
                System.Windows.Application application = new System.Windows.Application();
                application.Run(new Window1());
                nxt.Connection.Close();
            }

            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
                Console.WriteLine("Press any key to end...");
                Console.ReadKey();
            }
        }
    }
}
$\endgroup$
0
$\begingroup$

You need to read the value continually in order to get the updated value, see the tutorial on there website.

Here is what the code to do that might look like (untested as I do not have a NXT):

using System;
using MonoBrick.EV3;
public static class Program{
    static void Main(string[] args)
    {
        var nxt = new Brick<Sensor, Sensor, Sensor, Sensor>("usb");
        nxt.Connection.Open();
        nxt.Sensor1 = new TouchSensor();
        nxt.Sensor1.Reset(false);
        nxt.Sensor1.Initialize();
        ConsoleKeyInfo cki;  
        Console.WriteLine("Press Q to quit");  
        do   
        {  
            cki = Console.ReadKey(true); //press a key  
            Console.WriteLine(nxt.Sensor1);                       
            }  
        } while (cki.Key != ConsoleKey.Q);
        ev3.Connection.Close();
    }
}
$\endgroup$
  • $\begingroup$ this is what I tried before, it doesn't work $\endgroup$ – jpcreeper13 Feb 10 '17 at 22:04
  • $\begingroup$ What did you do? Can you post all your code? What errors did you get? How did you try to fix them? $\endgroup$ – Mark Omo Feb 10 '17 at 22:25
  • $\begingroup$ I tried using do...while. All my code won't fit. I got no errors. I tried to fix the error by using do...while $\endgroup$ – jpcreeper13 Feb 10 '17 at 22:34
  • $\begingroup$ post it in your original post, along with any output. $\endgroup$ – Mark Omo Feb 11 '17 at 19:07
  • $\begingroup$ I changed it. Up top $\endgroup$ – jpcreeper13 Feb 14 '17 at 22:11
0
$\begingroup$

For some reason MonoBrick is not able to force the NXT to configure the sensor, or it should be done in some other way, which I do not know of.

However, your program works, if you go to the "View" menu on NXT brick and select Touch sensor on Port1. NXT will be showing the value from the sensor, and your program as well.

Likewise, if you write a program for NXT in NXT-G for instance that will use a sensor read block to read value from touch sensor on Port1 first (and then do whatever else...), then your program will also read the value of the sensor, while the NXT program keeps running.

At least this is the way it worked for me.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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