Robotics Asked by jpcreeper13 on October 31, 2020
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();
}
}
}
}
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.
Answered by Palo on October 31, 2020
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();
}
}
Answered by Mark Omo on October 31, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP