Hmmmm, Looks like the Train has been moved on. I seemd to have lost my last Posting along with it!
A quick rehash;
I have two sets of Servos driving the inside pair and the outside pair of Points.
I have a Relay8 Shield running on I2C, (I've dropped the LCD and am feeding back data to a Laptop using Serial.println)
All events are triggered by Infra Red Photo Interrupters, (as per the Letters in the ASCII Drawing).
I can get either the Outside Points OR the Inside Points OR the Relay8 Shield working but cannot seem to integrate each Block of Working Code into one Sketch.
As requested, here is the Code, some of which is mine, (That I Understand), and the rest is written by a Friend, (whose Code I don't understand).
[code]
/* Controller for On30 Logging Layout
Tested on Layout with EAST & WEST Points set up correctly 07-12-2013 - OK
Flip Flop WEST END & EAST END Servos each time either WEST END or EAST END IR Rx are occluded - OK
Flip Flop YARD Servos each time either 'X' Track or 'Z' Track IR Rx are occluded - NOT OK
Operate RELAY8 Shield for Track Polarity and Track Voltage - OK for "All Relays OFF" at end of void setup Section then Fails
Most Code Compiles OK but all Operation then Fails when Commented Code is un-Commented
*/
#include <Servo.h> // Include the Servo Library Code
Servo WestServoA; // create servo object to control WestServoA. Controls WEST END 'A' POINTS SERVO
Servo EastServoD; // create servo object to control EastServoD. Controls EAST END 'D' POINTS SERVO
Servo YardServoB; // create servo object to control YardServoB. Controls FIDDLE YARD 'B' POINTS SERVO
Servo YardServoC; // create servo object to control YardServoC. Controls FIDDLE YARD 'C' POINTS SERVO
#include "Wire.h"
#define I2C_ADDR 0x20 // 0x20 is the address of the RELAY8 Shield with all Jumpers removed
#define R1 1 //Relay OFF = Track Power OFF, Relay ON = Track Power ON
#define R2 2 //Relay OFF = Track Polarity EAST, Relay ON = Track Polarity WEST
#define R3 4 //Relay OFF = X Track ON, Relay ON = Z Track ON
#define R4 8 // Unused - Yet
#define R5 16 // Unused - Yet
#define R6 32 // Unused - Yet
#define R7 64 // Unused - Yet
#define R8 128 // Unused - Yet
#define Rx2 2 // WEST END & EAST END IR Rx Pair connected to digital pin 2
#define Rx3 3 // FIDDLE YARD IR Rx Pair connected to digital pin 3.
#define left 1
#define right 2
#define yard 1
#define main 2
#define Xtrack 1
#define Ztrack 2
#define WSA_yard 108
#define WSA_main 98
#define ESD_yard 80
#define ESD_main 92
#define YSB_Xtrack 91 //NEW
#define YSB_Ztrack 89 //NEW
#define YSC_Xtrack 89 //NEW
#define YSC_Ztrack 91 //NEW
int val = 0; // val stores state of input pin
boolean state = false;
int end_points = yard;
int yard_points = Xtrack;
void setup() {
Serial.begin(9600);
{
Serial.println(" ** ON30 Logging Layout Points and Polarity Status ** ");
}
Wire.begin(); //Wake up I2C Bus
// Set addressing Style for RELAY8 Shield
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12);
Wire.write(0x20); // Use Table 1.4 addressing
Wire.endTransmission();
// Set I/O bank A to Outputsfor RELAY8 Shield
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x00); // IODIRA register
Wire.write(0x00); // Set all of bank A to Outputs
Wire.endTransmission();
sendValueToLatch(00); // All RELAYs OFF
Serial.println("Track Power Off");
WestServoA.attach(10); // attaches WestServoA to digital pin 10
EastServoD.attach(11); // attaches EastServoD to digital pin 11
YardServoB.attach(12); // attaches YardServoB to digital pin 12
YardServoC.attach(13); // attaches YardServoC to digital pin 13
pinMode(Rx2, INPUT); // initialize the digital pin as an input:
digitalWrite (Rx2, HIGH); // Sets Rx2 HIGH, Negates the need for external Pull-Up Resistor
pinMode(Rx3, INPUT); // initialize the digital pin as an input:
digitalWrite (Rx3, HIGH); // Sets Rx3 HIGH, Negates the need for external Pull-Up Resistor
attachInterrupt(0, sensorRX2, FALLING); // reads sensor on port pin 2. function sensorRX2 is the interrupt handler
attachInterrupt(0, sensorRX3, FALLING); // reads sensor on port pin 3. function sensorRX3 is the interrupt handler
WestServoA.write(WSA_yard); // tell WEST END SERVO to point to YARD
EastServoD.write(ESD_yard); // tell EAST END SERVO to point to YARD
YardServoB.write(YSB_Xtrack); // tell WEST END SERVO to point to X Track
YardServoC.write(YSC_Xtrack); // tell EAST END SERVO to point to X Track
end_points = yard;
yard_points = Xtrack;
Serial.println("Track Polarity West");
Serial.println("End Points to Yard");
Serial.println("Yard Points to 'X' Track");
delay(5000);
sendValueToLatch(R1); // Track Power ON - Starts Layout Operation
Serial.println("Track Power On");
}
void loop()
{
noInterrupts();
if ( state == true)
{
ChangePoints();
state = false;
}
interrupts();
}
void sensorRX2()
{
state = true;
}
void sensorRX3()
{
state = true;
}
void ChangePoints()
{
if (end_points == yard)
{
// sendValueToLatch(00); // All Relays OFF
WestServoA.write(WSA_main); // tell WEST END 'A' SERVO to point to MAIN
EastServoD.write(ESD_main); // tell EAST END 'D' SERVO to point to MAIN
delay(1000);
// sendValueToLatch(00); // All Relays OFF
// sendValueToLatch(R2); // RELAY 2 ON = Polarity chage to WEST
// sendValueToLatch(R1); // RELAY 1 ON = Track Power ON
end_points = right;
Serial.println("End Points to Main");
}
else
{
// sendValueToLatch(00); // All Relays OFF
WestServoA.write(WSA_yard); // tell WEST END 'A' SERVO to point to YARD
EastServoD.write(ESD_yard); // tell EAST END 'D' SERVO to point to YARD
delay(1000);
// RELAY 2 Already OFF = Polarity change to EAST
// sendValueToLatch(R1); // RELAY 1 ON = Track Power ON
end_points = left;
Serial.println("End Points to Yard");
/* // NEW CODE BLOCK
if (yard_points == Xtrack)
{
YardServoB.write(YSB_Ztrack); // tell Yard 'B' SERVO to point to Z Track
YardServoC.write(YSC_Ztrack); // tell Yard 'C' SERVO to point to Z Track
yard_points = right;
Serial.println("Yard Points to 'Z' Track");
}
else
{
YardServoB.write(YSB_Xtrack); // tell Yard 'B' SERVO to point to X Track
YardServoC.write(YSC_Xtrack); // tell Yard 'C' SERVO to point to X Track
yard_points = left;
Serial.println("Yard Points to 'X' Track");
//END OF NEW CODE BLOCK
*/ }
}
void sendValueToLatch(int latchValue)
{
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select GPIOA
Wire.write(latchValue); // Send value to bank A
Wire.endTransmission();
}
[/code]