We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
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).
Thanks Richard, I will have a play about over the next few days. very helpful comments