Hey guys. So i have an Arduino controlling a dc motor through a motor driver, and that alone works fine. However, I am trying to have this one Arduino manipulate BOTH the motor driver and a servo.
Started by writing code/wiring up motor driver.. all works well
Then started adding code for servo, wired it up.. motor driver doesnt work anymore (no code/wiring changed for the motor driver)
After a bit of troubleshooting I found out that it is the servo.attach() function that messes things up. I comment out that one line and the motor driver is back up and running. Put it back in, motor driver no longer works.
Using my voltmeter I found out that with .attach() both output pins go high instead of one low one high.
Anyone have any ideas on why the .attach() function is messing me up? And hopefully any ideas to fix it! ![]()
If you want more info, let me know!
Here's my code:
//include libraries
#include <Servo.h>
//create servo object
Servo servo;
//initiate variables
int potVal = 0;
int pwm = 0;
int servoPin = 10;
int left = 0;
int right = 0;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(9, OUTPUT);
//servo.attach(servoPin); **This messes up motor driver**
}
void loop() {
//**begin motor control**////
potVal = analogRead(A0);
potVal = map(potVal, 0, 700, 0, 100);
if(potVal < 40){
//12 HIGH = OUTA HIGH
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
pwm = map(potVal, 0, 39, 200, 0);
analogWrite(9, pwm);
}
else if(potVal > 50){
//13 HIGH = OUTB HIGH
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
pwm = map(potVal, 50, 100, 0, 200);
analogWrite(9, pwm);
}
else {
digitalWrite(12, LOW);
digitalWrite(13, LOW);
analogWrite(9, 0);
}
Serial.print(potVal);
delay(500);
//**end motor control**//
//**begin servo control**//
left = analogRead(A1);
right = analogRead(A2);
//**end servo control**//
}