Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Arduino Sketch to test robotic servos RDS3115 and RDS3135
//////////////////////////////////////////////////////
//SKETCH: SERVO TEST RDS3115/RDS3135
//AUTHOR: DAST / NAYLAMP MECHATRONICS
//D CREATED: 28/12/17
//D LAST M.: 03/01/18
//////////////////////////////////////////////////////
#include <Servo.h>
//VARIABLES
Servo myservo; //create a servo object
//RDS3115 0°:833uS,180°:2167uS
//RDS3135 0°:500uS,180°:2500uS
int servo_min_pulse=500; //minimum pulse time in microseconds
int servo_max_pulse=2500; //maximum pulse time in microseconds
int t=3000; //hold time for test
int led=13; //led on board
//FUNCTIONS
void servo_seq1(int hold_time);
void servo_seq2(int min_pulse, int max_pulse, int hold_time); //sweep by pulse
void servo_seq3(int min_angle, int max_angle, int hold_time); //sweep by angle
void rds3115_angle(int angle);
void setup() {
myservo.attach(9,servo_min_pulse,servo_max_pulse); //servo connected to pin 9
pinMode(led,OUTPUT);
Serial.begin(9600);
Serial.println(">>SERVO TEST:");
myservo.writeMicroseconds(1500); //servo to 90°
Serial.println("Angle: 90°");
digitalWrite(led,1);
delay(5000); //hold time
digitalWrite(led,0);
}
void loop() {
//test for servo rds3115
for(int i=-45;i<=225;i=i+45){
rds3115_angle(i);
delay(2000);
}
//servo_seq1(t);
servo_seq2(servo_min_pulse,servo_max_pulse,5); //sweep
//servo_seq3(0,180,10);
}
void servo_seq1(int hold_time){
Serial.println("Sequence #1");
myservo.write(0); //go to 0°
Serial.println("angle: 0°");
delay(hold_time);
myservo.write(90); //go to 90°
Serial.println("angle: 90°");
delay(hold_time);
myservo.write(180); //go to 180°
Serial.println("angle: 180°");
delay(hold_time);
myservo.write(90); //go to 90°
Serial.println("angle: 90°");
delay(hold_time);
myservo.write(0); //go to 0°
Serial.println("angle: 0°");
delay(hold_time);
return;
}
void servo_seq2(int min_pulse, int max_pulse, int hold_time){
Serial.println("Sequence #2");
int pulse_width_us;
for(pulse_width_us = min_pulse; pulse_width_us <= max_pulse; pulse_width_us += 1){ // goes from 0 degrees to 180 degrees
myservo.writeMicroseconds(pulse_width_us); //
delay(hold_time); //
}
delay(2000);
for(pulse_width_us = max_pulse; pulse_width_us >= min_pulse; pulse_width_us -= 1){ // goes from 180 degrees to 0 degrees
myservo.writeMicroseconds(pulse_width_us); //
delay(hold_time); //
}
delay(2000);
return;
}
void servo_seq3(int min_angle, int max_angle, int hold_time){
Serial.println("Sequence #3");
int servo_angle;
for(servo_angle = min_angle; servo_angle <= max_angle; servo_angle += 1) // goes from 0 degrees to 180 degrees
{
myservo.write(servo_angle);
delay(hold_time);
}
delay(2000);
for(servo_angle = max_angle; servo_angle >= min_angle; servo_angle -= 1) // goes from 180 degrees to 0 degrees
{
myservo.write(servo_angle);
delay(hold_time);
}
delay(2000);
return;
}
void rds3115_angle(int angle){
int pulse_width_us;
float temp_p;
//transform angle to pulse width
temp_p=((angle+45)*7.407)+500; // 2000/270 = 7.407
pulse_width_us= int(temp_p);
myservo.writeMicroseconds(pulse_width_us);
Serial.print("angle: ");
Serial.print(angle);
Serial.print("° ");
Serial.print("pulse_width:");
Serial.print(pulse_width_us);
Serial.println("uS");
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.