Friday, March 4, 2016

Servo Labs

Servo controlled via pulse width modulation

1. The delay is because we can't create an analog signal out of the Arduino, only simulate one. And in order for pulse width modulation to work, you need some time in order to simulate the analog signal.
2. The range of the servo motor is from 0 to 180. Then a 45 rotation is 1/4th of the maximum pulse, or 25%.
3. Here is a quote from the tone manual page (located https://www.arduino.cc/en/Reference/Tone): "Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega)." This means that the PWM required for both tone and servo movement will interfere with each other.

Servo controlled by potentiometer

(no questions)

Servo controlled by push button switch

(no questions)

Here is the code I wrote for this to work (I will leave pin setup and circuit setup as an exercise for the reader):
#include <Servo.h>

const int servo_pin = 5;
const int pause = 45;
const int switch_pin = 3;
int angle = 0;
int direction = 1;
Servo servo;

void setup()
{
  servo.attach(servo_pin);
  pinMode(switch_pin, INPUT);
}

void turn_servo()
{
  if (angle == 0)
  {
    direction = 1;
  }
  if (angle == 180)
  {
    direction = -1;
  }
  angle += 10 * direction;
  servo.write(angle);
}

void loop()
{
  int switch_val = digitalRead(switch_pin);
  if (switch_val == HIGH)
  {
    turn_servo();
    delay(100);
  }
  else
  {
    delay(100);
  }
}

No comments:

Post a Comment