//DCC_Points_Control.ino --application or client code#include <DCC_Probe.h>
#include "DCC_Points_Control.h"DCC_Points_Control point1(1,0,140,200,8); //these are the values from by trial and error for my layoutDCC_Points_Control point2(1,1,180,270,9);DCC_Points_Control point3(3,2,140,240,10); DCC_Points_Control point4(4,3,180,250,11);DCC_Points_Control point5(5,4,200,280,12);DCC_Points_Control point6(6,5,260,180,13);DCC_Points_Control point7(7,6,250,140,14);DCC_Points_Control point8(8,7,260,180,15); #define LOCO 3DCC_Probe DCC; void setup() { DCC.begin(); point1.begin( ); point2.begin( ); point3.begin( ); point4.begin( ); point5.begin( ); point6.begin( ); point7.begin( ); point8.begin( ); pinMode(12,OUTPUT); //timing only pinMode(A6,INPUT_PULLUP);}void loop() { /* point1.op(1); //testing only point2.op(1); //testing only point3.op(1); //testing only point4.op(1); //testing only point5.op(1); //testing only point6.op(1); //testing only point7.op(1); //testing only point8.op(1); //testing only delay(500); point1.op(0); point2.op(0); point3 .op(0); point4.op(0); point5.op(0); point6.op(0); point7.op(0); point8.op(0); delay(500);*/ if (analogRead(A6)>512) { point1.centre( ); point2.centre( ); point3.centre( ); point4.centre( ); point5.centre( ); point6.centre( ); point7.centre( ); point8.centre( ); } else { digitalWrite(12,HIGH); //Time performance point1.op(DCC.DCC_function(LOCO,point1.the_fn( ))); point2.op(DCC.DCC_function(LOCO,point2.the_fn( ))); point3.op(DCC.DCC_function(LOCO,point3.the_fn( ))); point4.op(DCC.DCC_function(LOCO,point4.the_fn( ))); point5.op(DCC.DCC_function(LOCO,point5.the_fn( ))); point6.op(DCC.DCC_function(LOCO,point6.the_fn( ))); point7.op(DCC.DCC_function(LOCO,point7.the_fn( ))); point8.op(DCC.DCC_function(LOCO,point8.the_fn( ))); digitalWrite(12,LOW); delay(1); } }------------------------
//Implementation Code#include "DCC_Points_Control.h"
#include <Wire.h>#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm(0x40);
DCC_Points_Control::DCC_Points_Control( int fn, int ch, int str, int turn, int sig){ _fn= fn; _ch= ch; _str = str; _turn = turn; _sig = sig; };
void DCC_Points_Control::begin ( ){ pinMode(13,OUTPUT); pinMode(A6,INPUT_PULLUP); pwm.begin( ); pwm.setPWMFreq(50); }
int DCC_Points_Control::the_fn ( ){ return _fn; } void DCC_Points_Control::centre( ){ pwm.setPWM(_ch,0,(_str+_turn)/2); } void DCC_Points_Control::op(int con){ digitalWrite(13,con); if (con == 1) { pwm.setPWM(_ch,0,_str); pwm.setPWM(_sig,0,0); } else {pwm.setPWM(_ch,0,_turn); pwm.setPWM(_sig,4096,0); } }-----------------------------//DCC_Points_Control.h Header File#ifndef DCC_POINTS_CONTROL_H#define DCC_POINTS_CONTROL_H#include "Arduino.h"class DCC_Points_Control { public: DCC_Points_Control(int fn, int ch, int str, int turn, int sig); void begin( ); int the_fn( ); void op(int con); void centre( ); private: int _fn; int _ch; //the function and servo to control int _str; int _turn; //servo settings int _sig; //signal};#endif