February 29, 2016 at 19:12 #5583
I thought it would be nice to implement an event for the EV3 touchsensor which occurs when the state of the sensor changes (from pressed to released or from released to pressed). Reason is that the touchsensor is often used for user-input (to start or stop some kind of action). I have to say I’m still a novice to C#, but I managed to do this. It works fine, but only after a few seconds and presses on the touchsensor after program start. The first seconds nothing happens when the touchsensor is pressed, but after 7-10 sec it works fine: with every change in state of the touchsensor the event Changed occurs immediately.
It doesn’t seem to be time related, because when you wait 15 seconds after program start and than press the sensor, it still takes some time (7-10 sec) from that point for the events to occur for the first time. From that moment on it works perfectly well.
In the attachment are the two files of my project:
- TouchSensorEvents.cs containing the class TouchSensorEvents and the EventListener class.
- Program.cs: the testprogram which gives a beep when the state of the touchsensor changes.
To run this program, attach a touchsensor on port 1 of the EV3-brick.
My implementation is based on a combination of the ButtonEvents class of MonoBrick firmware and samples on the help of Visual Studio (MSDN resources).
What is the reason that the event doesn’t occur in the first seconds of the running program (or the EventListener of program doesn’t get any notification)? Any other remarkt on my code are welcome as well.
- This topic was modified 3 years, 4 months ago by Kees Smit.
Attachments:You must be logged in to view attached files.March 8, 2016 at 21:29 #5587
Ouch, stupid me…
The solution was simple. I made the mistake to create a MonoBrickFirmware.Sound.Speaker object in the method that is called whenever the event occurs (method StateChanged in TouchSensorEvents.cs). It takes some time to create this object for the first time, that’s why the events didn’t seem to come through at first press on TouchSensor. Instead of playing a sound I now write a line to the LCD and now everything works fine.
I hope my explanation is clear enough to understand it. As said before, I’m a newbe to C#…
Furthermore I simplyfied the code, see attachment.
- This reply was modified 3 years, 4 months ago by Kees Smit.
Attachments:You must be logged in to view attached files.
You must be logged in to reply to this topic.