EasyC - writing user functions in C

When I’m asked for example code, it’s always been easier to respond to questions about ROBOTC (or PROS and ConVEX) than it is EasyC. The reason for this is the difficulty in quickly creating and posting code in EasyC’s drag and drop flowchart interface. For some students I think it’s time to move on and start writing code using the EasyC text editor (or just switch programming environments ) so I though I would show how to jump out of the EasyC flowcharts into a more conventional (and yes, less forgiving) way or working.

Here are the steps to jump straight from OperatorControl into a user function written in C.

  1. Create a new competition project.

  2. In the OperatorControl function, delete the while(1) loop. Drop the “user code” block below the variables block and type into it “UserOperatorControl();” The flowchart should look like this.

[ATTACH]8458[/ATTACH]

  1. In the project explorer window, right click on “Block Diagram” and select “Add new function…”. In the dialog box that pops up give the function a name, UserOperatorControl. We don’t need any arguments.

[ATTACH]8459[/ATTACH]

This will create a new flowchart tab with the new function. ( We could have skipped this stage and just made a new C file directly but this way works just as well).

  1. If you wan’t you can create some code in the “normal” way, first add a while(1) loop, this is replacing the while loop that would have normally been in the OperatorControl function, as we are calling this new function from there we need to make sure it does not return back to where it was called from or the code will stop running. Here is a short program I entered.

[ATTACH]8460[/ATTACH]

  1. Turn this function into a C file, right clock on the function in the project explorer window, select “Convert to C code”. This will be a one way process, once done there’s no going back to the flowchart mode for this function.

[ATTACH]8461[/ATTACH]

6 The converted file looks like this, a text file where we can now start programming using the keyboard rather than drag and drop.

[ATTACH]8462[/ATTACH]

  1. There’s one more thing to do, we need to let the compiler know about our C code function. In the project explorer, double click on the file UserInclude.h under Header Files. Add one line of text so the file looks like this.
// UserInclude.h : header file
#ifndef USERINCLUDE_H_
#define USERINCLUDE_H_

// TODO: add user code here
void UserOperatorControl ( void );

#endif // USERINCLUDE_H_

Everything should now compile correctly.
easyc_1.jpg
easyc_2.jpg
easyc_3.jpg
easyc_4 .jpg
easyc_5.jpg

1 Like

So now we’re in the scary land of actually having to type some code, fear not, EasyC still helps you out here. Try the following; from the function block window open up the Joystick functions and select “Get Joystick Digital”, drag the function into the UserOperatorControl text window, a dialog should open and allow you to enter button and variable selection just as usual, click OK and EasyC will add that line of code to the file. This really helps when you cannot remember what the parameters to the function are.

This is a one way process, once you have created that line of code any changes must be made by editing, you cannot call up that dialog again.

1 Like

For those not ready to leave the EasyC API and others who don’t like the drag and drop blocks, another option in EasyC is to add your own source code. This can be achieved by right clicking on Source Files in the Project Explorer and then selecting Add New File. Once you get the hang of it, it makes coding much quicker than block functions.

Ok, that’s the other way of doing it, you can do it either way.

I’m happy to hear that you already use user functions, this was the first stage in working on some PID control for you and I wasn’t going to do the whole thing as drag and drop :slight_smile:

1 Like