That would be the case if you didn't account for both states of the sensor, but the logic table accounts for all states for both sensors. So unless there are exceptions, the on/off state should be accounted for in each case. You mentioned the code in post 6, which if I recall reverses the subroutines from the original code. However the moment a sensor is activated the code would do exactly as it did before the swap. It also ignores a sensor while in each loop. One loop ignores sensor1 while the other ignores sensor2. Because of this you can't be dependant on sensor2 for anything once you're in the second loop.
With those conditions Hal's code should work. And that assumes you do wish to ignore the other sensor in each case as described above.
Yes, i would like to ignore the other sensor in each case.
But, I also want default ON for motor and led when plug and battery is pulled out of AC outlet and out of Homework Board, and put back in.
Chris;
The only purpose in reversing the two subroutines was to establish a "motor on" condition at startup. I do wholeheartedly agree that in any good program all possible sensor states need to be considered. However, it does appear that this particular application behaves more like a simple state machine. Perhaps within each state the status of both sensors could be accounted for in the event that the orderly flow of the trains is disrupted by a curious child or pet.
KylePKing I am curious if you're still getting the "1/2 power" issue as that is a sign the code is jumping back and forth between both routines really fast, which is not the ideal solution.
In that case Hal is correct that a state machine is in order. In a state machine you could set a flag until a condition was met on one sensor while maintaining your loop, however in all the descriptions I never got a clear indication of how that should work. So if it is working right that is great, however if it is jumping back and forth at some points there is a problem, it just looks like 1/2 power.
You need to modify Start: so that the jump to loop occurs when sensor 2 returns to a 0. Something like this:
start:
HIGH motor
HIGH led
IF sensor2 = 1 THEN start1
GOTO start
start1:
IF sensor2 = 0 then loop
goto start1
You might have to do something similar to the loop: routine.
Also, you could time how long it takes the train to pass the sensor and insert an appropriate pause. That would prevent gaps in the train from pulsing the sensor.
Great to hear everything is working as planned. Perhaps you could show us a picture or two of the whole project, sounds like you put a lot of time into it. I would like to see what the whole thing looks like.
Comments
so that it works?
Yes, i would like to ignore the other sensor in each case.
But, I also want default ON for motor and led when plug and battery is pulled out of AC outlet and out of Homework Board, and put back in.
' {$PBASIC 2.0}
'Programmed in PBasic
sensor1 VAR IN0
sensor2 VAR IN15
motor CON 8
led CON 7
INPUT 0
INPUT 15
OUTPUT 8
OUTPUT 7
start:
HIGH motor
HIGH led
IF sensor2 = 1 THEN loop
GOTO start
loop:
LOW motor
LOW led
IF sensor1 = 1 THEN start
GOTO loop
END
Motor is default ON.
But, as sensor2 is interrupted, LED dims a little, and POWER is half-On? Once sensor2 is not interrupted, normal operation ON.
The only purpose in reversing the two subroutines was to establish a "motor on" condition at startup. I do wholeheartedly agree that in any good program all possible sensor states need to be considered. However, it does appear that this particular application behaves more like a simple state machine. Perhaps within each state the status of both sensors could be accounted for in the event that the orderly flow of the trains is disrupted by a curious child or pet.
It finally works correctly.
Here is the code:
' {$STAMP BS2}
' {$PBASIC 2.0}
'Programmed in PBasic
sensor1 VAR IN0
sensor2 VAR IN15
motor CON 8
led CON 7
INPUT 0
INPUT 15
OUTPUT 8
OUTPUT 7
start:
HIGH motor
HIGH led
IF sensor2 = 1 THEN loop
GOTO start
loop:
LOW motor
LOW led
IF sensor1 = 1 THEN start
GOTO loop
END
I really appreciate all of the time that you two have spent helping me with this.
Thank you very much.
Project Complete!
In that case Hal is correct that a state machine is in order. In a state machine you could set a flag until a condition was met on one sensor while maintaining your loop, however in all the descriptions I never got a clear indication of how that should work. So if it is working right that is great, however if it is jumping back and forth at some points there is a problem, it just looks like 1/2 power.
You might have to do something similar to the loop: routine.
Also, you could time how long it takes the train to pass the sensor and insert an appropriate pause. That would prevent gaps in the train from pulsing the sensor.
The 1/2 dim light is fixed.
It did this because the two IR sensor pairs were not perfectly lined up. I corrected this.
The code works!
The motor default is on!
The operation performs as desired!
Thank you Hal and Chris.