Your last post was most instructive. Please find below an updated sketch that should do precisely what you specified. Please note that I have incorporated in the comments all of your directions in the last post as step by step comments below. Try it out, and review the comments. I think they are completely consistent with the code.
will wait looping at this statement until the ReedSensor at the specific pin turns ON
The sketch was checked for correct compilation. If it does not do what you need, go back and change the English (Australian) in your last post and we can go from there.
// Corrimal Tipple Chain Control Circuit ver 4.0
// J. Garaty & G Bunza January 29 2018
// Equipment definitions and pin assignments
//
// Equipment definitions and pin assignments
//
#define ChainHome_pin 2 // D2 ChainHome has a reedswitch to ground yel
#define SkipArrive_pin 4 // D4 SkipArrive has a reedswitch to ground wht
#define Cage_pin 7 // D7 Cage has a reedswitch to ground red
#define KickbackApproach_pin 8 // D8 KicbackApproach has a reedswitch to ground grn
#define KickbackDivert_pin 12 // D12 KickbackDivert has a reedswitch to ground blk
#define CageServo_pin 3 // D3 output CageServo is output to ground for TipServo at ANE SmartSwitch prpl
#define KickbackServo_pin 5 // D5 output KickbackServo is output to ground for KickbackServo at ANE SmartSwitch brn
#define MotorRelay_pin 6 // D6 output MotorRelay is output to ground for a relay gry
// defined servo pins only provide input to ANE SmartSwitch and do not drive servos
// defined MotorRelay pin interrupts supply to tipple chain motor only. Speed control is elsewhere.
// Determine chain magnet location as part of setup;
#define Reed_ON LOW // When a reed switch turns ON it connects to Ground GND setting pin LOW
#define Reed_OFF HIGH // When a reed switch is OFF the input floats open with internal pullup setting pin HIGH
#define Switch_ON LOW // Setting output pin LOW turns relay ON
#define Switch_OFF HIGH // Setting output pin HIGH turns relay OFF
void setup() {
// Set up conditions for power up:
pinMode ( ChainHome_pin, INPUT_PULLUP); //yel
pinMode ( SkipArrive_pin, INPUT_PULLUP); //wht
pinMode ( Cage_pin, INPUT_PULLUP); //red
pinMode ( KickbackApproach_pin, INPUT_PULLUP); //grn
pinMode ( KickbackDivert_pin, INPUT_PULLUP); //blk
pinMode ( CageServo_pin, OUTPUT); // prpl
pinMode ( KickbackServo_pin, OUTPUT); // brn
pinMode ( MotorRelay_pin, OUTPUT); // gry
digitalWrite ( CageServo_pin, Switch_OFF); // Intialize to a known state, CageServo OFF, cage aligned with track
digitalWrite ( KickbackServo_pin, Switch_OFF); // Intialize to a known state, kickbacksServo off, track set to Divert
/*
At powerup - setup is triggered and MotorRelay should trigger the motor moving the chain to run until Chain Home is activated.
*/
while ((digitalRead ( ChainHome_pin) == Reed_OFF)) // Magnet on chain is not at ChainHome
digitalWrite( MotorRelay_pin, Switch_ON); // Keep looping with Motor On until magnet on chain is at ChainHome reedswitch
digitalWrite( MotorRelay_pin, Switch_OFF); // Magnet at ChainHome, breaks while loop, so turn off motor
//setup complete - ready for skips from incline
}
void loop() {
//ChainHome is activated AND SkipArrive is activated by an incoming skip. This should trigger MotorRelay and start the motor moving the chain.
while ((digitalRead ( ChainHome_pin) == Reed_OFF)
|| (digitalRead ( SkipArrive_pin) == Reed_OFF)); // wait
// when we get here ChainHome is ON and SkipArrive in ON
digitalWrite ( MotorRelay_pin, Switch_ON); // Turn motor ON and it stays ON
//As soon as the chain starts to move ChainHome will deactivate but SkipArrive still is activated.
//MotorRelay remains active and the motor moving the chain continues to run.
while (digitalRead ( ChainHome_pin) == Reed_OFF); // When magnet leaves ChainHome deactivates wait for it
// When the magnet on the chain passes SkipArrive and picks up the skip
// Skip Arrive will deactivate but MotorRelay should remain ON and
//the motor moving the chain continues to run, moving the skip along the tpple deck towards the tipple cage.
//The complication starts when the skip arrives at the tipple cage and activates the Cage reedswitch
while (digitalRead ( Cage_pin) == Reed_OFF) ; //wait until magnet on chain arrives at CAGE reedswitch
// We get here when magnet on chain arrives at CAGE reedswitch
//Case 2 - Tip skip at CAGE
// Cage does 2 things:
// stops the chain by turning MotorRelay off for a defined time of 8 seconds
// composed of 2 by 4 second "delay" intervals, then restarts the chain after the 8 seconds times out.
// Start tip - Stop chain and set CageServo_pin LOW to start tipping cycle
// initiates the tip cycle by passing a signal via Pin 3 CageServo to the ANE Switchmaster controlling the tipple Servo.
// The tip cycle commences immediately but takes approx 3.5 seconds for the cage to travel to the "tipped" position.
digitalWrite( MotorRelay_pin, Switch_OFF); // stop motor, prepare to tip skip
digitalWrite ( CageServo_pin, Switch_ON); // Move cage to tipped position, will take 3.5 seconds to get there
//hold cage at Tip position for an additional 0.5 second
delay(4000); // waits for 4 seconds while cage is in motion to and stable at Tip position
//return cage to Home position aligned with track
// At the end of 4 seconds, the CageServo signal is removed.
// This allows the tipple servo to bring the cage back to the home position
// so that the skip will be settled and aligned with the rails by the time the 8 seconds times out
// and the chain restarts by MotorRelay going active again.
digitalWrite( CageServo_pin, Switch_OFF); // Move Cage back to Home position aligned with track, will take 3.5 seconds to get there
//wait to ensure skip and cage are stable and aligned with track for an additional 0.5 second
delay(4000); // waits for 4 seconds while cage is in motion to and stable at Home position aligned with rails
// CAGE reedswitch ignored to prevent re-triggering Tip Cycle
//start chain
digitalWrite( MotorRelay_pin, Switch_ON); // start motor, haul skip to kickback
// The motor continues to run after it is restarted and moves the now empty skip towards the kickback siding
//Case 3 - Kickback points operation
//MotorRelay pin still LOW from previous case. motor continues to run
//empty skip approaches kickback point blades
//set to straight path with KickbackApproach
// When the skip arrives at KickbackApproach Pin 5 triggers Pin 8 which feeds a signal to the ANE Switchmaster servo controlling
while (digitalRead ( KickbackApproach_pin) == Reed_OFF) ; // wait until magnet on chain arrives at KickbackApproach reedswitch
// We get here when magnet on chain arrives at KickbackApproach reedswitch
// the kickback points to set the road to straight. This signal remains on until Kickback divert turns it off.
digitalWrite ( KickbackServo_pin, Switch_ON); // Set track to Straight
//remain set and allow skip to pass Kickback point blades
//MotorRelay pin still ON. motor continues to run
//set to divert path with KickbackDivert
// The ANE SwitchMaster settings for this servo are set at "almost instantaneous" so the blades at the turnout do not need a delay built in like at the tipple cage.
while (digitalRead ( KickbackDivert_pin) == Reed_OFF) ; //wait until magnet on chain arrives at KickbackDivert reedswitch
//we get here when magnet on chain arrives at KickbackDivert reedswitch so KickbackDivert is now ON
digitalWrite ( KickbackServo_pin, Switch_OFF); // Set track to Divert
// MotorRelay pin still ON. motor continues to run
//Case 4 - Chain magnet runs back towards ChainHome after releasing skip
//stop chain when magnet gets to ChainHome
// The motor continues to run until a magnet on the chain reaches ChainHome. This stops the motor by setting Motor Relay to off.
while ((digitalRead ( ChainHome_pin) == Reed_OFF)) ; // wait until Magnet on chain is at ChainHome
// we get here when Magnet on chain is at Magnet on chain is at ChainHome so ChainHome is ON
digitalWrite( MotorRelay_pin, Switch_OFF); // Magnet is at ChainHome, so turn off motor
// This ends the loop so go back and restart at Case 1 above when next skip arrives from the incline.
// The cycle is complete. Nothing can (should) happen until the next skip arrives triggering the AND condition
// of both Chain Home and Skip Arrive being activated.
}