Hello All,
I'm a little new to c# and hoping someone can help me with a timer problem I have. I would like an input to start a timer. I also need to know if the timer is currently running. While the timer is running I would like to count how many times an input has activated. Once the timer is complete I need to see if the inputs have activated the correct number of times.
Below is an example of my code that I know is incorrect. Could someone comment on how I could find various states of the timer and how I would get one started with an input?
Thanks.
using System; using System.Net; using System.Net.Sockets; using System.Threading.Timer; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.NetduinoPlus; using Microsoft; using System.Text; using System.Resources; using System.Threading; namespace Glue_detection_application { public class Program { public static void Main() { //initialize timer Timer timer1 = new Timer(TimerWindow,null,0,5000); //count values for senors int Sens2Count = 0; int Sens3Count = 0; //declare I/O OutputPort led = new OutputPort(Pins.GPIO_PIN_D0, false); InputPort Sens1 = new InputPort(Pins.GPIO_PIN_D1,false,Port.ResistorMode.Disabled); //trigger timer //declare interupts InterruptPort Sens2 = new InterruptPort(Pins.GPIO_PIN_D2, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh); InterruptPort Sens3 = new InterruptPort(Pins.GPIO_PIN_D3, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh); //define interrupt methods Sens2.OnInterrupt += new NativeEventHandler(Sens2_OnInterrupt); Sens3.OnInterrupt += new NativeEventHandler(Sens3_OnInterrupt); while (true) { bool SensorActivate = Sens1.Read(); if (SensorActivate) { timer1.start(); } if(/* timer is done */) { if ((Sens2Count!= 2) || (Sens3Count!=3) { led.Write(true); Sens3Count = 0; Sens2Count = 0: } else { led.Write(false); Sens2Count=0; Sens3Count=0; } } static void Sens3_OnInterrupt(uint data1, uint data2, DateTime time) { //if in timer window Sens2Count++; } static void Sens2_OnInterrupt(uint data1, uint data2, DateTime time) { //if in timer window Sens3Count++; } } } }
The big thing about C# is the event handlers. Instead of looping through code and constantly checking the state of a port, you can add an event handler and have the port tell you when its state has changed. I rewrote your code to take advantage of event handlers. You'll also notice I moved the variables outside the Main() function and made them static so they can be seen by the events.
I haven't debugged the code, but it should be what you're looking for....
using System; using System.Threading; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware.NetduinoPlus; namespace TimerSample { public class Program { // Input that triggers the timer static InputPort Sens1 = new InputPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.Disabled); // Output to do stuff with static OutputPort led = new OutputPort(Pins.GPIO_PIN_D0, false); // Interrupts that we need to track static InterruptPort Sens2 = new InterruptPort(Pins.GPIO_PIN_D2, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh); static InterruptPort Sens3 = new InterruptPort(Pins.GPIO_PIN_D3, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh); // The timer static Timer timer = null; // Input counts static int Sens2Count = 0, Sens3Count = 0; public static void Main() { // Watch for when the input is triggered Sens1.OnInterrupt += new NativeEventHandler(Sens1_OnInterrupt); // Watch for when the interrupts are triggered Sens2.OnInterrupt += new NativeEventHandler(Sens2_OnInterrupt); Sens3.OnInterrupt += new NativeEventHandler(Sens3_OnInterrupt); while (true) { // Do stuff } } static void Sens1_OnInterrupt(uint data1, uint data2, DateTime time) { // If the timer isn't started... if (timer == null) { // ... then start the timer timer = new Timer(timerDelegate, null, 0, 5000); } else { // Stop the timer timer = null; } } static void Sens2_OnInterrupt(uint data1, uint data2, DateTime time) { // Up the count Sens2Count++; } static void Sens3_OnInterrupt(uint data1, uint data2, DateTime time) { // Up the count Sens3Count++; } static TimerCallback timerDelegate = new TimerCallback(TimesUp); private static void TimesUp(object state) { // Check the sensor counts if (Sens2Count != 2 || Sens3Count != 3) { led.Write(true); Sens3Count = 0; Sens2Count = 0; } else { led.Write(false); Sens2Count = 0; Sens3Count = 0; } } } }
Let me know if you have any questions...
NICK
- TechnoGuy likes this