0

I'm creating a guitar hero kind of game in unity using an Arduino and an actual guitar.

My problem is that I have to constantly check for a returning value from the arduino.

If I constantly send a value my readings aren't accurate enough any more, and if I write my code for the Arduino in such a way that it only sends data when it receives input from my guitar, the unity program freezes due to it getting stuck on the Arduino code as it seems to keep waiting for a response. Is there a way that I can work around this?

Lex Li
  • 60,503
  • 9
  • 116
  • 147
MauritsE
  • 1
  • 1
  • 3
    Can you please post some code? It's difficult to tell what you are trying to achieve. – user703016 Dec 30 '11 at 09:52
  • You really have to help us more... post some code or explain why you really need to check the value. Anyway, if you HAVE TO check the return value and it returns a lot of value, there's nothing to do about it ? – Guillaume Slashy Dec 30 '11 at 10:12
  • I need to check the value because i need to see which note is played. this has to be checked constantly due to a scoring system. in my update function i'm calling the following function `public float Getfreqval() { string valuetext = stream.ReadLine(); //Read the information freqvalue = System.Convert.ToSingle(valuetext); return freqvalue; }` stream is my serialPort. i was basically just wondering if there is a way to not make the stream.readline wait for a value being returned. this is my first time using the system ports in c# so its all really new to me. – MauritsE Dec 30 '11 at 10:35
  • Thanks, I didn't really understand how to use this event at first, but now i seem to have got it working! thanks for the help! – MauritsE Dec 30 '11 at 13:52

1 Answers1

0

I understand your basic question to be:

Q: I'd like to interface my Arduino to a C# application running under Windows.

A: There are two ways you can do it:

  • Polling

  • Events

Polling is Bad :)

The correct way is to use an event, if at all possible.

Assuming you can use the C# serial interface, then you can assign an event to DataReceived.

Here's an article that discusses precisely that:

http://www.johnciacia.com/2010/06/03/interacting-with-the-arduino-with-c/

Look for:

serialPort1.DataReceived += 
  new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);

Here's another link: it looks like "Scrobby4" posted some example code:

http://forum.unity3d.com/threads/14731-COM1-and-events

paulsm4
  • 114,292
  • 17
  • 138
  • 190