Goddamnit, diebog.
You just aren't getting that you don't pause or loop inside the state machine. For as long as the machine is in PAUSE_BEFORE_ON state, it executes that block of code in the case statement thousands of times a second until something else happens to change the state. This is caused by the fact that the underlying main() calls loop() over and over again, thousands of times a second. You don't pause that, or run a blocking while() statement. If you want the state to reset back to IDLE when the state is PAUSE_BEFORE_ON and the beam becomes unbroken, then that's exactly what you do. You handle that in tha block of code in the switch statement.
case PAUSE_BEFORE_ON:
if(!beamIsBroken()) {
state = IDLE;
start_time = millis();
}
else if (millis() - start_time >= PAUSE_BEFORE_ON_ms) {
activateButton(onPin);
state = ON_DELAY;
start_time = millis();
}
break;
In any case, I'd just like to say to the rest of the board that the reason that I did not implement "if the beam becomes unbroken during the 10 second pause, then reset to IDLE" is that it was not part of the spec I was given.
The spec was:
diebog:
And instead of 2 seconds can we set it for 10 seconds. So once the beam is broke I want it to wait 10 seconds until it initiates the On-Open function.
and later:
diebog:
Once the beam is broken the following sequence happens:
It does nothing for ten seconds correct
Then the on button comes on (grounded) correct
then another 2 seconds 1 second or even half a second would work
then the open button comes on (grounded) correct
then … what? Both buttions are turned off immediately? Correct
And finally:
diebog:
Hi, I also forgot to mention that after the beam is reconnected that a signal needs to be sent to the OPEN button which in turn deactivates the function. Below is how I think it needs to work, I hope its not confusing.
beam breaks--wait 10 seconds--send signal to ON--wait half a second--send signal to OPEN--wait half a second--release/turn off/float both signals--if beam is still broke do nothing--if beam reconnects wait 2 seconds--send ground signal to ON for 1/4 of a second(which deactivates function)--wait for beam to break again
That's what I had to work with. To be perfectly fair, I have had a more confused client. I had to yell at that guy, too. Eventually told him, "look, I think the code will do everything you have said you wanted, have you tried it out?". Turned out, he hadn't. When he finall gave it a go, it worked perfectly.
That's not the case here, however. What's happening here is "Oh, and another thing …".
The thing is, my message to diebog was
--wait for beam to break (IDLE)
--beam breaks
--wait 10 seconds (PAUSE_BEFORE_OPEN)
--send signal to ON
--wait half a second (ON_DELAY)
--send signal to OPEN
--wait half a second (OPEN_DELAY)
--release/turn off/float both signals
--wait for beam to reconnect (WAIT_BEAM)
--wait another 2 seconds (PAUSE_BEFORE_CLOSE)
--send ground signal to ON(which deactivates function)
--wait 1/4 of a second (OFF_DELAY)
--turn off ON
--wait for beam to break again (IDLE)
I can edit my existing sketch to behave as described in this message for $50 payable by PayPal.
Diebog replied (eventually)
I guess I am going to just have to pay you to write this code for me.
And on that basis I proceeded to implement the sketch as agreed. I did so, and posted the message at the start of this unfortunate thread in reply. From there, you can read the rest of the story.
Notice that diebog is, in fact, trying to use my code. Now legally, I placed it in the public domain and people can as they wish. I have no recourse and realistically I am not going to attempt to sue someone in a whole different country for whom fifty bucks is a big deal. That's why I don't bother with attempting to license code I do on these forums - it's pointless. But ethically, diebog should either accept the work and pay me for my code, or write his own.
Now, you'll note that I actually have provided in this message, for free, a modification to my sketch to do the extra asked for. Why? Because I'm an old softie, that's why. But diebog could have gotten it with a lot less time and hassle if he'd nixed the whining about money for a week (adults understand that their problems are not other people's problems) and then paid me like he said he would for what we agreed I would do.