Change motor direction on certain points

Dear everyone.

We are building a robot and I have a question about the programming logic.

I want the program to do the following: 1. move the robot forward until having contact with the reed A (giving me a digital 1 if hit, but only while having contact) 2. then I want to move it backward until having contact with reed B 3. And then finally I want it to move forward again for a defined time.

I have defined the following functions for the motor. motorAn(); //motor on motorVor(); //move forward motorBremse(); //break motorRueck(); //move backward motorAus(); //motor off

However, I am struggeling with the programming logic of how I can make it move the way I want to.

I'd be really thankful if someone could help me with my problem :-)

I'm guessing that the actual problem is this... you reverse the robot when it hits the front switch but then as soon as the switch is cleared it stops?

Use a boolean flag, say frontSwitchHit, initialised false, and set it true when you hit the front switch. Make the reverse decision based on the flag not the switch. Then clear that flag when you hit the back switch.

Similar idea at the back with backSwitchHit.

Very rough outline, but something like that should do the trick, assuming I interpreted the problem correctly.

Thank you very much JimboZA, I’ll try that and will give you a feedback.

If your problem is as Jimbo suggests, and you have not said whether it is or not, then an alternative approach is to change direction when the hit sensor [u]becomes[/u] HIGH rather than when it [u]is[/u] HIGH. Look at the StateChangeDerection example in the IDE to see how to do it.