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
4 Asked on December 22, 2021 by king-amada
1 Asked on December 17, 2021 by karol-wajs
1 Asked on December 17, 2021 by assam-alzookery
1 Asked on December 17, 2021 by mauricio-moreno
1 Asked on December 7, 2021 by dong-jae-lee
3 Asked on December 4, 2021 by nikita-semenov
2 Asked on November 30, 2021 by abdullah-ansari
1 Asked on November 28, 2021 by oscarm
1 Asked on November 28, 2021 by hamedmh
1 Asked on November 28, 2021 by shubham-wani
1 Asked on November 28, 2021
2 Asked on October 3, 2021
1 Asked on October 3, 2021 by astghik-hakobyan
2 Asked on October 3, 2021 by oualid
1 Asked on October 3, 2021 by j_f
0 Asked on October 3, 2021 by rrrruo
Get help from others!
Recent Questions
Recent Answers
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP