-1

I am programming a robot with C# and using a digitial compass for direction. Problem I am having is when it goes into its turn loop, it doesnt come back out of it. The DragonBoard is my controller I am talking too. How this is supposed to work is given set heading and time, it turns left or right till heading is matched then drives forward for a set amount of time. Problem I am having is it will go forward but when it goes into the turn loop, it stays there, and doesnt return to for loop. Any help would be appreciated.

private void drive(int heading, int time)//going to start from kit
{
    int i;

    for (i = 0; i < time;i++ )
    {
        DragonBoard.Write("w");//go forward

        while (int.Parse(bearingTxt.Text) - 1 > heading)
        {
            DragonBoard.Write("a");//turn left
            break;
        }

        while (int.Parse(bearingTxt.Text) +1 < heading)
        {
            DragonBoard.Write("d");//turn right
            break;
        }
    }

    DragonBoard.Write(" ");

    if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
    {
        listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
        decision();
    }
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 3
    why don't you just use `if`? You seem to be misusing `while` there, the way you have written it, it will never actually loop.. – Karthik T Aug 13 '13 at 01:04
  • 1
    I think you're after `if`.. since you want each one to process each iteration of the `for` loop.. – Simon Whitehead Aug 13 '13 at 01:04
  • To debug your case, you can actually try to use a debugger or simply add prints every where to figure out where and why you are stuck – Karthik T Aug 13 '13 at 01:05
  • I have a background thread running and updating bearingTxt. When they equal it doesnt exit the loop. I even used if loops. – user2510640 Aug 13 '13 at 02:37

2 Answers2

1

It is because your break Exits the "While Loop" not the for loop...and im guessing your while loop only executes once? Why do you need while for that? Try this

 for (i = 0; i < time;i++ )
{
    DragonBoard.Write("w");//go forward

    if (int.Parse(bearingTxt.Text) - 1 > heading)
    {
        DragonBoard.Write("a");//turn left
        break;
    }

    else (int.Parse(bearingTxt.Text) +1 < heading)
    {
        DragonBoard.Write("d");//turn right
        break;
    }
}
TheProvost
  • 1,832
  • 2
  • 16
  • 41
  • Unless ofcourse you have a background thread that updates bearingTxt.Text then i would understand the using of while loop – TheProvost Aug 13 '13 at 01:12
  • bearingTxt is updated in background, just seems to have no effect on loop. I tried if loops before I switched to while loops. Although not in this manner. Ill try and let you know Thanks! – user2510640 Aug 13 '13 at 02:38
  • Thanks for the input...It is still doing the same thing, although with the If statements, and I switched the 'break' to a 'continue' and that seemed to help even more, problem is now is it cycles and cycles, and cycles when it goes into left or right, but when condition changes for third time, it freezes... – user2510640 Aug 14 '13 at 19:43
1

I hope while loop is not needed there; And the break is also not needed incase of if conditions; Modify your code as the below, which should work:

for (i = 0; i < time;i++ )
    {
        DragonBoard.Write("w");//go forward

        if(int.Parse(bearingTxt.Text) - 1 > heading)
            DragonBoard.Write("a");//turn left
        else if(int.Parse(bearingTxt.Text) +1 < heading)
            DragonBoard.Write("d");//turn right
     }
Vidhya Sagar Reddy
  • 1,521
  • 1
  • 13
  • 25