Please follow the forum guidelines and post in code tags next time.
//PROGRESS LOG
// This is a copy of2aspectonedirectiontimerMarch72020
// This now compiles 26 dec 2019.
// Only one block, but yellow is included.. Not sure what POWER does (Provides power to signal that faces the right way)
//Will butcher by eliminating yellow and bi-direction 4 March 20
// OMG!! It actually,,, B... well works!!!!
// Wii now add the second block... Done AM 5 March.. (g) It compliles and block f still functions
//Now to update breadboard! Whoops PM Block G works, Block f doesn't, LEDs reversed?
//AMFriday 6th MARCH 2020 All functions correctly as far as basic block detection and occupancy goes
//March7,add timer in guise of case yellow and add yellow LEDs for diagnostic purposes only
//March 8 Didn't work compile error green1. Will comment out timer and see.
//March 8 PM works as far as the yellow, but rset of program for block g not yet done.
// March 9 AM tried adding extra 'if' to yellow sub routine.. No change, still hangs on yellow
//March 9th 1610 hrs SHAZAM!!!! allseems to work as planned.. Have lefyblock f with 5 Secs and blockg with 2 secs
//March 10 this will be used for adding loop point function
//I note that pinmodes for analog IPs are missing.. will add and check Inputs
//March 11 Will add loop point function to block G
// PM March 11.. Loop mod not working
//Mar 12 Will try analog inputs for pointLever
//<code>
int fEntry = A0; //Analog sensors
int fExit = A1;
int gEntry = A2;
int gExit = A3;
int pointLever = A4; // temp jumper
int gantrytwoHome = 3;
// Outputs
int fYellow = 5;// 2 secs delay yellow LED
int fDanger = 8;//red LED
int fCaution = 7; //note caution is actually a green LED
int gYellow = 6; // 2 secs delay yellow LED
int gDanger = 9;//red LED
int gCaution = 10;//Green LED
int callingOn = 11; // activates signal to loop. temp blue LED
void setup() {
Serial.begin(9600);
pinMode(fEntry, INPUT);
pinMode(fExit, INPUT);
pinMode(gEntry, INPUT);
pinMode(gExit, INPUT);
pinMode(pointLever, INPUT);
pinMode(gantrytwoHome, INPUT);
pinMode(fDanger, OUTPUT);
pinMode(fCaution, OUTPUT);
pinMode(gDanger, OUTPUT);
pinMode(gCaution, OUTPUT);
pinMode(fYellow, OUTPUT);
pinMode(gYellow, OUTPUT);
pinMode(callingOn, OUTPUT);
}
enum BLOCKSTATESF
{
ST_FUNOCCUPIED,
ST_FOCCUPIED,
ST_FYELLOW,
};
enum BLOCKSTATESG
{
ST_GLOOPSET,
ST_GUNOCCUPIED,
ST_GOCCUPIED,
ST_GYELLOW,
};
BLOCKSTATESF signalStatef = ST_FUNOCCUPIED;
BLOCKSTATESG signalStateg = ST_GUNOCCUPIED;
static unsigned long currentTime;
// This is the main loop containing the switch/ case commands
void loop() { // this is the main loop
//Get current time
currentTime = millis();
int valA1 = analogRead(fEntry);
int valA2 = analogRead(fExit);
int valA3 = analogRead(gEntry);
int valA4 = analogRead(gExit);
int valA5 = analogRead(pointLever);
int valA6 = digitalRead(gantrytwoHome);
// Serial.println(valA1);
// Serial.println(valA2);
// Serial.println(valA3);
// Serial.println(valA4);
Serial.println(valA5);
delay(200);
switch (signalStatef)
{
case ST_FUNOCCUPIED:
signalgreen(valA1, valA2);
break;
case ST_FOCCUPIED:
signalred(valA1, valA2);
break;
case ST_FYELLOW:
signalyellow(valA1, valA2);
break;
}
switch (signalStateg)
{
case ST_GLOOPSET:
pointset(valA5);
break;
case ST_GUNOCCUPIED:
signalgreen1(valA3, valA4);
break;
case ST_GOCCUPIED:
signalred1(valA3, valA4);
break;
case ST_GYELLOW:
signalyellow1(valA3, valA4);
break;
}
}
// These are the sub routines associated with each case..
//Block F
void signalgreen(int valA1, int valA2) { // Sets green (so called caution) LED if neither sensor is detecting.
digitalWrite(fCaution, HIGH);
digitalWrite(fDanger, LOW);
digitalWrite(fYellow, LOW);
if (valA1 < 500 && valA2 > 500) { // Sets Occupied state if fEntry detects but not fExit
signalStatef = ST_FOCCUPIED;
}
}
void signalred(int valA1, int valA2) { // OCCUPIED turns green led off but red on
digitalWrite(fCaution, LOW);
digitalWrite(fDanger, HIGH);
digitalWrite(fYellow, LOW);
if (valA1 > 500 && valA2 < 500) { // train is leaving block
signalStatef = ST_FYELLOW;
}
}
void signalyellow(int valA1, int valA2) { //sets delay
const unsigned long duration = 5000;
static unsigned long startTime = 0;
if (startTime == 0) {
digitalWrite(fCaution, LOW);
digitalWrite(fDanger, LOW);
digitalWrite(fYellow, HIGH);
startTime = currentTime;
return;
}
if (currentTime - startTime >= duration) {
startTime = 0;
if (valA1 > 500 && valA2 > 500) { // train has left block
signalStatef = ST_FUNOCCUPIED;
}
}
}
//good up to here!
//Block G
void signalgreen1(int valA3, int valA4) { // Sets green LED if neither sensor is detecting.
digitalWrite(gCaution, HIGH);
digitalWrite(gDanger, LOW);
digitalWrite(gYellow, LOW);
if (valA3 < 500 && valA4 > 500) { // Sets Occupied state if gEntry detects but not fExit
signalStateg = ST_GOCCUPIED;
}
}
void signalred1(int valA3, int valA4) { // OCCUPIED turns all other leds off but red on
digitalWrite(gCaution, LOW);
digitalWrite(gDanger, HIGH);
digitalWrite(gYellow, LOW);
if (valA3 > 500 && valA4 < 500) { // train is leaving block
signalStateg = ST_GYELLOW;
}
}
void signalyellow1(int valA3, int valA4) { //sets delay
const unsigned long duration = 2000;
static unsigned long startTime = 0;
if (startTime == 0) {
digitalWrite(gCaution, LOW);
digitalWrite(gDanger, LOW);
digitalWrite(gYellow, HIGH);
startTime = currentTime;
return;
}
if (currentTime - startTime >= duration) {
startTime = 0;
if (valA3 > 500 && valA4 > 500) { // train has left block
signalStateg = ST_GUNOCCUPIED;
}
}
}
void pointset(int valA5) {
digitalWrite(gCaution, LOW);
digitalWrite(gDanger, HIGH);
digitalWrite(gYellow, LOW);
digitalWrite(callingOn, HIGH); // Servo needs low to activate
if (valA5 >= 900) { /// Loop point is switched to loop not main
signalStateg = ST_GLOOPSET;
// delay(6000);
digitalWrite(callingOn, HIGH);
}
}
// </code>
Regarding the analog point sensor, have you tried writing a simple test sketch that only does that, so you can rule out programming errors in the greater program, or hardware problems?