Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

why the program did not update its value? (c lannguage)

Status
Not open for further replies.

xeforux

Newbie level 2
Joined
May 1, 2012
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,314
hi, im doing a program to move a robot from (x1,y1) to (x2,y2). the problem is the robot did not update its current location after moved to the next location. here the code:


Code C - [expand]
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
if(S1==0)
     {
              LED1=1;
              robot_x=3;
              robot_y=1;
              goal_x=3;
              goal_y=3;
              new_state=2;
 
 
              while(robot_y!=goal_y && robot_x!=goal_x)
              {          scanningR();   //check for obstacle on right
                         scanningL();    //left
                         scanningC();   //front
 
                         if(new_state==1)
                              check_vertical1(robot_x,robot_y,goal_x,goal_y);
 
                         else if(new_state==2)
                              check_horizontal2(robot_x,robot_y,goal_x,goal_y);
 
                         else if(new_state==3)
                              check_vertical3(robot_x,robot_y,goal_x,goal_y);
 
                         else if(new_state==4)
                              check_horizontal4(robot_x,robot_y,goal_x,goal_y);
 
 
              }
              stop();
              Lcd_Out(1,1,"      GOAL      ");
              Lcd_Out(2,1,"     FOUND      ");
 
     }



the problem is the program immediately jump to "goal found"
kindly help ;-)
 
Last edited by a moderator:

the problem is the program immediately jump to "goal found"

Code:
robot_x=3;
robot_y=1;
goal_x=3;
goal_y=3;
while(robot_y!=goal_y && robot_x!=goal_x)

This means that in order to continue inside while(), both conditions should be true.
First condition is true, because robot_y=1 and goal_y=3.
But second condition is false because robot_x=goal_x=3.
In order to enter inside while(), you must initialize those two variables with different values.

Hope it helped,
Alexandros
 
thanks. its helped a lot.:p but i have another problem, this one is after the robot reach the goal location, (3,3) the program did not exit the while loop..it keeps moving.. here the codes:


Code C - [expand]
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
if(S1==0)
     {
              LED1=1;
              robot_x=3;
              robot_y=1;
              goal_x=3;
              goal_y=3;
              new_state=1;
 
 
              while(robot_y!=goal_y || robot_x!=goal_x)
              {          scanningR();
                         scanningL();
                         scanningC();
 
                         if(new_state==1)
                              check_vertical1(robot_x,robot_y,goal_x,goal_y);
 
                         else if(new_state==2)
                              check_horizontal2(robot_x,robot_y,goal_x,goal_y);
 
                         else if(new_state==3)
                              check_vertical3(robot_x,robot_y,goal_x,goal_y);
 
                         else if(new_state==4)
                              check_horizontal4(robot_x,robot_y,goal_x,goal_y);
              }
              stop();
              Lcd_Out(1,1,"     GOAL      ");
              Lcd_Out(2,1,"     FOUND      ");
 
     }






Code C - [expand]
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
63
64
65
66
67
68
69
70
void check_vertical1(int robot_x, int robot_y, int goal_x, int goal_y)
{    if(robot_y==goal_y)
          check_horizontal1(robot_x,robot_y,goal_x,goal_y);
 
     else if(robot_y<goal_y)
     {    if(sCenter==1)
          {     forward(255,255);
                robot_y=robot_y+1;
                new_state=1;
          }
          else if(sCenter==0 && sRight==1)
               check_horizontal1(robot_x,robot_y,goal_x,goal_y);
          else if(sCenter==0 && sLeft==1)
               check_horizontal1(robot_x,robot_y,goal_x,goal_y);
          else
          {    right_turn(200,200);
               right_turn(200,200);
               new_state=3;
          }
     }
     else if(robot_y>goal_y)
          check_horizontal1(robot_x,robot_y,goal_x,goal_y);
}
 
void check_horizontal1(int robot_x, int robot_y, int goal_x, int goal_y)
{    if(robot_x==goal_x)
     {      if(robot_y==goal_y)
                robot==0;
            else if(robot_y!=goal_y)
            {    if(sRight==1)
                 {    right_turn(200,200);
                      forward(255,255);
                      robot_x=robot_x+1;
                      new_state=2;
                 }
                 else if(sRight==0 && sLeft==1)
                 {    left_turn(200,200);
                      forward(255,255);
                      robot_x=robot_x-1;
                      new_state=4;
                 }
            }
     }
     else if(robot_x < goal_x)
     {      if(sRight==1)
            {    right_turn(200,200);
                 forward(255,255);
                 robot_x=robot_x+1;
                 new_state=2;
            }
            else
            {   right_turn(200,200);
                right_turn(200,200);
                new_state=3;
            }
     }
     else if(robot_x > goal_x)
     {    if(sLeft==1)
          {    left_turn(200,200);
               forward(255,255);
               robot_x=robot_x-1;
               new_state=4;
          }
          else
          {    right_turn(200,200);
               right_turn(200,200);
               new_state=3;
          }
     }
}




i hope im not asking too much..really appreciate it ;-)
 

after the robot reach the goal location, (3,3) the program did not exit the while loop..it keeps moving..
Although this is not the problem in line 28 of the second part of code:
code robot==0; should change into code robot=0;

Now there is a big chance that the problem could be that robot_x, robot_y, goal_x and goal_y are passed as arguments inside functions. But in there, their values are changed locally.
The result would be that the actual values inside while() loop will remain unchanged. You could check this from debugger (if available) to be 100% sure.
It seems that you have two options. First option is to pass them as pointer arguments and so their values will change in while() as well. Or you can get rid of arguments and declare those values as global, if they are not already global.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top