I am new to servos and need some help interfacing an arduino to one...
I have a hitec hs-322hd of which I have not been able to find much useful information to program this device online, so bear with me.
I am building a simple animatronic chicken and I want the wings to flap up and down. I have the framework all figured out but need to know how to program the boarduino to cycle the servo from one extreme to the other 5 times.
I hooked up the servo to the boarduino and found that I can cycle it from one end to the other when a button is pressed but when I change from one extreme to the other, the servo only moves a small amount so I set this up in a loop to repeat the write a number of times to get to the other end of the servo travel... Also, I found that the two values that move the servo from one end to the other are 0 and 180...
Here is my code...
#include <Servo.h> #include <Wire.h> #include <string.h> #include <stdio.h> #define Button 13 // action button is connected to d13
servo2.attach(15); //analog pin 1
pinMode(Button, INPUT); // sets the digital pin 13 as input
}
void loop() {
static int v = 0;
int j, k;
int Abtn = 0; // value read from Adam button
int run =1; // value to keep loop running
int RS=0; // value read from runstop switch
// servo1.write(v);
while (run == 1){
Abtn = digitalRead(Button); // get the current status of the button
if (Abtn == HIGH) {
for (k = 0; k < 5; k++) {
if (v == 180)
{
v = 0;
}
else
{
v = 180;
}
for (j = 1; j < 13; j++) {
servo1.write(v);
delay (80);
Servo::refresh();
}
}
}
}
You don't say which library you are using but I am guessing it's the software servo library because.
This library needs to have the refresh method called at least every 50 ms and 20ms will give best performance.
Below is some code to give you an idea of how you can cycle the servos. Note that servos take time to move. Increasing the value of the flapRato variable will increase the time it takes to flap. Reducing this value much below that in the example may try to drive the servo faster than it can move.
#include <Servo.h>
#define Button 12 // action button is connected to d12
Servo servo1; Servo servo2;
void setup() {
servo1.attach(14); //analog pin 0
servo1.setMaximumPulse(2000);
servo1.setMinimumPulse(700);
servo2.attach(15); //analog pin 1
pinMode(Button, INPUT); // sets the digital pin 12 as input
}
void flap(int nbrOfTimes, int flapRate){
// routine cycles the servos back and forth
while( nbrOfTimes--){
for(int i=0; i < 180; i+= flapRate){
servo1.write(i);
servo2.write(i);
servo::refresh();
delay(20);
}
for(int i=180; i > 0; i -= flapRate){
servo1.write(i);
servo2.write(i);
servo::refresh();
delay(20);
}
}
}
void loop() {
if (digitalRead(Button) == HIGH) {
flap(5,15); // flap 5 times at a fast rate
}
}
BTW, you had the button attached to pin 13 – this pin has an led attached on the standard arduino boards so I changed it to 12.
Ok, I got a chance to try this and it will not compile clean due to an error stating that "servo is not declared..."
I am using the same servo.h file as before and it is choking on the "servo::refresh();" line...
Here is the error that I received:
In function 'void flap(int, int)':
error: 'servo' has not been declared.
I added a few items to the code but other than that it is pretty much as you sent it.
Also, the servo.h file is stored in the folder c:\aduino-0010\hardware\libraries\servo as it was with my original bad code...
Here is the code:
#include <Servo.h>
#include <Wire.h>
#include <string.h>
#include <stdio.h>
#define Button 12 // action button is connected to d12
#define yada 10 // yadayadayada is connected to d10
Servo servo1;
Servo servo2;
void setup() {
servo1.attach(14); //analog pin 0
servo1.setMaximumPulse(2000);
servo1.setMinimumPulse(700);
servo2.attach(15); //analog pin 1
pinMode(Button, INPUT); // sets the digital pin 12 as input
pinMode(yada, OUTPUT); // sets the digital pin 10 as output
digitalWrite(yada, HIGH); // turns the yadayadayada off
} void flap(int nbrOfTimes, int flapRate){
// routine cycles the servos back and forth
while( nbrOfTimes--){
for(int i=0; i < 180; i+= flapRate){
servo1.write(i);
servo2.write(i);
servo::refresh();
delay(20);
}
for(int i=180; i > 0; i -= flapRate){
servo1.write(i);
servo2.write(i);
servo::refresh();
delay(20);
}
}
}void loop() {
if (digitalRead(Button) == HIGH) {
digitalWrite(yada, LOW); // turns the yadayadayada on
delay(100);
digitalWrite(yada, HIGH); // turns the yadayadayada off
flap(5,15); // flap 5 times at a fast rate
}
}
Case sensitivity is a fundamental part of the underlying C and C++ language that Arduino is built on. Its worth getting used to and becomes second nature after a while.