Hello all my name is Casey Hafeman. I have been struggling with this all night, and not sure how to attempt this anymore. I have the code working right when input P1^0 or P1^1 is true (1). But, I would like to add somewhere inside the code where when both inputs (P1^0, and P1^1 go false (0) there is a time delay of 5-10 seconds that it keeps cycling through the loop. Then finally turns completely off.
//Railroad Warning Light (With Lights and Bell) Source Code #include <reg51.h> void MSDelay (unsigned int); void MSDelay2 (unsigned int); sbit input = P1^0; sbit input2 = P1^1; sbit LLight = P0^1; sbit RLight = P0^2; sbit Bell = P0^4; void main(void) { unsigned int i; input = 0x00; //'input' As Input Port input2 = 0x00; //'input2' As Input Port LLight = 0xFF; //'LLight' As Output Port RLight = 0xFF; //'RLight' As Output Port Bell = 0xFF; //'Bell' As Output Port { LLight = 0; RLight = 0; Bell = 0; i=0; } for (;;) { if (input==1|input2==1) { MSDelay2 (1250); } LLight = 0; RLight = 0; Bell = 0; } } void MSDelay (unsigned int itime) { unsigned int i, j; for (i = 0; i < itime; i++) for(j = 0; j < 1275; j++); } void MSDelay2 (unsigned int itime) { unsigned int i; for (i = 0; i < itime; i++); { { Bell = 1; LLight = 1; RLight = 0; MSDelay (1250); } { Bell = 1; LLight = 0; RLight = 1; MSDelay (1250); } } }
I don't know if anyone has seen this thread yet, but I did go through the code and deleted some parts of the code that were not needed it made it easier to read and less confusing (i hope)
...still looking to have that delay...
but here is the updated code thus far...
#include <reg51.h> void MSDelay (unsigned int); void MSDelay2 (unsigned int); sbit input = P1^0; sbit input2 = P1^1; sbit LLight = P0^1; sbit RLight = P0^2; sbit Bell = P0^4; void main(void) { for (;;) { if (input==1|input2==1) { MSDelay2 (1250); } LLight = 0; RLight = 0; Bell = 0; } } void MSDelay (unsigned int itime) { unsigned int i, j; for (i = 0; i < itime; i++) for(j = 0; j < 1275; j++); } void MSDelay2 (unsigned int itime) { unsigned int i; for (i = 0; i < itime; i++); { { Bell = 1; LLight = 1; RLight = 0; MSDelay (1250); } { Bell = 1; LLight = 0; RLight = 1; MSDelay (1250); } } }
made it easier to read
replace the tabs with spaces and you have it
It's a forum, there is no guarantee of a response.
So you'd probably want to check the pin status as you iterate the loop, and then set a timeout value, and decrement that with each loop, and when that then expires leave.
In the if() you want to use the || form of OR, not the | form
I understand that's it's a forum, and there may be a lack of response. But thank you :-)
And, I'm sorry bud, but the only thing I understand out of all that is 'In the if() you want to use the || form of OR, not the | form' LOL...Would you mind giving me an example?
thank you I appreciate the critique, and i will keep that in mind for the next time. Thank you.
void MSDelay2 (unsigned int itime) { unsigned int i; for (i = 0; i < itime; i++); { // PROBE THE PIN STATE HERE, AND ADJUST ITIME, SO IT NOW EXITS "N" TIME UNITS LATER { Bell = 1; LLight = 1; RLight = 0; MSDelay (1250); } { Bell = 1; LLight = 0; RLight = 1; MSDelay (1250); } } }