//Servo_for_Points.ino Test or application file#include "Servo_for_Points.h"
Servo_for_Points servo1(0,375,200);Servo_for_Points servo2(1,270,400);Servo_for_Points servo3(2,350,200);Servo_for_Points servo4(3,270,370); Servo_for_Points servo5(4,350,240);Servo_for_Points servo6(5,450,225);Servo_for_Points servo7(6,170,310);Servo_for_Points servo8(7,150,400); Servo_for_Points servo9(8,200,350);Servo_for_Points servo10(9,250,350);Servo_for_Points servo11(10,150,350);Servo_for_Points servo12(11,350,250);Servo_for_Points servo13(12,250,350);Servo_for_Points servo14(13,250,350);Servo_for_Points servo15(14,250,350);Servo_for_Points servo16(15,250,350);
void setup() { // put your setup code here, to run once: servo1.begin( ); servo2.begin( ); servo3.begin( ); servo4.begin( ); servo5.begin( ); servo6.begin( ); servo7.begin( ); servo8.begin( ); servo9.begin( ); servo10.begin( ); servo11.begin( ); servo12.begin( ); servo13.begin( ); servo14.begin( ); servo15.begin( ); servo16.begin( ); Serial.begin(115200); Serial.println("Coal_Basin_Servo:Program to test servos"); Serial.println("Reduce timings servo arm will rotate more clockwise."); Serial.println("Increase timings servo arm will rotate more anti-clockwise"); Serial.println(" #define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates");Serial.println("By experiment range 150 to 500 - centre 325");
}
void loop() { Serial.print("\nServo1 :"); servo1.do_servo(LEFT); servo8.do_servo(LEFT); delay(1000); servo1.do_servo(RIGHT); servo8.do_servo(RIGHT); delay(1000); }
-------------------------------------------//Servo_for_Points.cpp Implementation file#include "Servo_for_Points.h"
#include <Wire.h>#include <Adafruit_PWMServoDriver.h>Adafruit_PWMServoDriver pwm(0x40);
Servo_for_Points::Servo_for_Points(int channel, int left_time, int right_time )
{ _channel= channel; _left_time = left_time; _right_time = right_time; current_pos= LEFT; };
void Servo_for_Points::begin ( ){ pwm.begin( ); pwm.setPWMFreq(50); do_servo(RIGHT); } void Servo_for_Points::do_servo(int dir ){ if (dir == current_pos) return; //no change necessary if (dir==LEFT){Serial.print(_left_time); Serial.print(" LEFT ");} else {Serial.print(_right_time); Serial.print(" RIGHT");} if (dir == LEFT) pwm.setPWM(_channel,0,_left_time); else pwm.setPWM(_channel,0,_right_time); //delay(30); //allows 2 pulses //statements added after testing on multiple servos long start = millis( ); while (millis( ) < start +100){ }; pwm.setPWM(_channel,0,0); current_pos = dir; }
-----------------------------------------------//Servo_for_Points.h Header File#ifndef SERVO_FOR_POINTS_H#define SERVO_FOR_POINTS_H#define LEFT 0#define RIGHT 1
#include "Arduino.h"
class Servo_for_Points { public: Servo_for_Points(int ch_channel, int left_time, int right_time); void begin( ); void do_servo(int dir); int current_pos; private: int _channel; //function servo to control int _left_time; int _right_time; //servo settings };#endif
--------------------------------------------