Pages: 1 [2]   Go Down
Author Topic: Driving a servo using brushed ESC  (Read 2732 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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.
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50901
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-wink
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In case someone else is interested in recreating this here, here's the complete code and diagram.

Code:
#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
}
 

« Last Edit: December 07, 2009, 07:37:22 am by feliksayk » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: December 07, 2009, 07:49:09 am by AWOL » Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//    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.
« Last Edit: December 08, 2009, 03:01:17 am by AWOL » Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  :smiley
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 08, 2009, 03:02:19 am by AWOL » Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, it is simple; just wrap all the code inside the braces of "loop" like this:
Code:
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.

« Last Edit: December 10, 2009, 10:12:46 am by AWOL » Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
  
}
« Last Edit: December 11, 2009, 11:21:45 am by feliksayk » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
//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!

Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Once again I thank you. It works like a charm. And of course I tested it using a smaller value in this line
Code:
for (int i = 0; i < 1000; ++i) {
« Last Edit: December 13, 2009, 11:19:26 am by feliksayk » Logged

Pages: 1 [2]   Go Up
Jump to: