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
}
}