0 Members and 1 Guest are viewing this topic.
I actually have 8 photocells installed evenly around the loop, but only working with two until the script gets going. I chose two that are farther apart while writing/testing, which should provide much higher precision in the calculation.
I actually have 8 photocells installed evenly around the loop, but only working with two until the script gets going. I chose two that are farther apart while writing/testing, which should provide much higher precision in the calculation. Yep, photocell voltage dividers. The idea behind the new sketch I'm writing is to 1st only check if sensor 1 is triggered. When it is, log the millis, then we check for sensor 2. Once sensor two is triggered, record millis and calculate speed. I'm hoping that this is efficient, by not reading sensor 2 unless sensor 1 is already triggered and millis logged we should get the most accurate timing for the reading. Then we ignore sensor one while we read sensor 2. This does mean if sensor 1 is cleared before sensor 2 is read, we reset. When fully operational, the sensors will be about the width of a locomotive apart, so this won't be an issue, but for now we need to have a train longer than the distance between the sensors.The readings I get when the sensor is vacant is 700, when occupied, between 150-450 (150 under bogies, ~450 mid car). This is soft top lighting. Eventually, I'll add a timer to average the reading over a few seconds (after the initial trigger) which will make the "continue warning" and end of train detection more reliable.
if (E1Occupied = true){
//Sensor Pinsint senseE1 = 0;int senseE2 = 1;int sensorDistance = 63; // distance in Centimeters.//Sensor Readingsint readE1, readE2;//Timerslong timeE1;long timeE2;long currentTime;long printTimer;long printInterval = 1000;//Statusboolean E1Occupied = false;boolean E2Occupied = false;int Speed = 0;void setup(){ Serial.begin(9600); }void loop(){ readE1 = (analogRead(senseE1));currentTime = millis();// If E1 IS triggeredif (readE1 < 600){ // If E1 IS already occupied, read E2 if (E1Occupied == true){ readE2 = (analogRead(senseE2)); // If E2 IS triggered if(readE2 < 600){ // If E2 IS already occupied, calculate speed if (E2Occupied == true){ // Calculate speed between E1 and E2 if(Speed == 0){ Serial.println("Calculate Speed."); Speed = ((sensorDistance *160/100*.00062137)/((timeE2 - timeE1)/1000.00000000*.00027777)); Serial.print("Speed = "); Serial.println(Speed); } } // IF E2 is NOT already occupied, set time of trigger and mark as occupied else{ timeE2 = millis(); E2Occupied = true; Serial.println("E2 is Occupied"); Serial.print("timeE2 = "); Serial.println(timeE2); } } } else{ // If E1 is NOT already occupied, set time of trigger and mark as occupied timeE1 = millis(); E1Occupied = true; Serial.println("E1 is Occupied"); Serial.print("timeE1 = "); Serial.println(timeE1); } } // If E1 is NOT triggered else{ // Reset E1 and E2 to Vacant timeE1 = 0; E1Occupied = false; timeE2 = 0; readE2 = 0; E2Occupied = false; Speed = 0; }}
Well, the double equal sign is not intuitive.
Welp, that was a waste of a week. Finally figured out the issue.Programmers will know how dumb I am. For non-programmers, this if statement checks to see if variable "E1Occupied" is equal to "true"... except the syntax is wrong. E1Occupied = true is a command to SET E1Occupied to true.it needs to be E1Occupied == true to CHECK if it is already true.