Name That Code!

So I had this crazy idea for a game. Here is how it goes.
A person posts a code (EasyC, MpLAB, etc…) then a person will guess what exactly the person is trying to accomplish. If the person gets it right, they get a chance to post their own code and others guess. But, if in your code you have a mistake like a missing semicolon (:wink: or other syntax errors, and someone catches them, the person who found the errors gets to post their own code.

I will start with a simple one.


#include "Main.h"

void main ( void )
{
      int crash; 

      while ( 1 )
      {
            crash = GetDigitalInput ( 5 ) ;
            if ( crash = 2 )
            {
                  SetPWM ( 1 , 0 ) ;
                  SetPWM ( 2 , 0 ) ;
                  Wait ( 2000 ) ;
                  SetPWM ( 1 , 0 ) ;
                  SetPWM ( 2 , 127 ) ;
                  Wait ( 2000 ) ;
                  SetPWM ( 1 , 0 ) ;
                  SetPWM ( 2 , 0 ) ;
            }
            else
            {
                  SetPWM ( 1 , 0 ) ;
                  SetPWM ( 2 , 0 ) ;
            }
      }
}

I included all of the code just because, but in general you only need t post your variables and the code.

Also, please make your code legible if created in MpLAB, and please use function names that can be easily recognized.

It would appear that it goes straight, tunrs then goes straight.

in that code is in an IF statement, which appears to be triggered by the value of crash being set to 2. i dont know what this could possibly be, since a digital switch works only with 1’s and 0’s. when a switch is pressed, it keeps going in a circle for 2 seconds, stops motor two causing the robot to turn in a wider circle for two seconds, and then resume turning in a circle. very interesting.

either that or the robot always goes straight. until a switch on the top of the robot is pressed, which makes the robot keep going straight for two seconds, make a very long turn with only one motor, then continue going in the forward direction.

also, if using easyC, it is not possible to mess up the syntax, people can just compile the code to check for error before posting.

Well, pacoliketaco sort of got it. As I forgot that the bumper works with 0’s and 1’s he gets to post a code. It was supposed to go straight hit something turn right go straight repeat…

s0FaB0y,

Have you entered that code and tested it?

You have a “classic” bug in your code.

Look at the test condition you have for the IF statement. Depending on the compiler I suspect the ELSE would be the only thing that would ever execute.

Ken

BTW, I like your idea. It’s a chance to look at different coding styles. This will help out allot when you have to look at your teammates’ code. I’ll post some code when I get a few minutes.

Wow I didn’t even notice that error at first. Anyways I think it would always execute the if statement not the else since when it set it equal to two that would be by default true. That’s if it even compiled the code, which is unlikely.

corpralchee,

I don’t think the compiler would have any problem with it since the syntax is correct. This is what is called a “logic” bug. I’ve seen compilers generate code a couple of different ways in this situation. In one case, as you stated, the assignment would occur first and then “crash” would be evaluated as “2” which would be “true”. The second case I’ve seen is the assignment is treated as an expression and the “result” or “return code” from the expression is evaluated. In this case the assignment would always succeed so the result would always be “0” which is “false”.

The lesson here: “=” and “==” are not the same thing.

The ANSI C standard says that “The Assignment Statement has a Value and can Occur in expressions; the most common example is while ((c = getchar()) != EOF)” (The C Programming Language, by K&R ANSI edition pg 51)
This is such a simply use of the C Language, I would think that it would only be “forgot” in the most basic compilers.

Hi Mark,

I must admit that much of my C experience was in the late 80’s and early 90’s using non-ANSI C compilers.

If I understand your example correctly, with any ANSI C compiler “crash = 2” would result in a value of “2” which would always be “true” therefore the “else” would never execute.

Thanks for the clarification,
Ken

I don’t think that the Embedded C Compilers really follow the ANSI standards, but something this simple should be “observed”… A little testing would “prove it out”.