Simulating a PLC ladder diagram on arduino

ash901226:
there's only one problem with this function of the one i post before,
if let say that i would like to call keep function a couple of time,
any term that use the same term will exhibit the same result.

If you want to use more than 1 keep function, you will need more than 1 "memory bit" variable...

Output1 = keep(Input1, Input2, &M1);
Output2 = keep(Input3, Input4, &M2);

I have to agree with the others here that trying to rewrite ladder to c is not the best way to go... You are making things more difficult than it should be. Everything you want can be solved with some simple bool & integer logic and some if statements. Even the counters you want...

Either use a ladder to c translator as suggested or forget about the ladder structure.

But i understand that you try to stick to ladder though... people tend to forget that programming is only a means to reach a goal and that we're not all programmers in the first place.