Servo

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Servo

const byte potpin = A0; // analog pin used to connect the potentiometer
const unsigned long PRESCALER = 8; // Timer 1 prescaler
const float PULSE_PERIOD = 0.020; // 20 mS
const float ZERO_POSITION_WIDTH = 0.0005; // 0.5 mS
const float FULL_POSITION_WIDTH = 0.0024; // 2.4 mS
// how far apart the pulses are
const unsigned long PULSE_WIDTH_COUNT = F_CPU / PRESCALER *
PULSE_PERIOD;
// minimum pulse width (-45 degrees)
const unsigned long ZERO_POSITION_COUNT = F_CPU / PRESCALER *
ZERO_POSITION_WIDTH;
// minimum pulse width (+45 degrees)
const unsigned long FULL_POSITION_COUNT = F_CPU / PRESCALER *
FULL_POSITION_WIDTH;
void setup()
{
//REGISTRO DE CONTROL DEL TIMWER1//
TCCR1A = 0; // disable all PWM on Timer1 whilst we set it up
ICR1 = PULSE_WIDTH_COUNT - 1; // frequency is every 20ms (zero-relative)

// Configure timer 1 for Fast PWM mode using ICR1, with 8x prescaling
TCCR1A = bit (WGM11);
TCCR1B = bit (WGM13) | bit (WGM12) | bit (CS11); // fast PWM top at ICR1
TCCR1A |= bit (COM1A1); // Clear OC1A/OC1B on Compare Match,
pinMode (9, OUTPUT);
} // end of setup
void loop()
{
int val = analogRead(potpin); // reads the value of the potentiometer (value
between 0 and 1023)
OCR1A = ZERO_POSITION_COUNT + (val * (FULL_POSITION_COUNT -
ZERO_POSITION_COUNT) / 1024) - 1;
delay(15); // wait for the servo to get there
} // end of loop
Servo y motor
#include "TimerOne.h"
#include <Servo.h>
int analog1=0;
int analog2=1;
int motorDC=10;
int servomo=9;
int valor1;
int valor2;
int conversion;
Servo servo1;
void setup() {
Timer1.initialize(1000);
servo1.attach(3);
}
void loop() {
valor1=analogRead(analog1);
valor2=analogRead(analog2);
Timer1.pwm(motorDC,valor1);
conversion=(valor2*180)/1023;
servo1.write(conversion);
}

You might also like