Problem Programming Servo

Hello. I've tried many many time to correct an issue. Basically I took this off instructables and am building upon it. I want a servo (CONTINUOUS ROTATION*) to move, stop and wait, and then move back. The problem is that the servo moves forward and back in about a second. Why does it do this when I have increased what I thought are all the necessary delays. Can anyone tell me what I need to change in the program to fix this. Again when the password is entered correctly, the servo should spin for about 7 seconds, then spin the opposite direction for seven seconds.

#include <Password.h>
#include <Keypad.h>
#include <Servo.h> //tells to use servo library

Servo myservo; //declares servo
Password password = Password( "8675309" ); //password to unlock box, can be changed

const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{
'1','2','3','A' }
,
{
'4','5','6','B' }
,
{
'7','8','9','C' }
,
{
'*','0','#','D' }
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {
5, 4, 3, 2 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {
8, 7, 6, 9 };

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
delay(7000);
pinMode(11, OUTPUT); //green light
pinMode(12, OUTPUT); //red light
myservo.attach(13); //servo on digital pin 9 //servo
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
int pos = 0

void loop(){
keypad.getKey();
myservo.write(90);
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:

switch (eKey){
case 'A':
checkPassword();
delay(1);
break;

case 'B':
password.reset();
delay(1);
break;

default:
password.append(eKey);
delay(1);
}
}
}
void checkPassword(){
digitalWrite(11,HIGH);
if (password.evaluate()){ //if password is right open box
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(7000); // waits 15ms for the servo to reach the position
}
digitalWrite(11, LOW);
{
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(7000); // waits 15ms for the servo to reach the position
}
digitalWrite(11, LOW);// turn off
myservo.write(90);

}
else{
delay(1);
digitalWrite(12, HIGH); //turn on
delay(5000); //wait 5 seconds
digitalWrite(12, LOW);//turn off

}
}

Basically none of the delays on the bottom of the code seem to work. Both the delay(7000) under the IF condition and the delay(5000) under the ELSE condition seem to be cut short and only last about 1 second.

Hi, can you post your sketch in # code tags please, it makes it easier to read.

Have you tried the sketch with just the servo run part, comment out everything but the servo. Or just write a servo program.

You do not have position control of a Continuous Rotation servo, only direction and speed.
To make it continuous the position control feedback potentiometer has been physically disconnected from the output shaft.

Tom....... :slight_smile:

As has been pointed out you have no control over the position of a continuous rotation servo. It is in fact not a servo at all but a geared motor with electronic speed control.

Bearing that in mind this code

  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(7000);                       // waits 15ms for the servo to reach the position
  }

will not do what the comments say. It will increase the speed of the servo rotating in one direction in 180 discrete steps, each of which will take 7 seconds, which will take a total of 21 minutes.

By the way, the code as posted does not compile.

Hi, auto-format says too many left curly braces for a start.
What arduino controller are you using?

Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom...... :slight_smile: