Help

Hi Arduino community,

I have a bit of a trouble with coding. At the moment, I am trying to merge a servo code and an LED code (the Blink sketch). My goal is to make the servo move, then stop. After the servo finishes moving, the LED will blink. Can someone help me accomplish this goal? Thanks.

Servo code:

/*

Example sketch 08

SINGLE SERVO

  Sweep a servo back and forth through its full range of motion.

  A "servo", short for servomotor, is a motor that includes 
  feedback circuitry that allows it to be commanded to move to
  specific positions. This one is very small, but larger servos
  are used extensively in robotics to control mechanical arms,
  hands, etc. You could use it to make a (tiny) robot arm,
  aircraft control surface, or anywhere something needs to be
  moved to specific positions.

Hardware connections:

  The servo has a cable attached to it with three wires.
  Because the cable ends in a socket, you can use jumper wires
  to connect between the Arduino and the servo. Just plug the
  jumper wires directly into the socket.
  
  Connect the RED wire (power) to 5 Volts (5V)
  Connect the WHITE wire (signal) to digital pin 9
  Connect the BLACK wire (ground) to ground (GND)

  Note that servos can use a lot of power, which can cause your
  Arduino to reset or behave erratically. If you're using large
  servos or many of them, it's best to provide them with their
  own separate 5V supply. See this Arduino Forum thread for info:
  http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1239464763

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://www.arduino.cc to learn about the Arduino.

Version 2.0 6/2012 MDG
*/


// If we had to write a sketch to control a servo from scratch,
// it would be a lot of work. Fortunately, others have done the
// hard work for you. We're going to include a "library"
// that has the functions needed to drive servos.

// A library is an set of additional functions you can add to
// your sketch. Numerous libraries are available for many uses,
// see http://arduino.cc/en/Reference/Libraries for information
// on the standard libraries, and Google for others. When you're
// using a new part, chances are someone has written a library
// for it.

#include <Servo.h>  // servo library

// Once you "include" a library, you'll have access to those
// functions. You can find a list of the functions in the servo
// library at: http://arduino.cc/en/Reference/Servo
// Most libraries also have example sketches you can load from
// the "file/examples" menu.

// Now we'll create a servo "object", called myservo. You should
// create one of these for each servo you want to control. 
// You can control a maximum of twelve servos on the Uno 
// using this library. (Other servo libraries may let you
// control more). Note that this library disables PWM on
// pins 9 and 10!

Servo servo1;  // servo control object


void setup()
{
  // We'll now "attach" the servo1 object to digital pin 9.
  // If you want to control more than one servo, attach more
  // servo objects to the desired pins (must be digital).

  // Attach tells the Arduino to begin sending control signals
  // to the servo. Servos require a continuous stream of control
  // signals, even if you're not currently moving them.
  // While the servo is being controlled, it will hold its 
  // current position with some force. If you ever want to
  // release the servo (allowing it to be turned by hand),
  // you can call servo1.detach().

  servo1.attach(9);
}


void loop()
{
  int position;
  
  // To control a servo, you give it the angle you'd like it
  // to turn to. Servos cannot turn a full 360 degrees, but you
  // can tell it to move anywhere between 0 and 180 degrees.

  // Change position at full speed:

  servo1.write(90);    // Tell servo to go to 90 degrees

  delay(1000);         // Pause to get it time to move

  servo1.write(180);   // Tell servo to go to 180 degrees

  delay(1000);         // Pause to get it time to move

  servo1.write(0);     // Tell servo to go to 0 degrees

  delay(1000);         // Pause to get it time to move
  
  // Change position at a slower speed:

  // To slow down the servo's motion, we'll use a for() loop
  // to give it a bunch of intermediate positions, with 20ms
  // delays between them. You can change the step size to make 
  // the servo slow down or speed up. Note that the servo can't
  // move faster than its full speed, and you won't be able
  // to update it any faster than every 20ms.

  // Tell servo to go to 180 degrees, stepping by two degrees
 
  for(position = 0; position < 180; position += 2)
  {
    servo1.write(position);  // Move to next position
    delay(20);               // Short pause to allow it to move
  }

  // Tell servo to go to 0 degrees, stepping by one degree

  for(position = 180; position >= 0; position -= 1)
  {                                
    servo1.write(position);  // Move to next position
    delay(20);               // Short pause to allow it to move
  }
}

LED code:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

Thanks!

How will you know that the servo has finished moving? You can't ask the servo that.

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html http://arduino-info.wikispaces.com/CombiningArduinoSketches http://forum.arduino.cc/index.php?topic=258240 http://en.wikiversity.org/wiki/Arduino_Sketch_Merge

What seems to be the problem ? Can't you copy and paste the LED code into the servo sketch ? Why is that so difficult ?

PaulS: How will you know that the servo has finished moving? You can't ask the servo that.

I know the servo running time cannot be controlled. What I mean by the servo finishing moving is when the servo finishes moving to a certain angle. I hope that is possible :~

raschemmel: What seems to be the problem ? Can't you copy and paste the LED code into the servo sketch ? Why is that so difficult ?

I was afraid that wouldn't work, but now you said it, I will try it out. Thanks.

There is no feedback from the servo....

raschemmel: What part of " There is NO FEEDBACK from the servo !" did you not undrrstand ?

Have you tried decaf?

Sorry. I corrected it.

You could put a delay() at the end of the servo move part, with a bit of an experiment to see how long the delay is. But it's no guarantee: if the load on the servo prevented its movement, it might never reach its target but when delay was up, you would incorrectly assume it had. Aslo very inefficient but maybe that doesn't matter.

There are servos with feedback; if this is a real-life problem not just an exercise, you might consider one like this.

To combine the sketches, you basically use one as your "target", do a save as with new name, then copy the code from the other one into there. The part above setup() goes above setup() in the target, the part in setup() goes into the new setup(), and loop's code goes into loop(). Check that you don't have duplicate variable names or pin usage.

Perhaps this demo about merging sketches might help.

...R

There are servos with feedback; if this is a real-life problem not just an exercise, you might consider one like this.

That's news to me. Pretty cool ! Learn something new every day...

My goal is to make the servo move, then stop. After the servo finishes moving, the LED will blink. Can someone help me accomplish this goal? Thanks.

To do just what you say, just put the servo movement command in the blink code setup section. I suspect you actually want to do more.