Two while loops

OK i am working with Robot C which is a C based program and I was wondering how I should go about my problem. (im in high school) I dont know if this is the way to do it but i have two while loops.

float x=1;

task main()

{

while(x == 1)
{
while(SensorValue[IRSeeker] == 5)
{
motor[motorD] = 25;
motor[motorE] = 25;
motor[motorF] = 25;
motor[motorG] = 25;

if(SensorValue[sonar] < 20)
{
x=2;



}





if(SensorValue[IRSeeker] > 5)
{
motor[motorD] = 20;
motor[motorE] = -20;
motor[motorF] = 20;
motor[motorG] = -20;
}

if(SensorValue[IRSeeker] < 5)
{
motor[motorD] = -20;
motor[motorE] = 20;
motor[motorF] = -20;
motor[motorG] = 20;
}
}

while(x==2)
{
while(SensorValue[sonar] < 20)
{
motor[motorD] = 25;
motor[motorE] = 25;
motor[motorF] = 25;
motor[motorG] = 25;

if (SensorValue[sonar] > 20)
{
x=1.0;

}
}
}
}

If you are lost by now and dont understand what I am trying to do then i will tell you. I am trying to have my program run my first while loop (while x==1) but if the sensor reads < 20 then x should equal 2 and should skip two my secound while loop (x==2) I am not sure if this is how you do this but i dont see why not...so please help
There's a problem with unmatched braces here, a '}' is missing.
I'm not sure where it should go, as there's no indentation as a guide.

Please use code tags when sharing your code, and proper indentation to show the structure. [code] your code here [/code] using the <> formatting button on the right.

Also, task is not a valid keyword, the function should be defined as

1
2
3
4
5
6
7
int main()
{

    // your code here

    return 0;
}

Robot C is a bit different and i have realized im on a C++ site but the robot C people are all my age and dont know what they are doing. Im more worried about the program it self. The first while loop goes from line 16 to line 51 i dont see where your talking about.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
float x=1;

task main()

{
	
  while(x == 1)
  {
    while(SensorValue[IRSeeker] == 5)
    {
      motor[motorD] = 25;
      motor[motorE] = 25;
      motor[motorF] = 25;
      motor[motorG] = 25;
	if(SensorValue[sonar] < 20)
    {
   x=2;
      
   
  }
  }
 
 



    if(SensorValue[IRSeeker] > 5)
    {
      motor[motorD] = 20;
      motor[motorE] = -20;
      motor[motorF] = 20;
      motor[motorG] = -20;
    }

    if(SensorValue[IRSeeker] < 5)
    {
      motor[motorD] = -20;
      motor[motorE] = 20;
      motor[motorF] = -20;
      motor[motorG] = 20;
    }
  }

  while(x==2)
  	{
	while(SensorValue[sonar] < 20)
    {
      motor[motorD] = 25;
      motor[motorE] = 25;
      motor[motorF] = 25;
      motor[motorG] = 25;
      
    if (SensorValue[sonar] > 20)
     {
       x=1.0;

     }
   }
 }
}

}
The first while loop goes from line 16 to line 51 i dont see where your talking about.
this seems to be just arbitrary line number. No while loop starts or ends there.

why did you make x float? Due to inaccuracy the comparison might not work
im sorry i was looking at my program. It starts at 7 and ends at 42. I want the program to change from line 7 while loop and line 44. I made x a float because i tried making it a variable and an integer but it didnt even work remotely. When i am in the debugger it show that x will change but it wont run the lines from line 45 to 57.
I made x a float because i tried making it a variable and an integer but it didnt even work remotely
Nah, that makes the whole thing worse. Switch back to local variable and int.

insert break; after line 17 and it should work.
I know floats are more for decimals but i figured i didnt want x to be a set number per say but i dont know im at a lost so i tried it.
Last edited on
ha ok well thanks you guys are very understanding and nice thanks very much ill try in my robotics class later today. O what do you mean do break?
Other than the possible problem testing a float value (though comparing it with an integer such as 1.0 or 2.0 should be ok), the code looks reasonable.

However, you also have an inner while loop from lines 9 to 21.
while(SensorValue[IRSeeker] == 5) does this condition ever change, or might the program remain within this loop?
O what do you mean do break?
The solution of your problem...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    while(SensorValue[IRSeeker] == 5)
    {
      motor[motorD] = 25;
      motor[motorE] = 25;
      motor[motorF] = 25;
      motor[motorG] = 25;
	if(SensorValue[sonar] < 20)
    {
   x=2;
   break; // this keyword ends the while above
      
   
  }
  }


I know floats are more for decimals but i figured i didnt want x to be a set number per say but i dont know im at a lost so i tried it.
That was a shot in the pitch black. It didn't work so undo
That was a shot in the pitch black. It didn't work so undo
it was a shot in the dark but wouldn't the break end the while and it wouldnt come back?


does this condition ever change, or might the program remain within this loop?

This condition doesnt change that i know of but i could be wrong
just use a debugger and set some breakpoints and you can answer all these questions on your own

learning how to use a debugger is essential anyways
Last edited on
just use a debugger and set some breakpoints and you can answer all these questions on your own

learning how to use a debugger is essential anyways

I have used the debugger and it shows the x going to 1 and 2 but not changing the program. So i figured it was programming error
Last edited on
it was a shot in the dark but wouldn't the break end the while and it wouldnt come back?
No, what you think of is the function exit(), break; in this case just exits from the enclosing while not more

@Darkmaster
debugging in this case is somewhat difficult due to the real time requirement.
No, what you think of is the function exit(), break; in this case just exits from the enclosing while not more
o ok ya sorry ha and cool this should help thanks so much
Topic archived. No new replies allowed.