Servo.h library (included with arduino) (SOLVED)


#1

I tried to compile the servo example from the arduino examples but servo.h is not found. I assume it isnt pulled into the source yet. It would be nice to have since you include a servo in the kit.

Thanks,
Colin


#2

Hi Colin - the Servo library is actually included by default; it works just like the Arduino library, except you don’t need the line:

#include <Servo.h>

Remove that line and it should work fine!


#3

Yes, that works! Servo is moving. Servo attached to D0. (For the next person: PWM is available on D0,D1 and the An pins. Servo must be on one of these pins.)

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(D0);  // attaches the servo on pin 0 to the servo object 
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
} 

#4

Just to clarify, a servo can only be attached to pins with a timer (A0, A1, A4, A5, A6, A7, D0, and D1), which are the same pins that the 8 PWMs are available on.

Even though D0 actually equates to 0, it’s probably best to code it like myservo.attach(D0); to keep everything super clear, considering the pins are labeled that way. Also makes it easier when you start talking about the analog pins. :wink:

Have fun!


#5

I am very new to this, apart from fiddling a bit with an Arduino I’m still not really sure what I am doing, so please bear with me!

I have flashed the above code to my Spark Core. I have an Analog Servo powered by a separate 5V USB charger and the Spark powered, for now, by USB from my iMac.

Flashing the code to the device does not move the servo, nothing happens. But if i take out and put in the power to the server it twitches then nothing. I have the control wire (yellow) to pin A0 but I have tried D0 and D1.

Any ideas of what I am doing wrong? I am thinking of making a circular gauge with weather icons around the edge and possibly a separate gauge for temperature. All mounted up to some nice wooden panel/face … or something like that!


#6

Hmm, that “Analog” servo name sounds confusing… it’s digital it you are hooking it up to a digital output.

That said, maybe that particular servo has an issue with input pulses that are 3.3V? Sounds like @guru_florida’s works so some must be ok.

Any chance you can try the servo on an Arduino to see if the servo definitely works, and then if not on the Spark Core, you could try to level shift the 3.3V up to 5V for the signal pin of the servo:


#7

@BDub and @guru_florida , would you mind taking a look at my code? I have the rc-car example code working well with my spark and motor shield. I have installed a robot arm and hand to my car, but haven’t quite gotten any servos to work. I have one of the servos plugged into D0 at the moment, but can’t seem to control it with my current software setup.

For the rc-car, I upload the .cpp firmware (below) then issue commands through a .html file I open on my browser.

    int leftMotorEnable   = D1;
int rightMotorEnable  = A7;
int leftMotorDir    = D3;
int rightMotorDir   = D4;

Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position

String lastCommand="rc,STOP";

void setup()
{
  myservo.attach(D0);  // attaches the servo on pin 0 to the servo object
  
  //Register Spark function
  Spark.function("rccar", rcCarControl);

  pinMode(leftMotorDir, OUTPUT);
  pinMode(leftMotorEnable, OUTPUT);
  pinMode(rightMotorDir, OUTPUT);
  pinMode(rightMotorEnable, OUTPUT);
  
  pinMode(D0, OUTPUT);

  pinMode(D7,OUTPUT);
}

void loop()
{
  // Nothing to do here
}

/*******************************************************************************
 * Function Name  : rcCarControl
 * Description    : Parses the incoming API commands and sets the motor control
          pins accordingly
 * Input          : RC Car commands
          e.g.: rc,FORWARD
            rc,BACK
 * Output         : Motor signals
 * Return         : 1 on success and -1 on fail
 *******************************************************************************/
int rcCarControl(String command)
{
  if(command.substring(3,7) == "STOP")
  {
    digitalWrite(leftMotorEnable,LOW);
    digitalWrite(rightMotorEnable,LOW);

    digitalWrite(leftMotorDir,LOW);
    digitalWrite(rightMotorDir,HIGH);
    
    lastCommand = command;
    return 1;
  }

  if(command.substring(3,7) == "BACK")
  {
    digitalWrite(leftMotorDir,LOW);
    digitalWrite(rightMotorDir,LOW);

    digitalWrite(leftMotorEnable,HIGH);
    digitalWrite(rightMotorEnable,HIGH);

    lastCommand = command;
    return 1;
  }

  if(command.substring(3,10) == "FORWARD")
  {
    digitalWrite(leftMotorDir,HIGH);
    digitalWrite(rightMotorDir,HIGH);

    digitalWrite(leftMotorEnable,HIGH);
    digitalWrite(rightMotorEnable,HIGH);

    lastCommand = command;
    return 1;
  }

  if(command.substring(3,8) == "RIGHT")
  {
    digitalWrite(leftMotorDir,HIGH);
    digitalWrite(rightMotorDir,LOW);

    digitalWrite(leftMotorEnable,HIGH);
    digitalWrite(rightMotorEnable,HIGH);
    
    delay(220);
    rcCarControl(lastCommand);
    
    return 1;
  }

  if(command.substring(3,7) == "LEFT")
  {
    digitalWrite(leftMotorDir,LOW);
    digitalWrite(rightMotorDir,HIGH);

    digitalWrite(leftMotorEnable,HIGH);
    digitalWrite(rightMotorEnable,HIGH);

    delay(200);
    rcCarControl(lastCommand);

    return 1;
  }
  
  if(command.substring(3,7) == "GRAB")
  {
    pos = 179;
    myservo.write(pos);

    delay(15);
    rcCarControl(lastCommand);

    return 1;
  }
  
  if(command.substring(3,7) == "RELEASE")
  {
    pos = 1;
    myservo.write(pos);

    delay(15);
    rcCarControl(lastCommand);

    return 1;
  }  
  
  // If none of the commands were executed, return false
  return -1;
}

Any advice? .html file is on my github: https://github.com/TheLastQuestion/GrabbyBot

thanks to folks @zach @will at spark for such an awesome product! have had lots of fun allowing my friends to control my robot from miles away and watching it move over skype/gchat !


#8

Code looks like it would work. Can you confirm that the GRAB/RELEASE code is actually working?
Does the servo resist you forcing it?
Yes - myservo object is outputting proper PWM so the software, pin and your wiring is working.
No - recheck wiring, scope the pin, or issue with software.
Does the servo work with my code? That code did work for me and it tests directly without any GRAB/RELEASE or other conditional code getting in the way.

Colin


#9

I’m guessing it GRABs but does not RELEASE?

This will never be true without changing it to:

if(command.substring(3,10) == "RELEASE")

Also recursively calling this routine seems like a bad idea. rcCarControl(lastCommand); Does that even work? This is something you should probably do in loop() instead. Save lastCommandGlobal as the last command if you want to continue calling it over and over… that way you can also get rid of the delays in the rcCarControl() routine, which don’t help the speed of receiving future commands and will make it have a laggy response.


#10

thanks for the responses!

so, @BDub, thanks for the 3,10 fix…whoops

even with that fix, neither grab nor release work. however, turn left and turn right commands make the arm move. might be a wiring issue or issue with the motor shield…? anyway, i am going to pick up another battery holder to try powering servo independent of motor battery holder and spark battery holder, then see if turn left and right commands still effect servo.

@BDub i’m afraid i am still so new at all of this that your last few points are a bit greek to me. i’ve done enough arduino sketches to understand the roles of setup() and loop(). as i grabbed the rc car example from spark though, i never got to fully understand how the .cpp works. are you saying i could simply take the Spark.function("rccar", rcCarControl); line and put it in loop? how would that really change anything? isn’t it just a function instructing the core to listen for api commands?

sorry if all of this is dribble…i just really want a robot that can fetch my slippers for me! (no pets allowed in the apartment…)


#11

No problem. So how are you connecting your servos? Can you tell me exactly what type of servo, wire colors and where they are plugged into? If on the shield, do you have a schematic for the shield? (I think I know which one it is, but this is kind of like your homework for troubleshooting :wink:

So I’m guessing you didn’t code the rcCarControl(lastCommand); in then. It seems like it’s meant to allow you to drive FWD or RVS and then if you press LEFT or RIGHT at the same time, it will continue trying to drive FWD or RVS while also working in a LEFT or RIGHT. Makes sense, but could easily be done with a simple state machine in the main loop(). Or maybe better yet would be to send a command that was FWDRT, FWDLF, RVSRT, or RVSLF in addition to the basic 4. that way you could just tell it what to do, instead of telling to to do one thing (turn RIGHT) and also process the last command.

Sounds like I need to mess with this… I have some motor shields :slight_smile: But mine work a little more normally… I can’t quite make out how the HIGH / LOW signals make sense for something like driving FOWARD or REVERSE… I’m guessing that works though!


#12

Hi, I am trying to get the code above going on my sparkcore and I’m very new to this so please forgive me if the questions are a little silly. I have a servo which I think is this one: https://www.sparkfun.com/products/9065 it came in a sparkfun arduino inventors kit. It has 3 wires: red, black and white. I have a separate 2amp power supplied to 5volts on red, ground on black and I have white going to pin D0. The sparkcore is powered by USB on my computer.

I flashed the above sketch to my sparkcore and then I powered up the servo. It does make a little sound when powered up initially. It seems to hum even when idle however so I am wondering if it connected correctly.

Anyhow, I opened up the sparkcore android tinker app and tried controlling pin D0 but I couldn’t get it to work. Am I missing something?


#13

Hi @yugnats

First off, the circuit! What you described sounds great except did you remember to tie the ground from the Spark core to the ground of the supply powering the servo? Without that, it won’t work.

When you flash the above code into your core, it replaces Tinker, so that is not the way to go. With the above code, your servo should be moving back and forth.

If you want to control a servo from a web page via Spark functions and variables, see this tutorial:

There are some pictures of how I hooked it up there too.


#14

Oh my goodness bko you were right! I grounded the SC and it works! There is no better feeling than when you have been tinkering with something and it finally does work- thank you for getting me there :smile:

I really appreciate the quick response and help, thanks again and I’m off to check out your tutorial :slight_smile:


#15

I had a quick read through of the tutorial and it looks great, thank you. The lock I want to control is actually close enough to my wi-fi connection so I don’t really need an internet connected solution but I figured that would be a great way to learn. I’m off to attempt your tutorial :slight_smile:


#16

I had the same issue… try using A2 - A5
those should work. I think there are issues with the digital pins that harbor SPI.


#17

Servo: You said to remove the include servo.h, which was throwing an error that it couldn’t be found. I did.

However, now I get an error on the first line, “Servo myservo;” as not declared, and “Servo does not name a type”.

I’m an old BASIC programmer for many years, so the oddities of this C variation are new to me.


#18

Hi mate,

If the above solution is not working (remove #include <Servo.h>)
just replace it with #include <Servo\src\Servo.h>