Go Down

Topic: Robotic Auto-Turret control system programming (Read 76 times) previous topic - next topic

1Ghasthunter1

I am working on a stepper-motor controlled 2 axis turret, and need to have a serial connection between a raspberry pi/laptop and my arduino which will have accelstepper which connects to my steppers. However, I need to control 2 steppers simultaneously, meaning I need to constantly be sending two values from my laptop/pi to the arduino.

My specific question:
How can I program my arduino to receive two different integer values over serial from a laptop or pi, and use those values to control the speed and direction of a stepper motor?

I worked almost 10 hours today in an effort to have my laptop transmit this format: (xxx,xxx), where the x are numbers and the parenthesis are start and end codes. Then, I tried programming my Arduino to look at the comma, and separate the two sides of this into different values, however this did not work. 

slipstick

Try Serial Input Basics  Example 5 should get your data moving correctly.

Steve

1Ghasthunter1

Thanks steve!

I was actually basing my original script of the of example 3 from that series. My final code looks like the following:

// Example 5 - Receive with start- and end-markers combined with parsing

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
int xInt = 0;
int yInt = 0;

boolean newData = false;

//============

void setup() {
    Serial.begin(9600);
    Serial.println("Enter data in this style <xxx, yyy>  ");
    Serial.println();
}

//============

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,","); // this continues where the previous call left off
    xInt = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    yInt = atoi(strtokIndx);     // convert this part to a float

}

//============

void showParsedData() {
    Serial.print("XVal ");
    Serial.println(xInt);
    Serial.print("XVal ");
    Serial.println(yInt);
}

I will probably get rid of all serial prints and instead use the values to control my steppers.

Go Up