Go Down

Topic: Driving a servo using brushed ESC (Read 2880 times) previous topic - next topic

feliksayk

Thanks for confirming, this is what I am now using, it's awesome.

Code: [Select]
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position

void setup()
{
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop ()
{
 myservo.write (5);   // move servo 30 degrees
 delay (75);     // wait 9 seconds
 myservo.write (10);   // move servo 30 degrees
 delay (75);     // wait 9 seconds
 myservo.write (15);   // move servo 30 degrees
 delay (75);     // wait 9 seconds
 myservo.write (20);   // move servo 30 degrees
 delay (1000 * 9);     // wait 9 seconds
 myservo.write (15);    //moves servo back 30 degrees
 delay (75);     // wait 9 seconds.
 myservo.write (10);    //moves servo back 30 degrees
 delay (75);     // wait 9 seconds.
 myservo.write (5);    //moves servo back 30 degrees
 delay (75);     // wait 9 seconds.
 myservo.write (0);    //moves servo back 30 degrees
 delay (1000 * 9);     // wait 9 seconds.
}

PaulS

Now it's time to learn about for loops, and proper commenting.

feliksayk

Thanks, I just changed the comments in my code ;D . Looping, I guess I'll have to learn it as well, expect more questions if I get another funny idea for arduino  ;)

feliksayk

#18
Dec 07, 2009, 01:36 pm Last Edit: Dec 07, 2009, 01:37 pm by feliksayk Reason: 1
In case someone else is interested in recreating this here, here's the complete code and diagram.

Code: [Select]
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position

void setup()
{
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop ()
{
 myservo.write (5);     // move servo to 5 degrees
 delay (75);            // wait 75ms
 myservo.write (10);    // move servo to 10 degrees
 delay (75);            // wait 75ms
 myservo.write (15);    // move servo to 15 degrees
 delay (75);            // wait 75ms
 myservo.write (20);    // move servo to 20 degrees
 delay (1000 * 9);      // wait 9 seconds
 myservo.write (15);    // move servo back to 15 degrees
 delay (75);            // wait 9 seconds.
 myservo.write (10);    // move servo back to 10 degrees
 delay (75);            // wait 9 seconds.
 myservo.write (5);     // move servo back to 5 degrees
 delay (75);            // wait 9 seconds.
 myservo.write (0);     // move servo back to 0 degrees
 delay (1000 * 9);      // wait 9 seconds
}




AWOL

#19
Dec 07, 2009, 01:48 pm Last Edit: Dec 07, 2009, 01:49 pm by AWOL Reason: 1
One way of doing what you want is to use a "for" loop containing the "servo.write" and a "delay".

BTW, congratulations - feels good when it works through your own efforts, doesn't it?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

feliksayk

Now that one I do not understand, maybe yet, but if you wish to expand on that, you are welcome. On the other hand, yes, it does feel good, but you did give me those 4 lines on which I expanded on. Thanks.

AWOL

#21
Dec 07, 2009, 02:18 pm Last Edit: Dec 08, 2009, 09:01 am by AWOL Reason: 1
Well, a "for" loop has four parts to it:
1) The initialisation (performed just once)
2) the condition for continuing the loop (performed at the start of every loop)
3) an action or actions to be performed at the end of the loop
4) the body of the loop

So, to print the numbers 1..10
Code: [Select]

//    initialise      condition       end of loop action      
for (int number = 1; number <= 10; number = number + 1) {
 // body of loop
 Serial.println (number);
}
// rest of sketch


a) So, declare a variable "number" and set it to 1.
b) Test if "number" is less than or equal to 10. if not, go to f)
c) if it is, print its value
d) add one to "number" (there are shorter ways of doing this)
e) goto  b)
f) rest of sketch

Now, imagine counting from 0 to 30, with a delay.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

feliksayk

Sorry, it's not getting to me. I kinda understand things when I see a complete example code, I break it down and see it part by part and understand what each part does, with a little explanation of course. But if you give me little piece of code and tell me to do the other parts, its unlikely that I will be able to understand much. Well, I guess I'll try to learn C++...once again.  ::)

AWOL

#23
Dec 08, 2009, 08:59 am Last Edit: Dec 08, 2009, 09:02 am by AWOL Reason: 1
Well, a little exercise for the imagination.
Imagine in the example above of printing the integers 1..10, I'd used a variable called "angle" instead of one called "number" (the name isn't really important, but it helps to understand what the program is doing).

Now, instead of going from 1 to 10, we go from 0 to 30, and instead of writing the variable "number" to the serial port (which is what, in essence "Serial.println" does), we write the variable "angle" to the servo, using "myservo.write (angle)" that you've already used.

Now, to give the servo time to move to its new position, we add a delay of a few tens of milliseconds (let's say 75) after the write.

Job done.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

feliksayk

Hmmm, I got another question. Is it possible, without making the code complicated, to add another statement which basically allows whatever I have in the "void loop" code to run only a limited number of times, say 1000 times. Or do you need a totally different setup?

AWOL

#25
Dec 10, 2009, 03:51 pm Last Edit: Dec 10, 2009, 04:12 pm by AWOL Reason: 1
Yes, it is simple; just wrap all the code inside the braces of "loop" like this:
Code: [Select]
for (int i = 0; i < 1000; ++i {

//all the code inside "loop" goes in here (but not "void loop () {" itself)

}

for (;;);  // this will stop "loop" from running again.

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

feliksayk

#26
Dec 11, 2009, 05:21 pm Last Edit: Dec 11, 2009, 05:21 pm by feliksayk Reason: 1
Thank you for that reply. I've incorporated it into my code and compiled successfully. Haven't tested yet though. Here's the code that I currently have:

Code: [Select]
#include <Servo.h>

Servo myservo;      // create servo object to control a servo

int pos = 0;        // variable to store the servo position
int ledPin = 13;    // LED connected to digital pin 13

void setup()
{
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 pinMode(ledPin, OUTPUT);   // sets the digital pin as output
}

void loop ()
{
 for (int i = 0; i < 1000; ++i) {

 digitalWrite(ledPin, HIGH);   // sets the LED on
 myservo.write (4);         // move servo to 4 degrees
 delay (50);                // wait 50ms
 myservo.write (8);         // move servo to 8 degrees
 delay (50);                // wait 50ms
 myservo.write (12);        // move servo to 12 degrees
 delay (50);                // wait 50ms
 myservo.write (16);        // move servo to 16 degrees
 delay (50);                // wait 50ms
 myservo.write (20);        // move servo to 20 degrees
 digitalWrite(ledPin, LOW);    // sets the LED off
 delay (1000 * 9);          // wait 9 seconds
 digitalWrite(ledPin, HIGH);   // sets the LED on
 myservo.write (16);        // move servo back to 16 degrees
 delay (50);                // wait 50ms
 myservo.write (12);        // move servo back to 12 degrees
 delay (50);                // wait 50ms
 myservo.write (8);         // move servo back to 8 degrees
 delay (50);                // wait 50ms
 myservo.write (4);         // move servo back to 4 degrees
 delay (50);                // wait 50ms
 myservo.write (0);         // move servo back to 0 degrees
 digitalWrite(ledPin, LOW);    // sets the LED off
 delay (1000 * 9);          // wait 9 seconds
   
}
for (;;);  // this will stop "loop" from running again.
 
}

AWOL

Code: [Select]
for (int i = 0; i < 1000; ++i) {

Useful trick for testing:
You're going to be there a while waiting for this to freeze to prove it "only" did it 1000 times, so:

Code: [Select]

//above "setup ()"
const int LOOP_TIMES = 10;
..
..
..

for (int i = 0; i < LOOP_TIMES; ++i) {


As PaulS noted, there's more scope for "for" loops there still.

Enjoy!

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

feliksayk

#28
Dec 13, 2009, 05:18 pm Last Edit: Dec 13, 2009, 05:19 pm by feliksayk Reason: 1
Once again I thank you. It works like a charm. And of course I tested it using a smaller value in this line
Code: [Select]
for (int i = 0; i < 1000; ++i) {

Go Up