ustepper motor control over serial

Hi Guys, looking for some help. I got a ustepper running arduino code.

my mission is to create a linear rail running off a txtbox and btn in processing. set distance click ok, slide moves to that position.

I have been trying for 2weeks and am now asking for help, it sounds simple but im pulling my hair out. does anyone have sample code for this?

attached are the parts i'm having trouble with. I thank you all well in advance for taking the time to look at my train wreck.

//arduino //other code outside this sample loops through temp sensor ect and prints to txtbox's as a read out

void readinput(){``

if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.readString();

    if(inByte == "red"){

stepper.moveSteps(200, CCW, SOFT);

    }

if(inByte == "move"){

while (Serial.available() <= 0) {
Serial.println('A');
}


inByte = Serial.readString();
//Serial.println(inByte.toInt());
stepper.moveSteps(inByte.toInt(), CCW, SOFT);
//Serial.flush();
//Serial.println('A');
}
}
}







//processing code

public void button4_click1(GButton source, GEvent event) { //_CODE_:button4:783421:

myport.write("move");
//53.5 steps per mm

while (myport.available() <= 0){
 // println("fffffffffffffff");
   println(myport.read());
}
double aa = Double.parseDouble(textfield6.getText());
double ans = aa*53.5;
int aws = (int) ans;
//println(ans);
println(aws);

println(myport.read());


myport.write(Integer.toString(aws));
}

Answers

  • Possibly relevant: previous discussions of steppers:

  • Hello bennett4455, There is lots that can go wrong, e.g. where you test for avaiable > 0, what if 1 char has arrived? red is 3 chars, so the prog might see the r on one pass, ed next, never see red. You could try 'available >= 3' it might work, might get out of step and ignore all.

    The method I use is to accumulate incoming chars on the end of a string, then examine the string to see if there's a valid instruction. If there is take it off the front of the string and use it. If the string is long and first part is not valid, take it off and ignore. This scheme tolerates errors, well, at least it doesn't get stuck.

    I keep the comms as plain text, as you are. This means you can emulate your Processing app by simply typing commands into the Arduino serial monitor window (or Putty is better).

    A much simpler scheme is to use single characters, e.g. M for move R for ?. You need to transfer a number? Skip that for now? just repeat a single char each time you want a step.

    Once above is working think about the number. Use single letter followed by digits. Immediately here we have issues again, and need some waiting and checking.

    I think you should try the single character method to start with, so you can see something working, and enjoy it.

    Richard.

  • Answer ✓

    Now I've found the prog I used. I'm posting it exactly as used, even though I can see some possible improvements. I've only added comments. It will complain about the vars where I store the results, you substitute yours there, e.g. step_count, and routines I'm calling (it was a 12 led flashing demo).

    /*
    
    The message protocol tranmitted to the Arduino is e.g.
    
    *MD1234,
    
    The * is the start of the message.
    MD is a 2 char command, add receiving program to select any other combinations of 2 letters.
    1234 is text value, can be any number of characters
    , is a terminating ,
    
    The receiving code accumulates characters until it sees the whole message, then interprets the value, and selects the action for the command.
    
    
    */
    
    // Globals  (above setup)
    const unsigned int in_bf_sz = 50;
    //
    char in_buff0[in_bf_sz];  // input buffer
    unsigned int in_ptr0;     // pointer to next location to store input chard
    unsigned int in_state0;     // 0 not started, 1 = we have a *, 2 = we have a ,
    unsigned int ptr_star0;   // pointer to the *
    unsigned int ptr_coma0;   // pointer to the ,
    
    
      // loop()...
    
        char            in_ch;
        char                    ch1;
        char                    ch2;
        unsigned int    in_val;
    
    
    
        // Get input serial without delaying the program.
        if (Serial.available() > 0)
        {
            // store any rcvd chars
            in_ch = Serial.read();
            in_buff0[in_ptr0++] = in_ch;
    
            // have we rcvd a *?
            if (in_state0 == 0 && in_ch == '*')
            {
                // yes, note where, and inc state
                ptr_star0 = in_ptr0 - 1;
                in_state0 = 1;
            }
    
            // have we rcvd a ,
            if (in_state0 == 1 && in_ch == ',')
            {
                // yes, note where and inc state
                ptr_coma0 = in_ptr0 - 1;
                in_state0 = 2;
            }
    
            // have we a complete message?
            if (in_state0 == 2)
            {
                // convert the text digits to integer
                in_buff0[ptr_coma0] = '\0';
                sscanf(&in_buff0[ptr_star0 + 3], "%d", &in_val);
    
                // store the command chars
                ch1 = in_buff0[ptr_star0 + 1];
                ch2 = in_buff0[ptr_star0 + 2];
    
                // command chars = MD? = mode change
                if (((ch1 & 0xDF) == 'M') && ((ch2 & 0xDF) == 'D'))
                {
                    L12Mode = in_val;
                    number = 0;
                    update12LEDs( 0 );
                }
                // command chars = LV? = lights value change.
                if (((ch1 & 0xDF) == 'L') && ((ch2 & 0xDF) == 'V'))
                {
                    update12LEDs( swap_ends_12(in_val) );
                    //update12LEDs( in_val );
                    L12Mode = 0;
                    number = 0;
                }
                // go back to beginning of buffer
                in_state0 = 0;
                in_ptr0 = 0;
            }
            // this shouldn't happen, but if we have not made sense of incoming
            // force it back to beginning of buffer.
            if (in_ptr0 >= in_bf_sz)
            {
                in_ptr0 = 0;
            }
        }
    
  • Thanks Richard, I will have a play about over the next few days. very helpful comments

Sign In or Register to comment.