// Copyright (C) 2014 Paul T Middlehurst. // Permission is granted to copy, distribute and/or modify this document // under the terms of the GNU Free Documentation License, Version 1.3 // or any later version published by the Free Software Foundation; // with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. // A copy of the license is included in the section entitled "GNU Free Documentation License". // // Half Step Code can be found here: http://forum.arduino.cc/index.php?topic=46605.0;wap2 // // Home Semaphore Signal V0.1 Paul Middlehurst 10-02-14 Beta // V1.0 Paul Middlehurst 01-04-14 Mod. Invert distant output oops! // V1.2 Paul Middlehurst 15-05-14 Mod. HomeChk added to fix 3 1/2" / 4 wheel truck track circuit bounce problems! // V1.3 Paul Middlehurst 25-09-14 Changed to half step. // #include #define STEPS 400 Stepper stepper(STEPS, 2, 3, 4, 5, 1); const int HomeSection = 0; const int Red = 6; const int Green = 7; const int OutToDist = 8; int HomeVal; int HomeOc; int HomeEr; int HomeChk; int IsDown = HIGH; int Steps = 56; // No. of steps between green and red void setup() { pinMode(Red, OUTPUT); pinMode(Green, OUTPUT); pinMode(OutToDist, OUTPUT); stepper.setSpeed(10); lamp_test(); } void lamp_test() { digitalWrite(Red, HIGH); delay(200); digitalWrite(Red, LOW); delay(200); digitalWrite(Red, HIGH); delay(200); digitalWrite(Red, LOW); delay(200); digitalWrite(Green, HIGH); delay(200); digitalWrite(Green, LOW); delay(200); digitalWrite(Green, HIGH); delay(200); digitalWrite(Green, LOW); } void StatusCheck() { if (analogRead(HomeSection) > 682) { HomeEr = HIGH; } else HomeEr = LOW; if (analogRead(HomeSection) < 341) { HomeOc = HIGH; HomeChk = 300; } else { if (HomeOc == HIGH) { HomeChk--; delay(10); } if (HomeChk < 1) { HomeOc = LOW; HomeChk = 300; } } } void HomeError() { while (HomeEr == HIGH) { digitalWrite(Green, LOW); SignalHigh(); digitalWrite (Red, HIGH); delay(500); digitalWrite (Red, LOW); delay(500); digitalWrite(OutToDist, HIGH); // Inverted! StatusCheck(); } } void Home() { while (HomeOc == HIGH) { digitalWrite(Green, LOW); digitalWrite(OutToDist, HIGH); SignalHigh(); digitalWrite(Red, HIGH); StatusCheck(); } if (HomeOc == LOW && HomeEr == LOW) { digitalWrite(Red, LOW); digitalWrite(OutToDist, LOW); SignalLow(); digitalWrite(Green, HIGH); } } void SignalLow() { if (IsDown == HIGH) { stepper.step(-Steps); IsDown = LOW; digitalWrite(2, HIGH); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); } } void SignalHigh() { if (IsDown == LOW) { stepper.step(Steps); IsDown = HIGH; digitalWrite(2, HIGH); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); } } void loop() { StatusCheck(); HomeError(); Home(); }