// 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 // // Distant Semaphore Signal V0.1 Paul Middlehurst 10-02-14 // V1.0 Paul Middlehurst 01-04-14 // V1.1 Paul Middlehurst 25-09-14 Changed to half step. // #include #define STEPS 400 Stepper stepper(STEPS, 2, 3, 4, 5, 1); const int DistantSection = 0; const int Yellow = 6; const int Green = 7; const int OutToDist = 8; int DistantVal; int DistantOc; int DistantEr; int IsDown = HIGH; int Steps = 56; // No. of steps between green and yellow void setup() { pinMode(Yellow, OUTPUT); pinMode(Green, OUTPUT); pinMode(OutToDist, OUTPUT); stepper.setSpeed(10); lamp_test(); } void lamp_test() { digitalWrite(Yellow, HIGH); delay(200); digitalWrite(Yellow, LOW); delay(200); digitalWrite(Yellow, HIGH); delay(200); digitalWrite(Yellow, 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(DistantSection) > 682) { DistantEr = HIGH; } else DistantEr = LOW; if (analogRead(DistantSection) < 341) { DistantOc = HIGH; } else DistantOc = LOW; delay(500); } void DistantError() { while (DistantEr == HIGH) { digitalWrite(Green, LOW); SignalHigh(); digitalWrite (Yellow, HIGH); delay(500); digitalWrite (Yellow, LOW); delay(500); digitalWrite(OutToDist, HIGH); // Inverted! StatusCheck(); } } void Distant() { while (DistantOc == HIGH) { digitalWrite(Green, LOW); digitalWrite(OutToDist, HIGH); SignalHigh(); digitalWrite(Yellow, HIGH); StatusCheck(); } if (DistantOc == LOW && DistantEr == LOW) { digitalWrite(OutToDist, LOW); digitalWrite(Yellow, 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(); DistantError(); Distant(); }