Hello World

This will be your first chapter into the world of Botball programming with the KISS-C programming environment. You will learn the basics of how to start your program, the proper formatting of programs, commenting your code, and we will also get into examples of using the printf() command to print out text to the screen and also the fd(), bk(), off(), and ao() commands that will teach you how to run very simple motor functions.

Setting Up Your First Program

Here is a very simple program for KISS-C, which displays the words "Hello World!" on the screen.

  1. int main()
  2. {
  3. printf("Hello World!\n");
  4. return(0);
  5. }

If this looks really scary to you, that's fine -- it looks scary to all new programmers. Let's go through bit by bit, and we'll explain what this code is doing.

  1. int main()
  2. {
  3. printf("Hello World!\n");
  4. return(0);
  5. }

Lines 1, 2, 4, and 5 of the code above will be the same in just about all programs you write. All you need to know at this point is that when the computer runs your program, it will look for the "int main()" and run all the commands that are inside the following curly braces, stopping at the "return(0);" line. If you put your program's commands outside of those curly braces or after the "return(0);", it won't work properly.

  1. int main()
  2. {
  3. printf("Hello World!\n");
  4. return(0);
  5. }

On line 3, the "printf" function (a function is a command that does a particular thing) displays text on the screen. When we're giving information to a function about what it should do (e.g. giving printf the text to display), we put parentheses around the information, and put it after the function name. In this case, the data we're passing to printf is "Hello World!\n", so we stick that in parentheses and put it after printf. You may be wondering why the text is in quote marks -- whenever you're passing text of some kind to a function (as opposed to a number), you enclose it in double-quote marks. The \n at the end of the text is called a newline, and is the equivalent of pressing the Enter key to go to the next line. You should always put \n at the end of your printf text; otherwise, later printf commands will print on the same line, which looks weird. Finally, after the closing parenthesis for printf, we put a semicolon. In English, sentences stop with a period; in the C language, commands stop with a semicolon. Forgetting the semicolon is probably the most common mistake among new C programmers.

If you want to see your Hello World program in action, click Simulate in KISS-C, and you'll see it run on your computer. Or, you can click Download, and run it on your CBC.

Commenting Your Code

Something that good programmers do is comment their code. The sole purpose of commenting your code is to make it readable by people. Comments are also completely skipped over by the compiler, so you can write whatever you want in your comments, and they won't affect what your program does at all. Comments should describe what the code does, in a way that people unfamiliar with your code will understand. There are two types of comments in C. There are 1 line comments, and multi-line comments. Here is an example of using comments:

  1. /* This is a multi-line comment. Everything from after the forward slash and asterisk
  2. all the way until I end it with an asterisk and forward slash is a comment. So this can
  3.  be as many lines long as you want! You might stick one of these at the top of your
  4. program, with information on the author and maybe a change log.
  5.   */
  7. int main()
  8. {
  9. //These are one-line comments. Everything after the two forward slashes
  10. // to the end of the line is a comment.
  11. // You might use this to describe what specific lines of your code do, like this:
  13. printf("Hello World!\n"); // displays a greeting on the screen
  15. return(0);
  16. }

Above you see the first comment is a single line comment. To start a single line comment you start with two forward slashes (//). Everything after those two forward slashes on until the end of the line will now be a comment. It will not be compiled by the compiler and will have no affect on your program, but can be there to let the reader of the program know what the line before or after does or something along those lines. Next we see the multi-line comment. The multi-line comment starts with a forward slash and an asterisk (/*). Everything after the forward slash and asterisk is now a comment until you end it with an asterisk and then a forward slash (*/). These can be useful at the beginning of your program when you want to write in an in-depth explanation of the program and it is too long to be on just one line.

There are two main reasons why you should liberally comment your code. The first reason is that you may be sharing your code with someone else, and you need them to be able to figure out what your code is doing. But, you ask, what if you're not ever going to give your code to someone else? Well, be careful when making that claim, because if you ever have a problem with your code, you'll probably want to send a snippet to a friend or post it on a forum to get help. But even if you won't be sharing your code, what happens when, next year, you haven't touched your code in 6 months, and you want to add a small feature? Chances are, you'll have forgotten the inner details of how your code works. Comments will save you here. To make a long story short, you will usually regret it if you don't comment your code. So don't make that mistake.

The fd(), bk(), off(), And ao() Commands

Now that we have a basic understanding on how our program is setup and will look, and we've now seen how calling a function works, lets talk about simple motor movements. Here you will learn how to use the fd() [forward] bk() [backward] off() [off] and ao() [all off] commands. These have to do with moving the motors and each accept only one parameter, which is the motor port number (written next to the motor port on the CBC). The exception is ao(), which applies to all the motors, and so doesn't need you to specify a motor port number. Here is a simple example on how to use these commands:

  1. int main()
  2. {
  3. fd(1); //Turns motor number 1 on at full speed going forward
  4. bk(1); //Turns motor number 1 on at full speed going backward
  5. ao(); //Turns all motors off
  6. //Alternativly you could turn off individual motors by using off(motor number);
  8. return(0);
  9. }

So here we show easily how you can turn motor number 1 going forward, backward, then off. Do not however try to use this code, because it will not work without a function you will learn in the next section called sleep(). However this does give a good example on how these functions work.

The sleep() and msleep() Commands

As you probably know, computers do things very fast. The above program will not work properly, because first the computer sees the fd(1); which turns on the motor going forward. So far so good, right? But the moment after it finishes turning on that motor (possibly a few microseconds later), it goes on to the bk(1); which turns on the motor going backward. After another few microseconds, it gets to the ao(); command, turning the motor off. The computer does this as fast as it can, and the result is that the motor is only turned on for so short a time that you can't see it -- it will look like the motor stays off. Sometimes you want the computer to run stuff as fast as possible. But other times, like here, you will want the computer to simply sit and do nothing for some period of time -- this is called sleeping. Here's an example of the sleep() command in action, fixing the problem in the above program.

  1. int main()
  2. {
  3. fd(1);//Turn motor 1 on at full speed going forward
  4. sleep(2);//Wait 2 seconds before going on to the next command -- the motor will be turning forward for these 2 seconds
  5. bk(1);//Turn motor 1 on at full speed going backward
  6. sleep(4);// Wait 4 seconds before going on to the next command -- the motor will be turning backward for these 4 seconds
  7. ao();//Turns all motors off
  8. return(0);
  9. }

So can you guess what this program will do? Well I'll tell you. The first thing it does is turns on motor 1 forward at full speed. Then it hits the sleep command which forces it to wait for the amount of time given, in this case 2 (seconds). Then it hits the backward function where it will turn motor 1 backward at full speed. Then it hits sleep yet again, which will make it wait for the amount of time given, in this case 4 (seconds). Then it hits the last line to turn all motors off, and it does so. So this will essentially make motor 1 go forward for two seconds, then backward for four seconds, then stop. So what makes this program different from the one we gave earlier? We made it sleep which forced it to wait for the motors to physically move.

There is a similar function called msleep(), which is the same as sleep, but uses milliseconds instead of seconds. (1000 milliseconds = 1 second.) You will learn more about directly using this function for specific purposes later on, especially in the create chapter.

Congratulations, you've finished the first lesson! You should now be able to display text on the screen (using printf) and turn on or off motors (using fd, bk, off, and alloff) for a specified period of time (using sleep).

Exercise: write a KISS-C program which does the following (in order):

  1. Display your name, followed by "likes to move motors!". For example, you might make it display "David Miller likes to move motors!"
  2. Wait 2 seconds.
  3. Turn on motor 1, going forward.
  4. Wait 3 seconds.
  5. Display "Now the other way!"
  6. Make motor 1 go backward.
  7. Wait 2 seconds.
  8. Turn off motor 1.
  9. Display your name, followed by "is done with the motors!"

Also, add a comment at the top of your program, with your name, your Botball team's name, and the current date.

If you have any trouble doing this exercise, reread the above lesson. If you're still having trouble and can't make any progress by rereading the lesson, ask for help on the Botballer's Chat or the Botball Forum. (Please note that if you clearly didn't try to reread the lesson, people may become terse with you after a few minutes. We want to help, but we can't help you if you can't help us help you.) Once you are able to do the entire exercise without looking at notes (i.e. you've memorized how it works), you're ready to move on to the next article!


peterseo's picture

Awesome Information sharing

Awesome Information sharing .. I am extremely cheerful to peruse this article .. much obliged for giving us experience info.Fantastic decent. I value this post. Invention

intelburn's picture

I like the C++ version

  1. I like the C++ version better.
  2. #include <istream>
  3. using namespace std;
  4. int main
  5. {
  6. cout<<"Hello World";
  7. return 0
  8. }

We kick bot

Another quick thing that I

Another quick thing that I noticed while reading is the fact that occasionally, your apostrophes and semicolons turn into some pretty weird symbols. I don't really know why, but it's just something to look at. Great job!

Hey, looks good! Some minor

Hey, looks good! Some minor tweaks:

A few different times you talk about "All you need to know" and "You'll learn more later". I dunno, to me that stuff just clutters it up. If you're going to show it simply (which you should), I'd just show it simply.

Next thing is coding style. I don't think we should hold our own holy war over the proper coding style, but I think each article, and in particular the beginning article should use a consistent style. In this one you've got these two kinds kinda mixed up:

  1. void func() { // this is Java-style K&R/1 True Brace Style (Java puts brackets on same line as function declaration, K&R did not for legacy C reasons no longer relevant)
  2. while (something) {
  3. something_else();
  4. }
  5. }
  7. void func() // this is Allman
  8. {
  9. while (something)
  10. {
  11. something_else();
  12. }
  13. }

So I'd pick one and only one so as to not confuse people reading the article. I prefer K&R but I think most people in botball (including KIPR code) use Allman. If you chose to stick with K&R note that there is a space between the closing parenthesis and the opening bracket on a function declaration.

Last thing I'd try to split up the long comments that put horizontal scrollbars on the codebox if at all possible.
Looks great otherwise, and thanks for helping!