-4

This code is for making a robot in a simulator move in specific paths using C. The code of the while loop is where I need to make changes, I was wondering how to change the code so that the robot moves in a straight line and stop, and how to use 'timed distance' to make it move in a triangle.

while (1) {
    sprintf(buf, "M LR 60 -60\n");
    write(sock, buf, strlen(buf));
    memset(buf, 0, 80);
    read(sock, buf, 80);
}

M LR is the name for the Left and Right motors in the robot(the robot has two motors that make it move) and 60 and -60 are their speeds. This code makes the robot spin.

The full code is in this link: [C Robot]

How to make this code work?

int k = 1;

for (k = 1; k < 3 ; k++){
    sprintf(buf, "M LR 20 10\n");
    write(sock, buf, strlen(buf));
    memset(buf, 0, 80);
    read(sock, buf, 80);
}

for (k=3; k > 3; k++) {
    sprintf(buf, "M LR 0 0\n");
    write(sock, buf, strlen(buf));
    memset(buf, 0, 80);
    read(sock, buf, 80);
}
Ken Kin
  • 4,503
  • 3
  • 38
  • 76
mrahh
  • 281
  • 1
  • 4
  • 13
  • 1
    Your title really doesn't match what your question says. – unwind Feb 01 '13 at 12:23
  • It seems that your question title is incorrect. Do you just want to replace the while loop with a different construct that does that same thing or do you want to achieve something completely different?! – Joe Feb 01 '13 at 12:23
  • With your current for loop : `for (k = 3; k > 3; k++)` => since k is assigned 3 just before entering the loop, the test `k > 3` is false right from the beginning and the loop is simply skipped. Moreover, since the numbers after the "M LR" sequence give the speed to apply to the motors, I guess you should set positive values if you want your robot to move. – Rerito Feb 01 '13 at 12:35

1 Answers1

2
for(;;) {
    sprintf(buf, "M LR 60 -60\n");
    write(sock, buf, strlen(buf));
    memset(buf, 0, 80);
    read(sock, buf, 80);
}

I see no reason to do this.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • 1
    Some compilers (e.g. MSVC++) will warn `while (1)`, but not `for(;;)`. – Alexey Frunze Feb 01 '13 at 12:24
  • int k = 1; for (k = 1; k < 3 ; k++){ sprintf(buf, "M LR 20 10\n"); write(sock, buf, strlen(buf)); memset(buf, 0, 80); read(sock, buf, 80); } for (k=3; k > 3; k++) { sprintf(buf, "M LR 0 0\n"); write(sock, buf, strlen(buf)); memset(buf, 0, 80); read(sock, buf, 80); } – mrahh Feb 01 '13 at 12:25
  • `while(1)` is a legal construct in C though.. why does it warn? – Aniket Inge Feb 01 '13 at 12:25
  • Because the proper way to write an infinite loop is `for(;;)`. (Seriously, the reason probably is that the compiler writers thought one might accidentally mistype the condition, so it's safer to warn. Similar to the warning for the - also legal - `if (x = y)`.) – Daniel Fischer Feb 01 '13 at 14:27
  • @DanielFischer that's the problem with MSVC, it thinks programmers are notoriously lazy.. while in reality most are quite attentive while programming(which is why I sometimes like GCC, you can shoot yourself.. no strings attached) – Aniket Inge Feb 01 '13 at 14:29