0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #15 on: December 06, 2009, 07:01:30 pm » |
Thanks for confirming, this is what I am now using, it's awesome. #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
Offline
Brattain Member
Karma: 316
Posts: 35590
Seattle, WA USA
|
 |
« Reply #16 on: December 06, 2009, 07:06:19 pm » |
Now it's time to learn about for loops, and proper commenting.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #17 on: December 06, 2009, 07:08:00 pm » |
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 
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #18 on: December 07, 2009, 07:36:57 am » |
In case someone else is interested in recreating this here, here's the complete code and diagram. #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
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #19 on: December 07, 2009, 07:48:42 am » |
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.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #20 on: December 07, 2009, 08:09:32 am » |
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
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #21 on: December 07, 2009, 08:18:29 am » |
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 // 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.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #22 on: December 07, 2009, 09:16:41 pm » |
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. : 
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #23 on: December 08, 2009, 02:59:54 am » |
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.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #24 on: December 10, 2009, 09:14:00 am » |
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
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #25 on: December 10, 2009, 09:51:23 am » |
Yes, it is simple; just wrap all the code inside the braces of "loop" like this: 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.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #26 on: December 11, 2009, 11:21:09 am » |
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: #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
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #27 on: December 11, 2009, 03:41:33 pm » |
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: //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.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #28 on: December 13, 2009, 11:18:59 am » |
Once again I thank you. It works like a charm. And of course I tested it using a smaller value in this line for (int i = 0; i < 1000; ++i) {
|
|
|
|
« Last Edit: December 13, 2009, 11:19:26 am by feliksayk »
|
Logged
|
|
|
|
|
|