Hi, I have a problem. I am trying to connect a servo to an arduino with Motor Shield V1.0
I tried both analog and digital pins, but calling Servo::attach method for any servo makes motor shield unworking.
Leds still work, but motors woun’t move.
Here is the code.
Am I wrong or is it Motorr Sheld problem?
[code]#include <Servo.h>
#define DEBUG_ENABLED 1
/////////////////////
//MOTOR CONTROLS/////
const int pinI1=8;
const int pinI2=11;
const int speedpinA=9;
const int pinI3=12;
const int pinI4=13;
const int speedpinB=10;
const int spead =127;
/////////////////////
Servo neck;
Servo grabber;
char sensorPos = 0;
char command = ‘H’;
int motorState = ‘H’;
int systemState = 0;
const int pingPin = 4;
boolean grabbed;
//////////////////////
////////BODY//////////
//////////////////////
void setup()
{
//neck.attach(3);
grabber.attach(A4);
pinMode(pinI1,OUTPUT);
pinMode(pinI2,OUTPUT);
pinMode(speedpinA,OUTPUT);
pinMode(pinI3,OUTPUT);
pinMode(pinI4,OUTPUT);
pinMode(speedpinB,OUTPUT);
Serial.begin(9600);
//pinMode(bt_RX, INPUT);
//pinMode(bt_TX, OUTPUT);
setupBlueToothConnection()
grab(false);
grabbed = false;
}
void loop()
{
if(Serial1.available())
{
command = Serial1.read();
switch(command)
{
case ‘F’:
motorState = ‘L’;
break;
case 'B':
motorState = 'R';
break;
case 'L':
motorState = 'F';
break;
case 'R':
motorState = 'B';
break;
case 'H':
motorState = 'H';
break;
/*
case 'A':
Serial1.println("Autopilot Mode!");
systemState = 1;
break;
case 'a':
Serial1.println("Manual Operation!");
systemState = 0;
motorState = 'H';
break;
*/
case 'S':
Serial1.println("Scanning...");
//systemState = 2;
break;
case 's':
Serial1.println("Scanner Stopped.");
//systemState = 0;
break;
case 'G':
grabbed = !grabbed;
grab(grabbed);
Serial1.print("Grab");
break;
case '+':
delay(2000);
Serial1.print("Connected!");
Serial1.flush();
default:
break;
}
}
switch (systemState)
{
case 0:
switch (motorState)
{
case ‘F’:
forward();
break;
case 'B':
backward();
break;
case 'L':
left();
break;
case 'R':
right();
break;
case 'H':
halt();
break;
}
break;
case 1:
//autoAvoid();
break;
case 2:
for(sensorPos = 0; sensorPos < 180; sensorPos++)
{
neck.write(sensorPos);
long s = readRange();
s = (s/180) * 100;
String message = "[" + (String)sensorPos + " " + (String)s + "]";
Serial1.println(message);
}
break;
}
//Serial1.flush();
}
[/code]
void forward()
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
digitalWrite(pinI1,HIGH);
}
void backward()//
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
}
void left()//
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
}
void right()//
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
digitalWrite(pinI1,HIGH);
}
void halt()//
{
digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
digitalWrite(speedpinB,LOW);
}
void grab(boolean g)
{
if(g)
{
grabber.write(90);
}
else
{
grabber.write(1);
}
}