Pages: [1]   Go Down
Author Topic: arduino servo  (Read 815 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this may sound stupid but im making an arduino keypad lock and im having trouble programming the servo. can somebody tell me if there is a way to program the servo so that it will turn 180 degrees, wait around 5 seconds and then go back 180 degrees
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49073
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can somebody tell me if there is a way to program the servo so that it will turn 180 degrees, wait around 5 seconds and then go back 180 degrees
Yes, there is. Two ways, actually, depending on whether you want to use or avoid the dreaded delay() function.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

at the moment im trying the servo sweep code,but sadly the servo has no delay between going right 180 degrees and back 180 degrees ive tried putting delays in throughout the code and that didn't work.
Logged

Espoo, Finland
Offline Offline
God Member
*****
Karma: 7
Posts: 586
"Oops, try again..."
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Show us the code, show us the code!!!! And all together... "SHOW US..."


Cheers,
Kari
Logged


The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

Sydney, AUS
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Like PaulS said, you can use delay(); to pause between sweeps.

Personally, unless it's critical, I'd just be using a few basic lines of code (plus setup) - simple and I know it will work.

Code:
#include <servo.h>
Servo myservo;

Then in void loop:

Code:
myservo.write(180);
delay(5000);
myservo.write(0);

Should work no problems..  Obviously you'd need to tell the servo to rotate to 0 degrees somewhere during setup..
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

heres the code. basically its a keypad controlling a servo, a speaker and a couple leds

* arduino_doorlock.pde (4.95 KB - downloaded 8 times.)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please, insert code using INSERT CODE #.

Simas
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <Keypad.h>
#include <Password.h>
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
 

Password password = Password( "3184" );

const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

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

byte ledPin = 13;
byte ledPin2 = 12;
int pos = 0;    // variable to store the servo position

boolean programMode = false;

int speakerPin = 9;


void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(ledPin2, OUTPUT); // sets the digital pin as output
pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output]
digitalWrite(ledPin, LOW); // sets the LED off
digitalWrite(ledPin2, LOW); // sets the LED off
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
startupSound();
}

void loop(){
char key = keypad.getKey();


}


void keypadEvent(KeypadEvent key){
switch (keypad.getState()){
case PRESSED:
switch (key){
case '#': guessPassword();
digitalWrite(ledPin, LOW);
break;
case '*': password.reset();
resetSound();
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
break;
default : if (key != NO_KEY){
Serial.print("Key Pressed = ");
Serial.println(key);
}
password.append(key);
digitalWrite(ledPin, HIGH);
analogWrite(speakerPin, 128);
break;
}
break;
case RELEASED:
switch (key){
default : digitalWrite(ledPin, LOW);
analogWrite(speakerPin, 0);
break;
}
break;
}
}
void guessPassword(){
if (password.evaluate()){
digitalWrite(ledPin2,HIGH);
Serial.println("Password ");
//Serial.println(key);
Serial.println(" Correct");
openSound();
delay(200);

 digitalWrite(ledPin2,LOW);
myservo.attach(10);
   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(15);                       // waits 15ms for the servo to reach the position
  }
  myservo.detach();
  delay(5000);
  myservo.attach(10);
  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(15);                       // waits 15ms for the servo to reach the position
  }
  myservo.detach();
    delay(5000);
    myservo.attach(10);
     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(15);                       // waits 15ms for the servo to reach the position
  }

myservo.detach();
password.reset();
}else{                                        //servo code insert above
digitalWrite(ledPin2,LOW);
Serial.println("Password ");
//Serial.println(key);
Serial.println(" Incorrect!");
password.reset();
errorSound();                           
}
}
void resetSound() {
int n = 100;
for (int vez=0;vez<6;vez++){
for (int _=0;_<100;_++){
delayMicroseconds(n);
digitalWrite(speakerPin, HIGH);
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, LOW);
delayMicroseconds(n);
digitalWrite(speakerPin, LOW);
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, HIGH);
}
delay(20);
//myservo.detach();
}
}
void errorSound() {
int n = 2000;
for (int vez=0;vez<1;vez++){
for (int _=0;_<200;_++){
delayMicroseconds(n);
digitalWrite(speakerPin, HIGH);
digitalWrite(ledPin, HIGH);
delayMicroseconds(n);
digitalWrite(speakerPin, LOW);
digitalWrite(ledPin, LOW);
}
delay(50);

}
}
void openSound() {
int n = 300;
for (int vez=0;vez<3;vez++){
for (int _=0;_<100;_++){
delayMicroseconds(n);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(n);
digitalWrite(speakerPin, LOW);
}
delay(100);
}
}
void startupSound() {
int note = (1/587.33)*500000;
for (int _=0;_<100000/note;_++){
delayMicroseconds(note);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(note);
digitalWrite(speakerPin, LOW);
}
note = (1/659.26)*500000;
for (int _=0;_<100000/note;_++){
delayMicroseconds(note);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(note);
digitalWrite(speakerPin, LOW);
}
note = (1/523.25)*500000;
for (int _=0;_<100000/note;_++){
delayMicroseconds(note);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(note);
digitalWrite(speakerPin, LOW);
}
note = (1/261.63)*500000;
for (int _=0;_<100000/note;_++){
delayMicroseconds(note);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(note);
digitalWrite(speakerPin, LOW);
}
note = (1/392.00)*500000;
for (int _=0;_<200000/note;_++){
delayMicroseconds(note);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(note);
digitalWrite(speakerPin, LOW);

}
}/code]
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49073
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
at the moment im trying the servo sweep code,but sadly the servo has no delay between going right 180 degrees and back 180 degrees ive tried putting delays in throughout the code and that didn't work.
The code you posted is not doing just that. When developing an application, you should develop a simple sketch that properly implements some behavior, like moving the servo. It is a waste of time and effort to build this capability into a large sketch if the basic functionality doesn't work.

So, show us the simple sketch and explain what happens when you run it.

Why is the servo not being attached in setup()?

What is this doing?
Code:
void loop(){
char key = keypad.getKey();
}
On every pass through loop, see is a key has been pressed. Store the pressed key in a local variable, and then forget about it. Well, OK, but why?

Code:
   for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
There is no reason on earth for pos to be a global variable. None.

Code:
  myservo.detach();
Why? There is no reason to detach the servo.

When the correct password is entered, you are moving the servo from 0 to 180, back to 0, and back to 180. That doesn't make sense. When that function ends, the servo should be back where it started.

Code:
void startupSound() {
int note = (1/587.33)*500000;
for (int _=0;_<100000/note;_++){
1 is an integer. Dividing 1 by 587.33 will result in 0. 500000 is not an int, so who knows what 0 will be multiplied by.

You have a variable named _? WTF? Have most of your keys fallen off?

When _ is incremented beyond 32767, it goes negative. That will certainly be less that the non-int value 100000.

You really need to match variable types (and constants) correctly to the value that they are to contain.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks everyone for you help i got the code working with the myservo.write code
Logged

Pages: [1]   Go Up
Jump to: