Attempting to make my first real project and animatronic build. Eyes.
I have the servos hooked up with their own power supply of 9 volts and the signal pins to 8/9 and the program i am using is:
//======================================
//Circle Math
//======================================
#include <math.h>
#define pi 3.14159265358979323846
#define twopi (2*pi)
float circleradius = 50; //50 each side - make no more any of your max limit values
float stepnumber = 360;
float stepangle;
//======================================
#include <Servo.h> //include servo library for servo control
Servo horServo; //servo for left/right movement
Servo vertServo; //servo for up/down movement
byte randomhor; //define random horizontal position variable
byte randomvert; //define random vertical position variable
int randomdelay; //define random delay variable
#define HLEFTLIMIT 0 //define left limit on horizontal (left/right) servo
#define HRIGHTLIMIT 180 //define right limit on horizontal (left/right) servo
#define VTOPLIMIT 60 //define top limit on vertical (up/down) servo
#define VBOTLIMIT 180 //define bottom limit on horizontal (up/down) servo
void setup()
{
horServo.attach(4); //horizontal servo on pin 8
vertServo.attach(5); //vertical servo on pin 9
randomSeed(analogRead(0)); //Create some random values using an unconnected analog pin
//=====================================================
//Roll Eyes :D
//=====================================================
stepangle = twopi/stepnumber;
for(int i = 0; i<stepnumber; i++){
float angle = i*stepangle;
float x = sin(angle)*circleradius;
float y = cos(angle)*circleradius;
x = map(x, 1-circleradius, circleradius, 0, 2*circleradius);
y = map(y, 1-circleradius, circleradius, 0, 2*circleradius);
horServo.write(x); //write to the horizontal servo
vertServo.write(y); //write to the horizontal servo
delay(10);
}
//=====================================================
}
void loop()
{
randomhor = random(HLEFTLIMIT, HRIGHTLIMIT); //set limits
randomvert = random(VTOPLIMIT, VBOTLIMIT); //set limits
randomdelay = random(1000, 4000); //moves every 1 to 4 seconds
horServo.write(randomhor); //write to the horizontal servo
vertServo.write(randomvert); //write to the vertical servo
delay(randomdelay); //delay a random amount of time (within values set above)
}
All that happens is the servos turn to the right and don't move... and make a grrrrr noise so before I throw something what am I doing wrong?
#define pi 3.14159265358979323846
that many digits is kind of pointless. Arduino/avr-gcc only implements float which only has 6 or 7 digits of precision.
#define twopi (2pi)
floating point is slow, don't make the processor calculate this constant every time you want to use it. put "23.141592" into your calculator and then make a macro with that number.
"All that happens is the servos turn to the right and don't move..."
So they move to the right or they don't move? I don't understand how they can move and don't move.
Q: "All that happens is the servos turn to the right and don't move..."
So they move to the right or they don't move? I don't understand how they can move and don't move.
A: When looking from the top down at a servo the servo must have been in the middle and when i applied power it moved to the right completely then wouldn't move from that spot. (hope that answers that)
I thought about that last night! It hit me that the servo requires only 6v's! Could adding 9v be causing the above indication? Essentially overpowering the servo?
Also on the common ground issue. When I hooked the 9v wall wart with a plain black wire and a black wire with white stripe the $800.00 Fluke didn't see a difference in + or -!
Last Q I have is: Does the Arduino use a Java based programming language? I took Java last semester (didn't quite get it).
steinklatre:
horServo.attach(4); //horizontal servo on pin 8
vertServo.attach(5); //vertical servo on pin 9
It is generally a good idea for your comments to match what the code is actually doing. You say you are using pins 8 and 9 in the comments, but your code does not.
steinklatre:
I thought about that last night! It hit me that the servo requires only 6v's! Could adding 9v be causing the above indication? Essentially overpowering the servo?
Not "over power" but over voltage which can damage the servo.
steinklatre:
Also on the common ground issue. When I hooked the 9v wall wart with a plain black wire and a black wire with white stripe the $800.00 Fluke didn't see a difference in + or -!
Sounds like a mistake in measurement.
steinklatre:
Last Q I have is: Does the Arduino use a Java based programming language?
On the comments I changed them to reflect 8/9 I had posted the "original" code. Sorry about that. I verified the positive and negative of the wall wart and will look to see if I have a battery pack to use instead. How much will the non-same ground affect the unit? IE: I tried this with a 4.5v wall wart to the servos and the usb to the arduino to computer.
The servo's did nothing at all this time. After work this evening I will post up a picture of how I set it up.
Thanks at James for having some patience with this rookie!
The gerrr on the servos means (at least from playing with my own servos!) your pulse is to long/short.
At least for my servos a pulse of about 1500 micros should center the servo, a pulse of about 1000 will send as far as possible on way and a pulse about 2000 will send it all the to the other exstream. The exact numbers seam to depend on the servo. There is no standard.
It would help if you posted/reposted your current code with a discription of your current problems.
Ok so it must be something with my breadboard or hookups with the stupid wall wart. I hooked the servo's into the Arduino directly and IT WORKED!!!!!!!!!!!!!!!
Now to tweak it and transfer the results to some "subject of hacking" aka a mask.
Little video.
Current code:
//======================================
//Circle Math
//======================================
#include <math.h>
#define pi 3.14159265358979323846
#define twopi (2*pi)
float circleradius = 50; //50 each side - make no more any of your max limit values
float stepnumber = 360;
float stepangle;
//======================================
#include <Servo.h> //include servo library for servo control
Servo horServo; //servo for left/right movement
Servo vertServo; //servo for up/down movement
byte randomhor; //define random horizontal position variable
byte randomvert; //define random vertical position variable
int randomdelay; //define random delay variable
#define HLEFTLIMIT 30 //define left limit on horizontal (left/right) servo
#define HRIGHTLIMIT 90 //define right limit on horizontal (left/right) servo
#define VTOPLIMIT 60//define top limit on vertical (up/down) servo
#define VBOTLIMIT 120 //define bottom limit on horizontal (up/down) servo
void setup()
{
horServo.attach(8); //horizontal servo on pin 8
vertServo.attach(9); //vertical servo on pin 9
randomSeed(analogRead(0)); //Create some random values using an unconnected analog pin
//=====================================================
//Roll Eyes :D
//=====================================================
stepangle = twopi/stepnumber;
for(int i = 0; i<stepnumber; i++){
float angle = i*stepangle;
float x = sin(angle)*circleradius;
float y = cos(angle)*circleradius;
x = map(x, 1-circleradius, circleradius, 0, 2*circleradius);
y = map(y, 1-circleradius, circleradius, 0, 2*circleradius);
horServo.write(x); //write to the horizontal servo
vertServo.write(y); //write to the horizontal servo
delay(10);
}
//=====================================================
}
void loop()
{
randomhor = random(HLEFTLIMIT, HRIGHTLIMIT); //set limits
randomvert = random(VTOPLIMIT, VBOTLIMIT); //set limits
randomdelay = random(1000, 4000); //moves every 1 to 4 seconds
horServo.write(randomhor); //write to the horizontal servo
vertServo.write(randomvert); //write to the vertical servo
delay(randomdelay); //delay a random amount of time (within values set above)
}