using System;
using System.Collections.Generic;
using System.Text;
using Bram.NxtSharp;
namespace LegoNxtConsoleSample
{
class Program
{
const string c_BluetoothComPort = "COM4";
static void SensorDemo()
{
//sensors
//the general use pattern of attaching sensors and
//looping through sensors is similar and so not shown here
int i = 0;
NxtBrick brick;
brick = new NxtBrick();
brick.COMPortName = c_BluetoothComPort;
brick.Connect();
NxtPressureSensor pressureSensor = new NxtPressureSensor();
pressureSensor.Brick = brick;
brick.Sensor1 = pressureSensor;
pressureSensor.InitSensor();
NxtSoundSensor soundSensor = new NxtSoundSensor();
soundSensor.Brick = brick;
brick.Sensor2 = soundSensor;
Console.WriteLine("sound sensor adjusted for human ear");
soundSensor.AdjustForHumanEar = true;
soundSensor.InitSensor();
i = 0;
while (i++ < 10)
{
soundSensor.Poll();
Console.WriteLine("sound sensor raw value:" + soundSensor.Value);
Console.WriteLine("sound sensor value:" + soundSensor.Value);
System.Threading.Thread.Sleep(500);
}
Console.WriteLine("sound sensor not adjusted for human ear");
soundSensor.AdjustForHumanEar = false;
soundSensor.InitSensor();
i = 0;
while (i++ < 10)
{
soundSensor.Poll();
Console.WriteLine("sound sensor raw value:" + soundSensor.RawValue);
Console.WriteLine("sound sensor value:" + soundSensor.Value);
System.Threading.Thread.Sleep(500);
}
//lightsensor used uninitialized
NxtLightSensor lightSensor = new NxtLightSensor();
lightSensor.Brick = brick;
brick.Sensor3 = lightSensor;
Console.WriteLine("raw lightsensor value before poll:" + lightSensor.RawValue);
Console.WriteLine("lightsensor value before poll:" + lightSensor.Value);
lightSensor.Poll();
Console.WriteLine("raw lightsensor value:"+lightSensor.RawValue);
//note that this behaves differently when "initialized"
//but only when brick was "off" before or different sensor was
//used
Console.WriteLine("lightsensor value:" + lightSensor.Value);
//initSensor is done at brick.Connect(), so usually attach sensors first,
//then connect brick!
lightSensor.InitSensor();
Console.WriteLine("raw initialized lightsensor value before poll:" + lightSensor.RawValue);
Console.WriteLine("initialized lightsensor value before poll:" + lightSensor.Value);
lightSensor.Poll();
Console.WriteLine("raw lightsensor value:" + lightSensor.RawValue);
Console.WriteLine("initialized lightsensor value:" + lightSensor.Value);
//turn on ligth on lightSensor, needs InitSensor for change of lightstatus
lightSensor.Active = true;
lightSensor.InitSensor();
Console.WriteLine("raw active lightsensor value before poll:"
+ lightSensor.RawValue);
Console.WriteLine("lightsensor active value before poll:"
+ lightSensor.Value);
lightSensor.Poll();
Console.WriteLine("raw active lightsensor value:" + lightSensor.RawValue);
Console.WriteLine("active lightsensor value:" + lightSensor.Value);
//turnoff light
lightSensor.Active = false;
lightSensor.InitSensor();
//autopolling lightsensor
brick.AutoPoll = true;
lightSensor.AutoPoll = true;
lightSensor.AutoPollDelay = 1000;
//note that usually you will set up
//sensors and autopoll before first brick.Connect()
//which will do StartPolling for you
brick.StartPolling();
while (i++ < 20)
{
//this should show that AutoPollDelay matters
Console.WriteLine("Light sensor raw value:"+lightSensor.RawValue);
System.Threading.Thread.Sleep(500);
}
brick.StopPolling();
//sonar sensor measures distance
NxtSonar sonarSensor = new NxtSonar();
sonarSensor.Brick = brick;
brick.Sensor4 = sonarSensor;
sonarSensor.InitSensor();
i = 0;
while (i++ < 20)
{
sonarSensor.Poll();
Console.WriteLine("Sonar sensor raw value:"+sonarSensor.RawValue);
System.Threading.Thread.Sleep(500);
}
//disconnect Sensor2
brick.Sensor2 = null;
foreach(NxtSensorPort sensorPort in brick.SensorPorts())
{
Console.Write(sensorPort.ToString()+" ");
if (brick.GetSensor(sensorPort) == null)
Console.WriteLine("is not connected");
else
Console.WriteLine("is connected with a "
+ brick.GetSensor(sensorPort).ToString());
}
foreach(NxtSensor sensor in brick.ConnectedSensors())
{
sensor.Poll();
Console.WriteLine(sensor.GetType().Name + " is connected to port " + sensor.Port.ToString() + " and has value " + sensor.RawValue);
}
//of further interest could be:
//sensor.LastResult of type NxtGetInputValues
brick.Disconnect();
}
static void Main(string[] args)
{
SensorDemo();
}
}
}