Controlling Servo and Motor with Keyboard at the same time

I am fairly new to Arduino, and am struggling figuring out the correct way to program my code. I cant find any resources that represent controlling a servo and a motor simulataneously with the keyboard. Below is my code, which I assume is fairly messed up. What I am wanting the program to do is, when I press 'd' and 'a', the servo moves in its respective direction, depending on which is pressed. But I also want the motor to move depending on the direction the servo is moving. Ex, pressing 'a' makes the servo move, and the moto spin at 100 rpm. Pressing 'd' makes the servo move the other direction and spin at 250 rpm.

Any help would be greatly appreciated.

The motor I am using is the DAGU DG01D 48:1
The servo I am using is HiTEC HS-422

#include<Servo.h> // include server library
Servo ser; // create servo object to control a servo
int poser = 0; // initial position of server
int val; // initial value of input
int motorPin = 3;

void setup() {
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600); // Serial comm begin at 9600bps
  while (! Serial);
  Serial.println("Speed 0 to 255");
  ser.attach(9);// server is connected at pin 9
}

void loop() {
  if (Serial.available()) // if serial value is available 
  {
    int speed = Serial.parseInt();
    if (speed >= 0 && speed <= 255)
    {
    val = Serial.read();// then read the serial value
    if (val == 'd') //if value input is equals to d
    {
      poser += 30; //than position of servo motor increases by 1 ( anti clockwise)
      ser.write(poser);// the servo will move according to position 
      analogWrite(motorPin, 150);
      delay(5);//delay for the servo to get to the position
     }
    if (val == 'a') //if value input is equals to a
    {
      poser -= 30; //than position of servo motor decreases by 1 (clockwise)
      ser.write(poser);// the servo will move according to position 
      analogWrite(motorPin, 250);
      delay(5);//delay for the servo to get to the position
      
    }
  }
}
}

An RC servo or a DC motor? You say motor, but your code uses the servo driver.

One of the examples in the Arduino IDE:

/*
 Controlling a servo position using a potentiometer (variable resistor)
 by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

 modified on 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Knob
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

polymorph:
One of the examples in the Arduino IDE:

/*

Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott http://people.interaction-ivrea.it/m.rinott

modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);    // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                          // waits for the servo to get there
}

But this is controlling with a potentiometer, and only one device. I need to control it with keyboard commands and a servo and motor at the same time.

1 motor and 1 servo.
The motor I am using is the DAGU DG01D 48:1
The servo I am using is HiTEC HS-422

polymorph:
An RC servo or a DC motor? You say motor, but your code uses the servo driver.

KirkF22:
The motor I am using is the DAGU DG01D 48:1

Please post a link to the datasheets for the motor AND for the motor driver you are using.

I suspect your program is getting mixed up between the different values you are sending to it.

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.

...R

Gearmotor

Servo

Robin2:
Please post a link to the datasheets for the motor AND for the motor driver you are using.

I suspect your program is getting mixed up between the different values you are sending to it.

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.

...R

You have not posted a link to the datasheet for the motor driver that you propose to use with the gear motor. You can't connect it directly to an Arduino. If you do you may damage your Arduino.

...R

Well, I read that too fast.

We have a flyback diode in place for the gearmotor. I cant find any data sheet on the website on sparkfun. When I click data sheet only that .jpg file comes up.

Robin2:
You have not posted a link to the datasheet for the motor driver that you propose to use with the gear motor. You can't connect it directly to an Arduino. If you do you may damage your Arduino.

...R

KirkF22:
We have a flyback diode in place for the gearmotor.

I am not asking about the motor. I know what the motor is from your picture.

I am asking about the electronics that MUST go between the Arduino and the motor.

If that does not make sense to you then how are you proposing the power the motor? If I know that I can suggest a suitable motor driver board. And note that your cannot power the motor from the Arduino 5v pin - it can't provide enough current.

...R

Sorry. I actually wasn't aware I couldn't use the 5v pin. I used a test code for the moto plugged into the 5v pin and it worked no problem. I also had a code very similar to this that would run the motor, depending on what speed I entered, and the servo as well. The issue I was having with that is I could only get the servo to go one direction, and was unable to reverse it.

KirkF22:
Sorry. I actually wasn't aware I couldn't use the 5v pin. I used a test code for the moto plugged into the 5v pin and it worked no problem. I also had a code very similar to this that would run the motor, depending on what speed I entered, and the servo as well. The issue I was having with that is I could only get the servo to go one direction, and was unable to reverse it.

Can you post a diagram of how you had the motor connected to the Arduino? A photo of a simple pencil drawing will be fine. See this Simple Image Guide

...R

Attached is a photo of the setup. Before I didn't have that external power supply plugged in. New development however. I altered the code, and it seems to be working correctly, however, I keep losing connection to the board when controlling it. Sometimes it works properly up to 10 seconds, but then loses connection. I also am sensing that the servo is trying to force itself past its maximum limit, which may cause the internal mechanism I assume. Any idea what I could do code wise to limit the servo, so it shuts off when maximum angle is reached, and waits for the other input to reverse the direction of the servo? As well as any advice on the board disconnecting issue would be greatly appreciated!

New Code:

#include<Servo.h> // include server library
Servo ser; // create servo object to control a servo
int poser = 0; // initial position of server
int val; // initial value of input
int motorPin = 3;

void setup() {
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600); // Serial comm begin at 9600bps
  ser.attach(9);// server is connected at pin 9
}

void loop() {
  if (Serial.available()) // if serial value is available 
  {
    val = Serial.read();// then read the serial value
    if (val == 'd') //if value input is equals to d
    {
      poser += 30; //than position of servo motor increases by 1 ( anti clockwise)
      ser.write(poser);// the servo will move according to position
      analogWrite(motorPin, 125)d;
      delay(5);//delay for the servo to get to the position
     }
    if (val == 'a') //if value input is equals to a
    {
      poser -= 30; //than position of servo motor decreases by 1 (clockwise)
      ser.write(poser) ; // the servo will move according to position 
      analogWrite(motorPin, 255);
      delay(5);//delay for the servo to get to the position
    }
  }
}

Robin2:
Can you post a diagram of how you had the motor connected to the Arduino? A photo of a simple pencil drawing will be fine. See this Simple Image Guide

...R

You need to make a diagram showing all the connections. It is not possible to figure them out from your photo.

...R

Let me know if this is better. Thanks!

Robin2:
You need to make a diagram showing all the connections. It is not possible to figure them out from your photo.

...R

KirkF22:
Let me know if this is better. Thanks!

It's not. Fritzing diagrams are too easy to misunderstand.

Just get out a paper and pencil (or a drawing program like Powerpoint of LibreOffice Draw) and make a simple drawing with everything clearly labelled. Pencil and paper will be quicker than Fritzing also.

...R

Take a look at my signature for some links with tips on how to draw schematics for clarity.

Robin2:
It's not. Fritzing diagrams are too easy to misunderstand.

Just get out a paper and pencil (or a drawing program like Powerpoint of LibreOffice Draw) and make a simple drawing with everything clearly labelled. Pencil and paper will be quicker than Fritzing also.

...R

That drawing makes things much clearer. However you have not labelled the transistor or the resistor.

Now I can see that you are using a transistor as your motor driver - I had visions of you connecting the motor directly to an Arduino I/O pin.

Assuming you have a suitable transistor and base resistor that should work fine - I am not able to offer advice about transistors.

It is not a good idea to draw power for a motor or a servo through the Arduino board.

...R