I'm using an Arduino Due to rotate a servo when a code is entered into a 4x3 matrix keypad. I've tried both a Parallax continuous rotation servo, and a Futaba S3003, but neither work correctly. The continuous rotation simply turns endlessly, and twitches when the code is entered, and the other servo locks in a specific position, and twitches when the code is entered. I'd very much appreciate some assistance! It's almost certainly a software issue, as the servos work properly when run on Arduino's servo test program.
#include <Keypad.h>
#include <Servo.h>
Servo servo_Motor;
char* password = "123";
int position = 0;
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = { 11, 6, 7, 9 };
byte colPins[COLS] = { 10, 12, 8 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int redPin = 12;
int greenPin = 13;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
servo_Motor.attach(13);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
if (key == '*' || key == '#')
{
position = 0;
setLocked(true);
}
if (key == password[position])
{
position ++;
}
if (position == 3)
{
setLocked(false);
}
delay(100);
}
void setLocked(int locked)
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
servo_Motor.write(90);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
servo_Motor.write(90);
}
}