How It Works
The code for making a working dancing robot is actually not too difficult to make. The way that I went about making the robot dance was by using functions to make it do more complicated moves and then placing them in If/Else statements that check if it is too close to a wall. The Check function itself is a modified version of Follow 2D which only checks if it is too close to the wall and then backs away from it to give it a distance to dance more. This check statement is done in a two-fold way, one by checking its first distance. After checking the first distance it stores that and begins to back away at other angles if the smallest distance at the moment is smaller than the most recently checked distance. If the check statement is too close to a wall it will back away and return true. If it is not too close it will continue on to do the move itself and have not wasted any time in the check statement. The reason that it does this is to help it stay on time with the music.
Below is a video of the code running without any music.
As mentioned above this code builds off of the Follow 2D code and runs it when it gets too close to a wall but allows for it to do other things on its own dancing. When it is running the functions for squares it also runs it in between each movement to help it run better. All in all, this program builds off of Follow 2D but makes its own twists to make the robot able to do what it needs on its own.
The only issue I can see with how I do this is that unlike in a dance, if the robot was doing tasks, it might not want to skip steps if it was running into issues, this could be fixed easily however by just having it run checks and then run the code after a check.