C++ User Input sent to Arduino

I am trying to create a program that will prompt the user on the computer for an integer number and then use that number to define the number of steps to move a stepper motor forward. I’ve written a basic C++ code for the user prompt that works. I’ve also written an arduino code that activates my stepper motor when I manually enter the number of steps. I am currently using Visual Studio 2010 with the Arduino plugin from Visual Micro.

1: Is there a way in Visual Studio to take the input from C++ and pass it to the Arduino code?

2: Would it be more efficient/easier to rewrite the Arduino code and incorporate it into a C++ only program?

My C++ code:

#include<stdio.h>
#include<math.h>

int main()
{	//Delare variables
	int steps;// define steps and an integer number variable
	char go;  // define go as a character variable
	while(1)  // loop until data is entered
	{//Get Input
		printf("\Enter number of steps to move foward  ");
		fflush(stdin);
		scanf_s("%d", &steps);// get # of steps

	//Print results
		printf("\nStepper moving foward %d steps", steps);
		fflush(stdin);
		scanf_s("%c", &go);// pause program untill ready to continue
	return 0;
	} //end while (1) loop
}//end main

My Arduino Code:

#include <AFMotor.h>
AF_Stepper motor(200, 2);

void setup()
{
// Sets motor speed to 10 rpm
  motor.setSpeed(10);     
// Activate motor 
  motor.step(100, FORWARD, SINGLE); 
// disengage motor
  motor.release();
}

void loop()
{

}

I am a novice at programing in both languages and would like some helpful insight into this. Thank you.

Do you want the PC program to generate a version of the code with the number of steps to step and then upload it, or do you want the PC program to send a message to the arduino telling it how many steps to go?

For the second option (which is what you almost certainly need to go with) you just need to research how to open a serial port and send data through it. You'll only need the most basic uses of whatever serial library you use, so it probably doesn't matter too much which one.

1: Is there a way in Visual Studio to take the input from C++ and pass it to the Arduino code?

Yes. There is a Serial class that C++ can use to open, read from, write to, and close the serial port.

2: Would it be better to rewrite the Arduino code and incorporate it into a C++ only program?

Better than what? Sliced bread?

Your Arduino code will need to read from the serial port, and convert the data read back to an int, to define the number of steps. Exactly how to do that depends on whether you send binary or ASCII data to the serial port, in the C++ application.

I want the PC program to take a single input from the user then upload it to the the arduino. My starting idea was to run the C++ program first then upload the updated arduino code.

However, from what you are saying I think it might be more efficient to have the C++ code get the input, open the serial port, and write the input to the arduino. My arduino code should then include a loop to listen for/accept input from the serial port and then execute the on board code that was uploaded to it.

well actually i've done that, write some code in c++ or in c# to do it, not sure if i did it in c# or c++ but the ideas behind it are the same i think.

you just drop the serial icon on your windows form the difficulty is to raise a thread based on the serial comport that can be triggered to read info from usb sending from pc is more easy. i had posted my code in this blog (take a look of my posts (is that possible))