Dear Duke:
I connect the Dust sensor to Grove J4 and to FEZ Cerbuino Bee Socket 3.
But seems not work.
I always get the concentration =0.62, seems the Interrupt event not fired.
Below is my code
Thanks
public partial class Program
{
// This method is run when the mainboard is powered up or reset.
private GT.Timer _sensorTimer;
private GT.Interfaces.InterruptInput _dustSensor;
private bool _pulseLow = false;
public const Int64 ticks_per_millisecond = System.TimeSpan.TicksPerMillisecond;
private Int64 _startTime = 0;
private Int64 _lowTime = 0;
void ProgramStarted()
{
/*******************************************************************************************
Modules added in the Program.gadgeteer designer view are used by typing
their name followed by a period, e.g. button. or camera.
Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
button.ButtonPressed +=<tab><tab>
If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
timer.Tick +=<tab><tab>
timer.Start();
*******************************************************************************************/
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
//method 1
_dustSensor = eBlockExpansion.SetupInterruptInput(GT.Socket.Pin.Three, GT.Interfaces.GlitchFilterMode.Off, GT.Interfaces.ResistorMode.Disabled, GT.Interfaces.InterruptMode.RisingAndFallingEdge);
_dustSensor.Interrupt += new InterruptInput.InterruptEventHandler(_dustSensor_Interrupt);
_sensorTimer = new GT.Timer(60000);
_sensorTimer.Tick += new GT.Timer.TickEventHandler(_sensorTimer_Tick);
_sensorTimer.Start();
}
void _dustSensor_Interrupt(InterruptInput sender, bool value)
{
Debug.Print("pulse detected");
if (value)
{
_lowTime = _lowTime + (GetCurrentTimeInTicks() - _startTime);
_pulseLow = false;
}
else
{
_startTime = GetCurrentTimeInTicks();
_pulseLow = true;
}
}
public static long GetCurrentTimeInTicks()
{
return Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks;
}
void _sensorTimer_Tick(GT.Timer timer)
{
Mainboard.SetDebugLED(true);
//in case we are in the middle of a LOW
if (_pulseLow)
{
Int64 now = GetCurrentTimeInTicks();
_lowTime = _lowTime + (now - _startTime);
_startTime = now;
}
double ratio = (_lowTime / (double)ticks_per_millisecond) / 600.0;
_lowTime = 0;
double concentration = CalculateConcentration(ratio);
//display_HD44780.Clear();
//display_HD44780.PrintString(concentration.ToString());
Debug.Print(concentration.ToString());
Debug.Print(concentration.ToString());
// StoreResult(concentration);
Mainboard.SetDebugLED(false);
}
private static double CalculateConcentration(double ratio)
{
double concentration = 1.1 * (ratio * ratio * ratio) - 3.8 * (ratio * ratio) + 520.0 * ratio + 0.62;
return concentration;
}
}