read serial string from PC connected USB

Hi all

Wondering if anyone can point me in the right direction
I have a stepper linked to my uno board, the stepper rotates every time a push button is pressed.

Instead of me pressing the button to rotate the stepper What i would like to do is have my PC send a serial string ‘%13@’ via the USB connection of the uno, it reads this and then rotates the stepper every time it sees this string it then sends a return command to say I’ve it’s rotated.

Could this be possible?

the code i have to perform this already via the bush button is as follows:

/*changes
3: button!
int ButtonState and OnOrOffState
pinMode(2, INPUT);

*/

int Distance = 0; //How far we’ve traveled
int Speed = 1; //control how fast to “step” the motor
int NumSteps = 50; //how many steps does the stepper take to make a rotation (the actual motor, not factoring in “micro” steps)
int MicroSteps = 16; //how many microsteps is the Big Easy Driver set to (or whatever stepper driver is being used)
int FullRotation = NumSteps;
int ButtonState = 0; //stores status of button
int OnOrOffState = 0; //should motor running or should it be turned off

void setup() {
pinMode(8, OUTPUT); //DIRECTION pin (forward reverse)
pinMode(9, OUTPUT); //STEP pin (this increments the motor - e.g. makes it turn!)
pinMode(2, INPUT); //push button
digitalWrite(8, LOW);
digitalWrite(9, LOW);

}

void loop() {
ButtonState = digitalRead(2);
if (ButtonState == LOW){ //LOW means it has been pressed!
OnOrOffState = 1;} //(1 means it should be running!)}

while(OnOrOffState == 1){
digitalWrite(9, HIGH);
delayMicroseconds(5000);
digitalWrite(9, LOW);
delayMicroseconds(5000);
Distance = Distance + 1; // record this step
if (Distance == FullRotation) { //as soon as distance = “FullRotation”, that means we have made a full revolution!
OnOrOffState = 0;

Distance = 0; //reset our Distance counter!
//delay(10); //a pause at the end of each rotation; not necessary to have FYI
}
}
}

Thanks

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

You refer to a string “%13@” and if the % and the @ are start and end markers you could substitute those for the < and > in my third example if you prefer.

…R