serial control of dc motor

Hello folks! in the past few days I have been trying to control the speed of my dc motor through my PC , reading the value from the keyboard instead of through a potentiometer. I have tried many codes but no success yet. tried searching the internet too. I have my motor connected to a mosfet h-bridge. the direction control is fairly easy and it works, but when i want to use the PWM that's where nothing is happening. Also the motors run when I give it a digital output. So if anybody have an idea out there please let me know. :-/

So if anybody have an idea out there please let me know.

How about posting some code? If it doesn’t work, we can suggest changes to make.

Generally speaking, you will probably require some type of program running on your pc to detect the pc keypress and then send the desired data to the arduino.

Thank you all very much for replying. Actually I am planing to make an interface for the x-sim software to run some dc motors and I was trying with some mini codes while learning it. I just got an arduino so its my first time. softwares sends an ascii value for axis and a value from 0-255 for position. first I wrote the following code but I couldn’t find a way around for speed when I move the joystick. the motor just turn at a very slow speed but changes direction.

In here I actually don’t know the meaning of‘0’. can someone please tell my why I need that piece? I learnt about this in this forum and it works. Also how can I have speed control in each direction?

Pls remember this is just a crude code and without any position feedback as of yet. I am just working to learn how I can control the speed in both direction so far.

//testing of interface with serial control:

int M1= 3; //motor 1:
int M2= 5; //motor 2:
int MD_1 = 4; //motor_1 direction :
int MD_2= 6; //motor_2 direction :
int x;

void setup() {
//initialize serial communication:

// initialize output pins:


void loop(){

if(Serial.available()>0) {
int x =‘0’;
if (x>127){
if (x<127) {


void forward(){



void reverse(){

digitalWrite(MD_1,LOW) ;


Sorry for so many questions, but its been about three days i’ve been banging my head over this. Any help! out there would be greatly appreciated.
Thank you.

The - ‘0’ converts an ASCII decimal digit to binary.
Thus it is unlikely that a single?digit could ever be ‘> 127’.
It is not recommended to read a serial character without first checking to see if one is available.