I have been reading about image processing data using C#. I need to send to Arduino object coordinates. I sent data about x coordinate with the code I wrote below, but I still could not send y coordinates, because I don't know how Arduino will separate x and y coordinates. Is there a method to send data from 2 different channels?
if (serialok == true) {
int second =0;
int offset=300;
second = offset - Math.Abs(objectX);
map =(float) 0.85 * second;
buffer[0] = (byte)Math.Abs((int)map);
serialPort1.Write(buffer, 0, 1);
This is how I read the above code from Arduino.
if(Serial.available()>0) {
inbyte=Serial.read();
}
servo1.write(map(inbyte,0,255,0,180));
delay(15);
Sorry for my English.
Solution by OP.
combines the data as a string in c# , send that sting value serial port 1 and parse on arduino .
#include <Servo.h>
Servo servo1;
Servo servo2;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
servo1.attach(2);
servo2.attach(4);
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC = atoi(strtokIndx); // convert this part to an integer
servo1.write(map(integerFromPC, 0, 255, 0, 180));
strtokIndx = strtok(NULL, ",");
floatFromPC = atoi(strtokIndx); // convert this part to a float
servo2.write(map(floatFromPC, 0, 255, 0, 180));
}
//============
void showParsedData() {
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.