Hi. Have you programmed using a state machine?. I advice you to look into this way of approach. It uses a switch statement which handles the possible states of the machine. In your case when the robot senses an obstacle the code must set a state corresponding to the event. Lets say:
left sensor sets up left_sensor_state
right sensor sets up right_sensor_state
both sensors sets up both_sensor_state
then your loop() has a function called check_state();
and check_state() has your:
switch (state)
case left_sensor_state
do this
break;
like that with other 2 or 3 cases, or as many as you want.
I've coded using several state machines at the same time and its pretty stable. It feels like pseudomultitasking,.
Remember to always have a none_state kind of state, where your code "waits" for action like nothing occurs during the none_state. Here an example for the definition:
typedef enum
{
STATE_NONE,
STATE_LEFT,
STATE_RIGHT,
STATE_BOTH,
STATE_START,
} states;
states current_state = STATE_START; // current state-machine state
And also remember always after performing the action / maneuver to return the state to STATE_NONE, usually right before the break.
Regards