Programming Trouble

We are having trouble programming to the 5 and 6 channels on our controller. Here is a picture of our code. Any suggestions?

Take a close look at your while loop and how it is set up (you are missing one key part, or rather two key characters). Remember, the while loop typically needs three things to work ‘as intended’:

  • The while keyword
  • A condition, placed inside of a set of parenthesis ()
  • The code to be looped, placed inside a set of curly braces {}

Specifically, your while loop does not have a set of curly braces, so the code has no way of knowing what code should be contained and ‘controlled’ by the loop.

Now the next question is: Why didn’t this generate an error, and why does the tank control part of this program still work? The answer is how C programming treats a control structure that is missing the opening and closing curly braces {}. When this occurs, the control structure will ONLY control the next command, function, or structure that it encounters: in this case, the tankControl() command. To avoid the potential confusion that can be caused by this programming shortcut, I always recommend wrapping the code you want to control by a structure inside a set of curly braces {}.

Since the while loop is infinite, it will infinitely call the tankControl() command but will never actually reach the other commands in your program. To fix this, simply add a set of curly braces to the while loop’s structure and ensure that they ‘wrap’ the entire series of instructions you want that loop to control.

while(1==1)
{

//All of the code to be repeatedly looped should go here

}