Skyler Tran – Update 6

Update Servos Position using Arduino

In this week, I’ve worked on the code for updating servos’ position. The purpose of this code must be controlling servos independently, meaning the servos can move as once. To make all servos move as once, I have to use millis(). The millis() is a return function of millisecond built in Arduino IDE.  So each servo will be assigned different interval that the microcontroller generate a PWM. The interval is measured in millisecond which how long we want to generate another PWM. 

 On the diagram below shows PWM is generated every 20 microseconds that means the interval will be 20.ServoPwm
On the diagram below shows how big of a pulse is, and that will make the servo change its position. For example, the 1 ms pulses will make the servo move to -90 degrees; 1.5 ms pulses will make the servo move to 0 degree; and lastly 2 ms pulses will make the servo move to 90 degrees.
Sinais_controle_servomotor

The code below here shows the concept of how to make servos move. The code has the class, so in the main file you can add many servo as you would like by using Sweeper (interval, speed) function. 

#include <Servo.h>

/*
* Task 2: Updating Servo Positions
* Engineer: Skyler Tran
*
* Credit: Adafruit.com
*
*/
class Sweeper {
  Servo servo;
  int pos, increment, updateInterval;
  unsigned long lastUpdate;

  public:
    // Constructor
    Sweeper(int interval, int servoSpeed) {
      updateInterval = interval;
      increment = servoSpeed;
    }

    void Attach(int pin) {
      servo.attach(pin);
    }

    void Detach() {
      servo.detach();
    }

    void update() {
      if (millis() – lastUpdate > updateInterval) {
        lastUpdate = millis();
        pos += increment;
        servo.write(pos);
        //Serial.println(pos);
        if ((pos >= 180) || (pos <= 0)) {
          increment -= increment;
        }
      }
    }
};

// initial servos
Sweeper sweeper1(15, 5); // (interval, speed)
Sweeper sweeper2(15, 5);
Sweeper sweeper3(15, 5);
Sweeper sweeper4(15, 5);
Sweeper sweeper5(15, 5);
void setup() {
  Serial.begin(9600);
  sweeper1.Attach(2); // servo 1 = pin 2
  sweeper2.Attach(3); // servo 2 = pin 3
  sweeper3.Attach(4); // servo 3 = pin 4
  sweeper4.Attach(5); // servo 4 = pin 5
  sweeper5.Attach(6); // servo 5 = pin 6
}

void loop() {
  //sweeper1.update();
  //sweeper2.update();
  //sweeper3.update();
  //sweeper4.update();
  //sweeper5.update();

  // Serial Input
  const int BUFFERLENGTH = 255;
  char IncomingByte[BUFFERLENGTH];
  String RawCommandLine = “”;

  // Wait for command
  Serial.println(F(“Please insert a servo that you want to change position!”));
  Serial.println(F(“Ex: servo1, servo2, servo3, servo4, servo5”));
  while (1) {
    if (Serial.available() > 0) {
      int charsCounter = Serial.readBytesUntil(‘\n’, IncomingByte, BUFFERLENGTH);
      for (int i = 0; i < charsCounter; i++) {
        RawCommandLine += IncomingByte[i];
      }
      break;
    }
  }

  if (RawCommandLine == “servo1”) {
    sweeper1.update();
    Serial.print(RawCommandLine);
    Serial.println(F(“‘s position is updated.”));
    Serial.println();
  }

  else if (RawCommandLine == “servo2”) {
    sweeper2.update();
    Serial.print(RawCommandLine);
    Serial.println(F(“‘s position is updated.”));
    Serial.println();
  }

  else if (RawCommandLine == “servo3”) {
    sweeper3.update();
    Serial.print(RawCommandLine);
    Serial.println(F(“‘s position is updated.”));
    Serial.println();
  }

  else if (RawCommandLine == “servo4”) {
    sweeper4.update();
    Serial.print(RawCommandLine);
    Serial.println(F(“‘s position is updated.”));
    Serial.println();
  }

  else if (RawCommandLine == “servo5”) {
    sweeper5.update();
    Serial.print(RawCommandLine);
    Serial.println(F(“‘s position is updated.”));
    Serial.println();
  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s