Hi everyone, I am doing a project on an RC car with a claw to pick objects.
I used Bluetooth to send command to the Arduino and control the various hardware components.
I can control the car to move but I cannot rotate the servo of the claw.
When I try to rotate the servo, the servo only rotates a little and the led on pin 13 of the Arduino keeps blinking.
I have no idea of this problem, can anyone help me?
#include <Servo.h>
Servo myServo;
const int controlPin1 = 4;
const int controlPin2 = 5;
const int controlPin3 = 2;
const int controlPin4 = 3;
const int enablePin1 = 6;
const int enablePin2 = 11;
int motorEnabled1 = 0; // Turns the motor on/off
int motorEnabled2 = 0; // Turns the motor on/off
int motorSpeed = 110; // speed of the motor
int motorDirection1 = 0; // current direction of the motor
int motorDirection2 = 0; // current direction of the motor
String inputString = "";
void setup() {
// intialize the inputs and outputs
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin1, OUTPUT);
pinMode(controlPin3, OUTPUT);
pinMode(controlPin4, OUTPUT);
pinMode(enablePin2, OUTPUT);
// pull the enable pin LOW to start
digitalWrite(enablePin1, LOW);
digitalWrite(enablePin2, LOW);
myServo.attach(9);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == 'a') {
motorEnabled1 = 1;
motorEnabled2 = 1;
motorDirection1 = 0;
motorDirection2 = 0;
} else if (inChar == 'b') {
motorEnabled1 = 1;
motorEnabled2 = 1;
motorDirection1 = 1;
motorDirection2 = 1;
} else if (inChar == 'c') {
myServo.write(0);
delay(15);
} else if (inChar == 'd') {
myServo.write(180);
delay(15);
} else if (inChar == 'e') {
motorEnabled1 = 1;
motorEnabled2 = 1;
motorDirection1 = 0;
motorDirection2 = 1;
} else if (inChar == 'f') {
motorEnabled1 = 1;
motorEnabled2 = 1;
motorDirection1 = 1;
motorDirection2 = 0;
} else if (inChar == 'g') {
motorEnabled1 = 0;
motorEnabled2 = 0;
} else if (inChar == 'h') {
Serial.println(motorSpeed);
} else if (inChar == 'i') {
motorSpeed = inputString.toInt();
inputString = "";
} else {
inputString += inChar;
}
}
if (motorDirection1 == 1) {
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
}
else {
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, HIGH);
}
if (motorDirection2 == 1) {
digitalWrite(controlPin3, HIGH);
digitalWrite(controlPin4, LOW);
}
else {
digitalWrite(controlPin3, LOW);
digitalWrite(controlPin4, HIGH);
}
if (motorEnabled1 == 1) {
analogWrite(enablePin1, motorSpeed);
}
else {
analogWrite(enablePin1, 0);
}
if (motorEnabled2 == 1) {
analogWrite(enablePin2, motorSpeed);
}
else {
analogWrite(enablePin2, 0);
}
}