if (Serial.available() > 0) {
// read the incoming byte:
pressButton = moveServo1 = moveServo2 = Serial.read();
No, no, no.
Really, with a side-order of "not on your life".
Read, carefully, what I have written.
if (Serial.available() > 0) {
// read the incoming byte:
pressButton = moveServo1 = moveServo2 = Serial.read();
No, no, no.
Really, with a side-order of "not on your life".
Read, carefully, what I have written.
im confuzed. remember, im a noob. i cant code for my life
OK.
If
pulseWidth1 = pulseWidth2 = 0;
sets both "pulseWidth1" and "pulseWidth2" to zero,
what do you think
pressButton = moveServo1 = moveServo2 = Serial.read();
does?
If "serial.available ()" returns "1", how many times do you think I should call "serial.read ()" ?
Seriously, I could write this for you.
And how much attention would you pay to what I'd written before you asked another question?
I'm not going to write it for you.
I'm really, really trying to help - trust me, if you write it, and understand it, you'll learn.
thanks. that cleared it up a lot( i think.) im still unclear of what pulseWidth is. and am i getting close to what the final code should look like?
im still unclear of what pulseWidth is
"pulseWidth" is what controls the servo. There's a big clue in the name.
The pulse repeats every 20 milliseconds or so, and the "high" portion is the bit we're interested in.
It has a nominal length of "maxPulse - ((maxPulse - minPulse)/2)" microseconds = 2400 - ((2400 - 600) / 2) = 1500 microseconds or 1.5 milliseconds.
That's the centre position. If the pulse is longer than this value, it will turn in one direction, if shorter, it will turn in the opposite direction.
The length of the pulse defines the position of the servo's output shaft.
As I've said, in your code the "#include <servo.h>" is redundant because you're controlling the servo yourself, but if you ditch all your servo-controlling code and read this:
http://arduino.cc/en/Reference/Servo you'll see that there's a much simpler way of doing what you're trying to do.
ok. i got some stuff working by changing the pins to 9 and 10, which i learned that i should do from the page on the servo library. sry i havent responded. i was at the maker faire in cali. it still quicers a lot. but i got a power source at the maker faire fo rthe servo so they are both working better.
how do i do this the simpler way. i read everything in the servo library page, and i found no simpler ways
how do i do this the simpler way. i read everything in the servo library page, and i found no simpler ways
In your code, you're generating the pulses to drive the servo explicitly, refreshing them every 20mS.
If you use the servo libraries, you can leave all of the pulse generation to the library, and simply tell the servo object where you want it to go.
All of this:
if (millis() - lastPulse >= refreshTime1) {
digitalWrite(servo1Pin, HIGH); // start the pulse
delayMicroseconds(pulseWidth1); // pulse width
digitalWrite(servo1Pin, LOW); // stop the pulse
lastPulse = millis(); // save the time of the last pulse
}
can be replaced by a single function call. (plus an attach)
i got some stuff working by changing the pins to 9 and 10, which i learned that i should do from the page on the servo library
Up to this point, you haven't been using the servo libraries - what you shown so far should work on any pin, even the analogue ones!
i understand what your saying, i just dont know how to fix the problem. and your vocabulary is much higher than mine. buffer?
do i change the 0 to something else? what do i change it to
ok, i have found out that my servo library is wrong, and when i replace it with the one from the arduino website, its still broken. refresh does not seem to be part of it and wont let me call it because its not there. can someone send it to me? send me a message and ill tell u my email so that u can send it to me. the one on the arduino websites broken, or im just putting it in the wrong place. i have a mac, by the way.
The Arduino library does not need refresh, you can remove it from your sketch. The library does the refresh in hardware so all you need to do is write a new value to the servo when you receive a serial command. This should significantly simplify your code.
Here is a version of your sketch that should work with the Arduino library, although you will probably need to tweak it to get it to do exactly what you want. The Arduino Servo reference can help with the commands. Note that this library uses an angle from 0 to 180 instead of pulse width in microseconds.
#include <Servo.h>
int minPulse1 = 0; // minimum servo position
int maxPulse1 = 180; // maximum servo position
int turnRate1 = 10; // servo turn rate increment (larger value, faster rate)
int minPulse2 = 0; // minimum servo position
int maxPulse2 = 180; // maximum servo position
int turnRate2 = 10; // servo turn rate increment (larger value, faster rate)
int buttonPin = 13; // pin that the trigger will be connected to
/** The Arduino will calculate these values for you **/
int centerServo1;
int centerServo2;
int pulseWidth1; // servo pulse width
int pulseWidth2; // servo pulse width
Servo servo1;
Servo servo2;
void setup() {
pinMode(buttonPin, OUTPUT);
servo1.attach(9);
servo2.attach(10);
centerServo1 = maxPulse1 - ((maxPulse1 - minPulse1)/2);
centerServo2 = maxPulse2 - ((maxPulse2 - minPulse2)/2);
pulseWidth1 = centerServo1;
pulseWidth2 = centerServo2;
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial.println(" Arduino Serial Servo Control");
Serial.println("Press a, s, d, or w to move, spacebar to center, and f to fire");
Serial.println();
}
void loop() {
// check for serial input
if (Serial.available() > 0) {
int data = Serial.read(); // read the incoming byte:
digitalWrite(buttonPin, LOW); // turn the pin off on any incoming data
switch(data)
{
case 'a' : pulseWidth1 = pulseWidth1 - turnRate1; break;
case 'b' : pulseWidth1 = pulseWidth1 + turnRate1; break ;
case ' ' : pulseWidth1 = pulseWidth2 = centerServo1; break;
case 's' : pulseWidth2 = pulseWidth2 - turnRate1; break;
case 'w' : pulseWidth2 = pulseWidth2 + turnRate1; break ;
case 'f' : digitalWrite(buttonPin, HIGH); break;
}
// stop servo pulse at min and max
if (pulseWidth1 > maxPulse1) { pulseWidth1 = maxPulse1; }
if (pulseWidth1 < minPulse1) { pulseWidth1 = minPulse1; }
// stop servo pulse at min and max
if (pulseWidth2 > maxPulse2) { pulseWidth2 = maxPulse2; }
if (pulseWidth2 < minPulse2) { pulseWidth2 = minPulse2; }
servo1.write(pulseWidth1);
servo2.write(pulseWidth2);
// print pulseWidth back to the Serial Monitor (uncomment to debug)
Serial.print("Servo 1: ");
Serial.print(pulseWidth1);
Serial.print(" Servo 2: ");
Serial.print(pulseWidth2);
Serial.println("degrees");
}
}
THANK YOU SOOOO MUCH
it all works now, i made a couple of changes to the code tho. i had it so that the buttonPin would only stay on for a second, so that it would register. if not, then it would stay on until i moved one of the servos, but its great! thanks mem for the code fixes, and thanks AWOL for showin me what i should be changing. iz learned a lot frum u.
btw, heres my final code. now i just gots to build everythin.
#include <Servo.h>
int minPulse1 = 0; // minimum servo position
int maxPulse1 = 180; // maximum servo position
int turnRate1 = 10; // servo turn rate increment (larger value, faster rate)
int minPulse2 = 0; // minimum servo position
int maxPulse2 = 180; // maximum servo position
int turnRate2 = 10; // servo turn rate increment (larger value, faster rate)
int buttonPin = 13; // pin that the trigger will be connected to
/** The Arduino will calculate these values for you **/
int centerServo1;
int centerServo2;
int pulseWidth1; // servo pulse width
int pulseWidth2; // servo pulse width
Servo servo1;
Servo servo2;
void setup() {
pinMode(buttonPin, OUTPUT);
servo1.attach(9);
servo2.attach(10);
centerServo1 = maxPulse1 - ((maxPulse1 - minPulse1)/2);
centerServo2 = maxPulse2 - ((maxPulse2 - minPulse2)/2);
pulseWidth1 = centerServo1;
pulseWidth2 = centerServo2;
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial.println(" Arduino Serial Servo Control");
Serial.println("Press a, s, d, or w to move, spacebar to center, and f to fire");
Serial.println();
}
void loop() {
// check for serial input
if (Serial.available() > 0) {
int data = Serial.read(); // read the incoming byte:
digitalWrite(buttonPin, LOW); // turn the pin off on any incoming data
switch(data)
{
case 'a' : pulseWidth1 = pulseWidth1 - turnRate1; break;
case 'd' : pulseWidth1 = pulseWidth1 + turnRate1; break ;
case ' ' : pulseWidth1 = pulseWidth2 = centerServo1; break;
case 's' : pulseWidth2 = pulseWidth2 - turnRate1; break;
case 'w' : pulseWidth2 = pulseWidth2 + turnRate1; break ;
case 'f' : digitalWrite(buttonPin, HIGH); delay (1000); digitalWrite(buttonPin, LOW); break;
}
// stop servo pulse at min and max
if (pulseWidth1 > maxPulse1) { pulseWidth1 = maxPulse1; }
if (pulseWidth1 < minPulse1) { pulseWidth1 = minPulse1; }
// stop servo pulse at min and max
if (pulseWidth2 > maxPulse2) { pulseWidth2 = maxPulse2; }
if (pulseWidth2 < minPulse2) { pulseWidth2 = minPulse2; }
servo1.write(pulseWidth1);
servo2.write(pulseWidth2);
// print pulseWidth back to the Serial Monitor (uncomment to debug)
Serial.print("Servo 1: ");
Serial.print(pulseWidth1);
Serial.print(" Servo 2: ");
Serial.print(pulseWidth2);
Serial.println("degrees");
}
}
yay! it all works, and then i found out that the nerf turret i was puttin on top of it was too heavy for the servos, and it didnt work. so now im gonna put an airsoft gun on top of it. yay! auto turret!
Hi all, just found this thread and an ideia came to my mind about implementing a device that could read the 8ch PPM frame from a futaba Tx and convert it to serial PPM (8N,1) @19.2kbps and so it could be packetized to be sent to the receiver wich could use this code modified for 8ch !
Do you guys have any ideia how this could be done ? :
thanks in advance
Not sure what you mean by "serial PPM", but maybe you should look at "pulseIn".
You've got an average of 4.8 characters per channel @ 50Hz.
Choose your coding scheme carefully.
Is it possible to control two servos through serial by JUST PRESSING LETTER, not LETTER + ENTER (im talking about Arduino 0018 software serial monitor). If i want to move servos i must press all the time letter (A,D,W or S) and Enter - key.
@AOWL , i figured out how to do it ! thanks