Yes, I think the code:
reading =analogRead(pot_pin);
analogWrite(DC, reading / 4);
is simpler for arduino. 1023/4 is faster to write than:
int duty,pot_pin=0, reading;//control power to DC motor
reading =analogRead(pot_pin); // read potentiometer
duty = map(reading,0,1023,0,255); // rescale to 8-bit
duty = constrain(duty,0,255); // be safe
analogWrite(DC,duty); // set duty cycle
I'm not sure if that is what fixed it though. I just changed the DC motor pin to pin 11 and now the Servo and DC motor run at the same time. The only issue that I have is I have to turn the DC motor shaft manually to get it started? I'm not sure why. I wrote the code your way and my way and I have turn the shaft manually for both code blocks. Do you maybe know why? Is that normal?
My Code:
int DC= 11; // DC motor attached to pin 11
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 11; // Analog output pin that the DC motor is attached to is attached to
int sensorValue = 0;// value read from the pot
int outputValue = 0; //value read from pmw 11
#include <Servo.h> // Make code inServo.havailable to this sketch
Servo myservo; // Create servo object called "myservo"
int servo_pin=9; // The servo must be attached to pin 9 or pin 10
void setup()
{
Serial.begin(9600);//turn on serial monitor
pinMode(DC,OUTPUT );//output power to DC motor pin 11
myservo.attach(servo_pin);//output power to servo pin 9
}
void loop()
{
//Code block for DC Motor
int duty,pot_pin=0, reading;//control power to DC motor
reading =analogRead(pot_pin); // read potentiometer
duty = map(reading,0,1023,0,255); // rescale to 8-bit
duty = constrain(duty,0,255); // be safe
analogWrite(DC,duty); // set duty cycle
//Code for reading input and output for pin A0 and 11 in Serial Monitor
sensorValue = analogRead(pot_pin); //
outputValue = map(sensorValue, 0, 1023, 0, 255);
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
//Code block for Servo Motor
int pos= 0; // variable to store the servo position
int dtwait=15; // duration of wait at the end of each step
for(pos= 0;pos< 180;pos+= 1) {
myservo.write(pos); // Move to position in variable 'pos'
delay(dtwait); // wait dtwait for the servo to reach the position
}
for(pos= 180;pos>=1;pos-= 1){
myservo.write(pos); // Move to position in variable 'pos'
delay(dtwait); // waitdtwait for the servo to reach the position
}
}
Your Code:
int DC= 11; // DC motor attached to pin 11
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 11; // Analog output pin that the DC motor is attached to is attached to
int sensorValue = 0;// value read from the pot
int outputValue = 0; //value read from pmw 11
#include <Servo.h> // Make code inServo.havailable to this sketch
Servo myservo; // Create servo object called "myservo"
int servo_pin=9; // The servo must be attached to pin 9 or pin 10
void setup()
{
Serial.begin(9600);//turn on serial monitor
pinMode(DC,OUTPUT );//output power to DC motor pin 11
myservo.attach(servo_pin);//output power to servo pin 9
}
void loop()
{
//Code block for DC Motor
int pot_pin=0, reading;//control power to DC motor
reading =analogRead(pot_pin);
analogWrite(DC, reading / 4);
//Code for reading input and output for pin A0 and 11 in Serial Monitor
sensorValue = analogRead(pot_pin); //
outputValue = map(sensorValue, 0, 1023, 0, 255);
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
//Code block for Servo Motor
int pos= 0; // variable to store the servo position
int dtwait=15; // duration of wait at the end of each step
for(pos= 0;pos< 180;pos+= 1) {
myservo.write(pos); // Move to position in variable 'pos'
delay(dtwait); // wait dtwait for the servo to reach the position
}
for(pos= 180;pos>=1;pos-= 1){
myservo.write(pos); // Move to position in variable 'pos'
delay(dtwait); // waitdtwait for the servo to reach the position
}
}