OK so I tried the state detection with mixed results, but I think I am close to getting what I want.
This is V1.5 It is a .5 version because 1.0 was only sensor 1 and worked perfectly. The tram stopped at the sensor, waited 2 seconds and then went on until it got back to the sensor. It didn't always stop at the exact same spot but close enough for what I'm doing. It ran that way for a good 10 minutes no fails. So I added another loop for the second sensor. Here is where things got strange. The tram would stop at sensor 1 correctly (with the nose of the tram as soon as it activated the sensor) wait 2 seconds and continue on to the second stop. It would then stop and stay stopped unless the sensor was cleared. I would push the tram past the sensor and then it would stop at station 1 with the middle of the tram. Next round same thing, push along, clear #2 then it would stop with the rear of the tram, next round it would stop about 6 or so inches away from the stop, and then back to stopping at the nose. Each time I would have to push it along away from sensor 2.
// this constant won't change:
int ena = 3; //Tram Speed
int mr1 = 4; //motor a = +
int mr2 = 5; //motor a = -
int se1 = 7; //IR #1
int se2 = 8; //IR #2
// Variables will change:
int se1State = 1; // current state of sensor 1
int se1LastState = 1; // previous state of sensor 1
int se2State = 1; // current state of sensor 2
int se2LastState = 1; // previous state of sensor 2
void setup() {
pinMode(mr1, OUTPUT);
pinMode(mr2, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(se1, INPUT);
pinMode(se2, INPUT);
analogWrite(ena, 65);
delay(200);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read Sensor 1:
se1State = digitalRead(se1);
// read Sensor 2
se2State = digitalRead(se2);
// compare the se1State to its previous state
if (se1State != se1LastState)
{
if (se1State == 1) {
digitalWrite(mr1, HIGH);
digitalWrite(mr2, LOW);
} else {
digitalWrite(mr1, LOW);
digitalWrite(mr2, LOW);
}
// Delay a little bit to load passengers
delay(2000);
}
{
if (se2State == 1) {
digitalWrite(mr1, HIGH);
digitalWrite(mr2, LOW);
} else {
digitalWrite(mr1, LOW);
digitalWrite(mr2, LOW);
}
// Delay a little bit to load passengers
delay(2000);
}
// save the current state as the last state, for next time through the loop
se1LastState = se1State;
se2LastState = se2State;
}
V1.6
So I looked over this code and noticed a line that se1 had that se2 didn't if (se2State != se2LastState) So I added this exactly how se1's was and this time the tram would not stop at either station even when both were activated. So decided to try the exact oppisite and // out the statements on both the se1 and se2. This caused a compiling error so I // the last 2 lines and it compiled. This time the tram would not move at all.
// this constant won't change:
int ena = 3; //Tram Speed
int mr1 = 4; //motor a = +
int mr2 = 5; //motor a = -
int se1 = 7; //IR #1
int se2 = 8; //IR #2
// Variables will change:
int se1State = 1; // current state of sensor 1
int se1LastState = 1; // previous state of sensor 1
int se2State = 1; // current state of sensor 2
int se2LastState = 1; // previous state of sensor 2
void setup() {
pinMode(mr1, OUTPUT);
pinMode(mr2, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(se1, INPUT);
pinMode(se2, INPUT);
analogWrite(ena, 65);
delay(200);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read Sensor 1:
se1State = digitalRead(se1);
// read Sensor 2
se2State = digitalRead(se2);
// compare the se1State to its previous state
if (se1State != se1LastState)
{
if (se1State == 1) {
digitalWrite(mr1, HIGH);
digitalWrite(mr2, LOW);
} else {
digitalWrite(mr1, LOW);
digitalWrite(mr2, LOW);
}
// Delay a little bit to load passengers
delay(2000);
}
if (se2State != se2LastState)
{
if (se2State == 1) {
digitalWrite(mr1, HIGH);
digitalWrite(mr2, LOW);
} else {
digitalWrite(mr1, LOW);
digitalWrite(mr2, LOW);
}
// Delay a little bit to load passengers
delay(2000);
}
// save the current state as the last state, for next time through the loop
se1LastState = se1State;
se2LastState = se2State;
}
I feel like I'm on the right track here but my lack of coding skills is holding me back. I know millis() will improve my delay function at the station but I'm thinking I'm missing something in the rest of the code that is causing the system to not stop the tram correctly. Could the tram be taking each delay into account somehow after it starts moving again and that is causing it to stop in the wrong spot over and over? It takes the tram less than a second the first time, just over a second by the 3rd time and about 2 seconds the 4th time, then everything resets.
Thanks in advance for everyone's help!