Hi!
I am currently trying to make my arduino uno communicate with a Joystick connected to my computer. I have written a c# program which sends the data from the joystick via COM3 to arduino.
There is no problem sending an int with the data of just one direction, that works great. The problem arises when I try to send different direction. I realised that I would needdifferent "channels" or "tags" on the data to specify which direction the data sent was regarding.
loop()
{
if (Serial.available() > 0)
{
char cmd1 = Serial.read();
if (cmd1 == 'y')
{
delay(2);
int offset = Serial.read();
//Do stuff
}
}
}
So my c# application was sending first the letter corresponding to the axis, in the example "y", followed by the data. After that I would read in the data but it's just nonsense.
I tried several different ways to do it. For example, I read the data in with Serial.readBytes(...) where i was able to put my data (and print it) in a character array, but I still can't seem to convert it to something usable.
So basically what I want is to be able to send several different types of data to the arduino in some way.
That gives me the data in the array. If I sent "1234", data is {'1','2','3','4'}. But I can't seem to convert them to the original number "1234" no matter what functions I use. I have been googling and trying different aproaches all day.
I understand you want to send X and Y values from the PC to the Arduino.
What I'd do is send a message containing an X value and a Y value, and encode the message as an ascii string containing comma separated variables with each message separated by a new line.
On the sending side you just print out the X value, a comma, the Y value and a line separator.
On the receiving side you could receive each character as it arrives and buffer then until you receive a line separator, then parse the buffer content to separate out the textual X and Y values and convert them to numbers. You can use strtok() to extract the textual values and atol() to convert each textual value to a number.