using Servo

Hey everyone,

Next step in my project is to use a servo. Right now my servo is connected and I'm using the Servo.h to send commands to it. If I go with servo1.write(0) and servo1.write(90) it works, but when I try servo1.write(180) it doesn't move. Why is that ?

Here my code:

#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Bridge.h>
#include <Console.h>
#include <Wire.h>
#include <Servo.h>
#include <YunServer.h>
#include <YunClient.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *frontWheels = AFMS.getMotor(2);
// You can also make another motor on port M2
Adafruit_DCMotor *rearWheels = AFMS.getMotor(1);
// Declaring the servo that controls the webcam
Servo *servo1;

// The pin that the LED is attached to
const int ledPin = 13;

// We open the socket 5678 to communicate.
YunServer server(5678);

void setup() {
  Serial.begin(9600);

  // Attach the LED to pin 13 as output
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  
  // Bridge startup
  Bridge.begin();

  // Create with the default frequency 1.6KHz
  AFMS.begin();

  // Front Wheels initialization
  frontWheels->setSpeed(150);
  frontWheels->run(FORWARD);
  frontWheels->run(RELEASE);

  // Rear Wheels initialization
  rearWheels->setSpeed(150);
  rearWheels->run(FORWARD);
  rearWheels->run(RELEASE);
  
  // Attach my servo to pin 10
  servo1->attach(10);

  server.noListenOnLocalhost();
  server.begin();
}

void loop() {
  int _speed;
  YunClient client = server.accept();

  // There is a new client
  if (client) {
    client.setTimeout(5);

    String command = "none";
    digitalWrite(ledPin, LOW);
    
    servo1->write(180);

    while (!command.equals("stop")) {
      if (client.available() > 0) {
        command = client.readString();

        Serial.println(command);

        if (command.length() == 3) {
          if (command.substring(1,3).toInt() > 0 && command.substring(1,3).toInt() <= 100) {
            _speed = map(command.substring(1,3).toInt(), 0, 100, 0, 255);
          } 
          _speed = map(command.substring(1,3).toInt(), 0, -100, 0, 255);
        }
        else if (command.length() == 4) {
          _speed = map(command.substring(1,4).toInt(), 0, 100, 0, 255);
        }
        else {
          _speed = 50;
        }

        if (command.substring(0,1).equals("f") && _speed > 0) {
          rearWheels->run(RELEASE);
          rearWheels->setSpeed(_speed);
          rearWheels->run(FORWARD);
        } 
        else if (command.substring(0,1).equals("b") && _speed > 0) {
          rearWheels->run(RELEASE);
          rearWheels->setSpeed(_speed);
          rearWheels->run(BACKWARD);  
        }
        else if (command.substring(0,1).equals("l") && _speed > 0) {
          frontWheels->run(RELEASE);
          frontWheels->setSpeed(165);
          frontWheels->run(BACKWARD);
        }
        else if (command.substring(0,1).equals("r") && _speed > 0) {
          frontWheels->run(RELEASE);
          frontWheels->setSpeed(165);
          frontWheels->run(FORWARD);
        }
        else if (command.substring(0,1).equals("s")) {
          frontWheels->run(RELEASE);
          rearWheels->run(RELEASE);
        }
        else if (command.substring(0,1).equals("0")) {
          Serial.println("SERRRVOOOO");
          for (int i = 0; i < 255; i++) {
            servo1->write(map(i, 0, 255, 0, 180));
            delay(3);
            }
        }
        else if (command.substring(0,2).equals("90")) {
          servo1->write(90);
        }
        else if (command.substring(0,3).equals("180")) {
          servo1->write(180);
        }
        delay(500);
      }
    }
    digitalWrite(ledPin, HIGH);
    client.stop();
  }
}

I confirmed that it does go in the else if (180)

Another thing is that I tried using the SoftwareServo librairy but it just doesn't compile on the Yun is there any reason why?

Thank you all

Hi

some servos can't reach 180 degrees... or better some servos doesn't work in the "default" pulse range (1-2ms) Arduino library generates. Try a lower value or use the "raw" writeMicroseconds() method to move your servo.

Thank you for your answer, I'll give writeMicroseconds a try, but what is weird is that if I execute the motorparty example the servo goes all the way to 180.

Yeah writeMicroseconds seem to fix my problem. Thank you luca for your input.