Hi all, OK well this seems to be such a noob question that I can't find anything online about it. I would like to control 1 servo and on small DC motor via serial input, I have 2 sample sketches (see below), which control a servo and motor individually but have no idea how to put them together so that I can control the motor and servo together.
Any Help would be greatly appreciated. I have add the 2 sketches below.
Thanks in advance
Servo Controller:
int servoPin = 7; // R/C Servo connected to digital pin
int myAngle; // angle of the servo (roughly in degrees) 0-180
int pulseWidth; // function variable
int val; // variable used to store data from serial port
void servoPulse(int servoPin, int myAngle) {
pulseWidth = (myAngle * 9) + 700; // converts angle to microseconds
digitalWrite(servoPin, HIGH); // set servo high
delayMicroseconds(pulseWidth); // wait a very small amount
digitalWrite(servoPin, LOW); // set servo low
delay(20); // refresh cycle of typical servos (20 ms)
}
void setup() {
pinMode(servoPin, OUTPUT); // set servoPin pin as output
Serial.begin(19200); // connect to the serial port
Serial.println("Servo Serial Simple ready");
}
void loop() {
val = Serial.read(); // read the serial port
// if the stored value is a single-digit number, blink the LED that number
if (val >= '0' && val <= '9' ) {
val = val - '0'; // convert from character to number
val = val * (180/9); // convert from number to degrees
Serial.print("moving servo to ");
Serial.print(val,DEC);
Serial.println();
for( int i=0; i<50; i++ ) {
servoPulse(servoPin, val);
}
}
}
DC Motor Controller:
int motorPin = 9;
int val = 0; // variable to store the data from the serial port
void setup() {
pinMode(motorPin,OUTPUT); // declare the motor's pin as output
Serial.begin(19200); // connect to the serial port
Serial.println("Welcome to SerialMotorSpeed!");
Serial.println("Enter speed number 0-9:");
}
void loop () {
val = Serial.read(); // read the serial port
if (val >= '0' && val <= '9' ) {
val = val - '0'; // convert from character to number
val = 28 * val; // convert from 0-9 to 0-255 (almost)
Serial.print("Setting speed to ");
Serial.println(val);
analogWrite(motorPin,val);
Serial.println("Enter speed number 0-9:");
}
}
Moderator edit: OP, you can finish off, and put the DC controller code in a code box.
You can try this way, change '0' to '9' on the DC motor into something else, say 'A' to 'J' so when you type in characters such as 'J' the DC motor runs and the servo is not affected.
Then you just combine the setup() from the two programs and save goes with loop() and everything outside these two functions. Give it a try.
AWOL I will check out the servo library, in all honesty I thought that only controlled the servo but I am very new to this so will research that option.
Luidr, thanks will have a look, it is that whole bit about joining the two sketches into one that has me confused. Tried combining them with no success but at the moment it really is shooting in the dark with all this coding work at least until I have some more experience, will have to read up on that one.
I can understand noobs need more help but don't expect to get spoon-fed for everything. You should try combining the code yourself and see how your way works/not-works and how the following code works fine. Tested with my servo and an LED. I can't find my transistors. It's holidays!
int servoPin = 7; // R/C Servo connected to digital pin
int myAngle; // angle of the servo (roughly in degrees) 0-180
int pulseWidth; // function variable
int val; // variable used to store data from serial port
int motorPin = 9;
void servoPulse(int servoPin, int myAngle) {
pulseWidth = (myAngle * 9) + 700; // converts angle to microseconds
digitalWrite(servoPin, HIGH); // set servo high
delayMicroseconds(pulseWidth); // wait a very small amount
digitalWrite(servoPin, LOW); // set servo low
delay(20); // refresh cycle of typical servos (20 ms)
}
void setup() {
pinMode(motorPin,OUTPUT); // declare the motor's pin as output
Serial.begin(19200); // connect to the serial port
Serial.println("Welcome to SerialMotorSpeed!");
Serial.println("Enter speed number 0-9:");
pinMode(servoPin, OUTPUT); // set servoPin pin as output
Serial.println("Servo Serial Simple ready");
}
void loop() {
val = Serial.read(); // read the serial port
// if the stored value is a single-digit number, blink the LED that number
if (val >= '0' && val <= '9' ) {
val = val - '0'; // convert from character to number
val = val * (180/9); // convert from number to degrees
Serial.print("moving servo to ");
Serial.print(val,DEC);
Serial.println();
for( int i=0; i<50; i++ ) {
servoPulse(servoPin, val);
}
}
if (val >= 'A' && val <= 'J' ) {
val = val - 'A'; // convert from character to number
val = 25 * val; // convert from 0-9 to 0-255 (almost)
Serial.print("Setting speed to ");
Serial.println(val);
analogWrite(motorPin,val);
Serial.println("Enter speed number 0-9:");
}
}
Good for you! Combining two codes in general is difficult to do. I suggest you make an attempt and post both the separate codes and your attempt of combination if you have trouble getting the combined code to work.