Introduction to Input/Output

Let's learn by doing! This tutorial uses the LCD on the IntelliBrain bot as an output device. You will learn how to write programs to have the robot display whatever you would like (as long as it fits) onto the LCD display. You will also learn how to use the thumbwheel on the robot as an input: obtaining a value from the thumbwheel and then displaying the value on the LCD screen. We will also introduce if/else and if/else if/else statements to allow for different output to be displayed depending on the input.

Objectives

  1. Start to develop an understanding of basic Java programming using the Intellibrain-Bots
  2. Use the LCD display on the robot to print text
  3. Use thumbwheel input and LCD display together to display results from sampling the thumbwheel

Important Words

  • variable
  • variable type
  • library import
  • main method
  • block
  • line comment
  • loop
  • infinite loop
  • reference variable
  • integer
  • string
  • if/else statement
  • if/else if/else statement


Before you start

Know how to create a project in a Java IDE and in RoboJDE //link to Getting Started

Have a basic familiarity with the IntelliBrain bot //link to Meet the IntelliBrain

//the two intellibrain pictures showing location of LCD display and thumbwheel



Printing text to the LCD display

In order to be able to use the LCD display on the IntelliBrain bot, we have to import the libraries that the program will need. These libraries contain pre-written code for the program to use and contain all the necessary information about how it is used. This is similar to going to your school library and checking out a book. There are a lot of options and but you only want the ones that you need for the specific project you are working on! The first program we will write will display the text "Hello World!" on the LCD of the IntelliBrain. Let's get started on the process!

First, let's decide on the name of our program. We want to name it something that is descriptive and help us remember what the program does later on - so let's call this one HelloWorldDisplay. Then we need to create a new program in our IDE of the same name (refer to this tutorial if you need a refresher //link). Let's start by adding the library imports we need.


	import com.ridgesoft.intellibrain.IntelliBrain;   //this import is for the IntelliBrain bot

	import com.ridgesoft.io.Display;                 //this import is for the LCD display

	

	public class HelloWorldDisplay {

	               

	    //your code will go here



	        }

	    }

	}

	

You probably noticed that there are several places in the code where text follows a double slash ( // ). These are called line comments, and are for our information only. The program will ignore the rest of the line following the //. There is a different notation for commenting out entire sections, and those are called block comments.

Even though we now have the library imports we need in order to be able to use the LCD display and the robot, the program does not know that an LCD display even exists on the robot. I know you just pointed at the LCD and were thinking "it's right here, silly", but a computer program only knows what you tell it and will only do what you tell it to do. You literally have to tell it everything! Let's create an LCD display for the program to use now.


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.io.Display;

	

	public class HelloWorldDisplay {

	               

	    public static void main(String[] args){



	        Display display = Intellibrain.getLcdDisplay();  //we are creating a display to use here

	                    

	        }

	    }

	}

	

We have an LCD display for the program to use, now what? We now need to tell the program what to display. This is done using the .print("") method (which is located in the library you imported earlier) and putting the text you want displayed between the two " " . We want the screen to display "Hello World!" so the method will look like .print("Hello World!").


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.io.Display;

	

	public class HelloWorldDisplay {

	               

	    public static void main(String[] args){

	        Display display = Intellibrain.getLcdDisplay();

	                    

	            display.print(0, “Hello World!”);

	        }

	    }

	}

	

You probably noticed that there was a 0 inside the parentheses. The .print method uses either a 0 or a 1 to indicate which line of the LCD display you want the message to appear on. If it is a 0 it will be on the top line, if it is a 1 it will be on the bottom (remember that in programming, counting starts at 0 instead of 1!).

We're almost done! The last thing we need to do is tell the program how long to display the message on the LCD display. We will do this by using a while(true) loop, which forces the program into an infinite loop. The program will run until you tell it to stop!


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.io.Display;

	

	public class HelloWorldDisplay {

	               

	    public static void main(String[] args){

	        Display display = Intellibrain.getLcdDisplay();

	                    

	        while(true){

	            display.print(0, “Hello World!”);

	        }

	    }

	}

	

Congratulations! You have just written the entire program for the robot to display "Hello World!" on its LCD display.



Words to know

variable
- a variable stores either a value or a reference to an object, allowing for later use. In the previous example, display (with the lowercase d) is a variable. It stores the result of the .getLcdDisplay() method at the specified address.
variable type
- this tells the computer what type of variable it is working with. It allows the computer to know the characteristics and associated methods for the variable. In the previous example, Display (with the uppercase D) is the variable type of the variable display.
library imports
- This tells the computer what files the program is going to use from the library.
main method
- the program runs from this method.
block
- segments of code that are located within braces ( { } ), this is important for the scope of variables (which we will discuss later on). If you are using an IDE, if you select a brace the program will highlight it's partner brace allowing you to see the code located within the block
string
- variable type that represents text. In this example, Hello World! is a string
comment
- notes for the programmer/user, ignored by the program


More printing text to the LCD display

  1. How would you make the robot display the text "I Love Science!" on the LCD display?
  2. Make your robot display "I Love Science!" on the LCD display.
  3. What happens if you change the 0 on line 10 to a 1? Try it!
  4. What happens if you make a copy of line 10 below it, and change the 0 to a 1 in the copy? Try it!
  5. What happens if you remove lines 9 and 11? Try it!

Gathering data from the Thumbwheel

Now that you know how to print something one the LCD display, let's take it a step further! We are now going to use the thumbwheel on the robot as an input and use the LCD display as an output.

Let's use the the same process that we did before and write a new program named ThumbwheelDisplay. The first step we need to do is import all the libraries that the program will need to use the LCD display and the thumbwheel.


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.AnalogInput;  //this import is for the thumwheel

	import com.ridgesoft.io.Display;

	

	public class ThumbwheelDisplay {



            public static void main(String[] args){          

	

	//you code will go here



	    }

        }

	

Now that we have the libraries we need, we need to tell the program that the robot has an LCD display and also a thumbwheel for it to use. This is just like what you did before, except you are now creating two variables instead of one.


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.AnalogInput;

	import com.ridgesoft.io.Display;

	

	public class ThumbwheelDisplay {          

	

	    public static void main(String[] args){

	        AnalogInput thumbwheel = IntelliBrain.getThumbWheel ();  //we are creating the thumbwheel to use here

	        Display display = IntelliBrain.getLcdDisplay();

	

	    }

	}

	

Now we have the LCD display and thumbwheel to use, what do we want to do with them? We want to get a value from the thumbwheel, and then show that value on the display. In order to get the value from the thumbwheel we will need to use the .sample( ) which is located in the library you imported for the thumbwheel. But we also need a place to store the value we get from the method. We will do this by creating a new variable called result as the place to store the result from the method. To put something on the display we use the same .print(" ") method we did above. Let's implement this in our program now.


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.AnalogInput;

	import com.ridgesoft.io.Display;

	

	public class ThumbwheelDisplay {          

	

	    public static void main(String[] args){

	        AnalogInput thumbwheel = IntelliBrain.getThumbWheel ();

	        Display display = IntelliBrain.getLcdDisplay();

	

	        

	            int result = thumbwheel.sample();  //we are getting the value from the thumbwheel and putting it into result

	            display.print(0, “Thumb Wheel ” + result);   //we are printing the value in result to the LCD display

	        

	    }

	}

	

You're almost done! All we need to do now is to tell the program how long we want it to display the result on the LCD. We will do this using a while(true) loop like we did before. Add the while(true) loop to your program.


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.AnalogInput;

	import com.ridgesoft.io.Display;

	

	public class ThumbwheelDisplay {          

	

	    public static void main(String[] args){

	        AnalogInput thumbwheel = IntelliBrain.getThumbWheel ();

	        Display display = IntelliBrain.getLcdDisplay();

	

	        while(true){

	            int result = thumbwheel.sample();

	            display.print(0, “Thumb Wheel ” + result); 

	        }

	    }

	}

	

The location of the while(true) loop does have an important role. In this case, by placing the variable result and the display method inside the loop, this allows the program to change what is displayed on the LCD if the value of result changes.

That's it, you did it! Now you are able to display the value from the thumbwheel to the LCD display. What happens as you move the thumbwheel?



Words to know

loop
- a loop executes a block of code a specific number of times or until a stopping condition is met. Loops are best used for tasks that are going to be repeated. In this case, the while(true) forces the program into an infinite loop.
infinite loop
- a loop that will continue forever. Generally, infinite loops are not desirable, but we force it into one in this instance.
integer type variable
- a variable that is of the type integer, this is a primitive variable.
reference variable
- a variable that holds a memory address as opposed to a specific value.


More gathering data from the thumbwheel

  1. What do you think would happen if we did not put the space after "Thumb Wheel " in the code?
  2. What happens if you take it out? Try it!
  3. What happens if you change the 0 on line 13 to a 1? Try it!
  4. What happens if you make a copy of line 13 below it, and change the 0 to a 1 in the copy? Try it!
  5. What happens if you remove lines 11 and 14? Try it!

if/else Statements using the Thumbwheel

We've gotten a value from the thumbwheel and displayed that value to the LCD. But what if you wanted the message displayed on the LCD to depend on what the value of the thumbwheel was? Let's make a program that will display one message if the thumbwheel is above a specified value, and a different message if is lower. How do we do this? The answer is: if/else statements. You can think of if/else statements as a fork in the road. The program will run until it gets to that point, then it has to make a "decision" as to which path to run. This is done using a boolean expression which the program will determine if it is true or false. If the expression within the parentheses is true, the program will execute the code in the block that immediately follows, else (see what I did there?) the code in the other block is executed. To see how boolean expressions are evaluated, please see //link to Introduction to Boolean Logic

Let's start writing our program using an if/else statement! Build your program part by part like we did earlier. Remember to import the libraries you need, create the LCD and thumbwheel, and create a variable to store the value we get from the thumbwheel. Compare the code you create to our code below.


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.AnalogInput;

	import com.ridgesoft.io.Display;

	

	public class ThumbwheelIfElse {

	

		public static void main (String[] args){

	

			AnalogInput thumbwheel = IntelliBrain.getThumbWheel();

			Display display = IntelliBrain.getLcdDisplay();

			int result = thumbwheel.sample();



			while(true){

				result = thumbwheel.sample();

	

				

				}

			}

		}

	}

	

Now let's add the if/else statement to the while(true) loop in our program. The value we will compare the result from the thumbwheel to will be 512.

While it may seem that the number 512 we choose was random, in reality, it was chosen carefully. If you are curious like us, you tested the thumbwheel to find out the minimum and maximum values. The numbers range from 0 to 1023, which a range of 1024. As you can see, 512 is half of the range.

	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.AnalogInput;

	import com.ridgesoft.io.Display;

	

	public class ThumbwheelIfElse {

	

		public static void main (String[] args){

	

			AnalogInput thumbwheel = IntelliBrain.getThumbWheel();

			Display display = IntelliBrain.getLcdDisplay();

			int result = thumbwheel.sample();

			while(true){

				result = thumbwheel.sample();

	

				if(result > 512){  //the if part of the if/else statement, the boolean expression is in the (  )



                                    //your code will go here

					

				} else {  //the else part of the if/else statement



                                    //your code will go here

					

				}

			}

		}

	}

	

You're almost there. All that we need to do now is to put what we want the program to do for each case into the appropriate block. Let's display "Value greater than" and what the thumbwheel value is if the number is above 512, and display "Value less than" and the value if the number is less than 512.


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.AnalogInput;

	import com.ridgesoft.io.Display;

	

	public class ThumbwheelIfElse {

	

		public static void main (String[] args){

	

			AnalogInput thumbwheel = IntelliBrain.getThumbWheel();

			Display display = IntelliBrain.getLcdDisplay();

			int result = thumbwheel.sample();

			while(true){

				result = thumbwheel.sample();

	

				if(result > 512){

					display.print(0, “Value is greater than”);

					display.print(1, Integer.toString(result));

				} else {

					display.print(0, “Value is less than”);

					display.print(1, Integer.toString(result));

				}

			}

		}

	}

	

You're done! Now move the thumbwheel up and down and see what happens to the display!



Words to know

if/else statement
- program code that will execute the code in the block after the if statement if condition in the parentheses is true, otherwise it will execute the code in the block after else statement


More if/then statements using the thumbwheel

  1. What are some advantages to using if/then statements with the robot?
  2. What happens if you use a number other than 512? Try different numbers!
  3. What is the maximum value from the thumbwheel? Find it!
  4. What is the minimum value from the thumbwheel? Find it!

if/else if/else statements

You are not limited to only two options or conditions. You can use a modified version of if/else statements to expand it to 3 or more! The following code shows how you can use three different values for the thumbwheel.


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.AnalogInput;

	import com.ridgesoft.io.Display;

	

	public class ThumbwheelIfElseIfElse {

	

		public static void main (String[] args){

	

			AnalogInput thumbwheel = IntelliBrain.getThumbWheel();

			Display display = IntelliBrain.getLcdDisplay();

			int result = thumbwheel.sample();

	

			while(true){

				result = thumbwheel.sample();

	

				if(result > 682){

					display.print(0, “Value is greater than 682”);

					display.print(1, Integer.toString(result));

				} 

	            else if(result <= 682 && result >= 341) {

					display.print(0, “Value is in the middle”);

					display.print(1, Integer.toString(result));

				}

	            else {

	                    display.print(0, “Value is less than 341”);

					display.print(1, Integer.toString(result));

			}

		}

	}

	

This code is very similar to the one you worked with for the if/else statements, except that it allows you to work with 3 conditions instead of only 2. You are not limited to using only 1 else if between the if and the else - you can use as many else if statements as you need or want, giving you a wide range of options.



Words to know

if/else if/else statement
- program code that is very similar to else/if statements but allows for 3 or more conditions instead of just 2.


More on if/else if/else statements

  1. Can you make the code work for 4 different conditions?
  2. What about for 5 or 6 different conditions? Try it!
  3. How did you decide what those conditions should be?