Okay, I'm thinking the title probably isn't very descriptive but it's late and it's the best I can do. Basically, I've written a program in C# (I shall unveil this on here in a few days) that ouputs a value between 0 and 180 based on certain conditions. I want to be able to take this value and print it over the serial and read it at the Arduino end as an integer, so i can just have myServo.write(int).
At the moment, in C# I'm using sp.Write(int.ToString()); but I can use the same command to write a byte array and a char array.
Based on this, first of all, what's the bend form to send it through the serial in? Second of all, how do I pick that up at the other end and make it back into an integer? I'm fairly new to the Arduino side of this so sorry for no copies of my attempts at my own Arduino code..!
I can’t see why you would send it like a string in the first place. On the write method there is an overload for sending any number of bytes to the serial. http://msdn.microsoft.com/en-us/library/ms143551.aspx So if you put your value in a 1 element array and just send the value instead of the string representation, the Arduino could just read it directly.
This is what I thought, but I don’t have a clue about how sending values over the serial works so I’m not sure how to do it! Would I just put the integer value into a byte array using something like this?
I did try that, and for numbers from 1 to 9 that works fine, as you can just do Serial.read() - 48 to convert the ASCII into an integer. Couldn’t make it work though for anything bigger, but I might have been approaching it from the wrong angle!
Okay, so that definitely works - if I send 123 and tell the Arduino to light the LED on pin 13 if Serial.read() == 123, the LED lights. However, if I tell it to turn off if Serial.read() == 321 nothing happens. I’m presuming this is because I need to clear the serial buffer? I tried using Serial.flush() but that didn’t work…
This is my really rather basic Arduino code:
val = Serial.read();
if (val == 123)
{
digitalWrite(13, HIGH);
}
if (val == 321)
{
digitalWrite(13, LOW);
}
Serial.flush();