Re: having trouble with sonar not updating
The sonar check is only happening at the beginning of each loop because that's the only place where you've programmed the robot to do the check.
If you would like the check to happen continuously while the search behavior is also happening, the simplest approach would probably be to have two tasks running simultaneously
, one checking the sonar and the other doing the search behavior. When the sonar task finds the opponent, it stops the search task.
The other approach would be to include the sonar condition in each of the internal while loops as well, so they only continue if they haven't completed the movement and
the opponent hasn't been found. That way, if the opponent is located, any of the movement behaviors will be immediately stopped, and the others will be skipped until the end of the outer loop, which will also detect the presence of the opponent, and exit the entire behavior.