B. Exploring Worcesterino Student Worksheet (instructional)

Activity 1 Working with the LCD Character Display

In this worksheet we are going to learn how to send text ( known as a “string”) to the lcd display. Then we shall learn something about strings, what they are and how to have fun with them.

Teacher Info / Info for Students / Details.
Prep / Do / Let’s Write something to the LCD Display. Remember you have two rows of 16 characters. Type in the following code and run it within your Wuino sketch.
#include <LiquidCrystal.h
#include "CBP_Worc.h"
char name[] = “Nunnery Wood”; // String to display
void setup() {
lcdSetup(); // prepare the Wuino to receive your text
}
void loop() {
lcdSetCursor(0,0); // set the cursor at row 0 and column 0
lcdPrintStr(name); // send your string to the lcd
}
Now change the code to write your school’s name to the LCD Display. If it is longer than 16 characters then you must be creative, perhaps writing a nick-name?
First we must find out what the lcd functions lcdSetCursor() and lcdPrintStr() actually do. So, let’s go!
Do / Change the call to lcdSetCursor() function in your code as follows, and see what happens to your string.
a) Try lcdSetCursor(1,0); and write down what happens.
b) Now try lcdSetCursor(1,4); and write down what happens.
Can you explain what the two numbers supplied to the function (its “arguments”) actually do?
Strings are arrays of characters. / Now let’s think about what a string really is. Well, a string like “Worcester” is really a load of characters, ‘W’, ‘o’, ‘r’, and so on. This load of characters is actually an array a collection of individual characters. You can see that since the declaration of the variable ‘name’ was char name[]. The ‘char’ says that we are thinking about characters (and not numbers, such as integers) and the square brackets [] says that we have an array of characters.
Let’s make this clearer. In the following code snippet we shall set up an array of 5 characters, assign their values, then send them to the lcd.
4. / Write the following code in your sketch
#include <LiquidCrystal.h
#include "CBP_Worc.h"
char name[5];
void setup() {
lcdSetup(); // prepare the Wuino to receive your text
name[0] = ‘J’;
name[1] = ‘u’;
name[2] = ‘n’;
name[3] = ‘e’;
}
void loop() {
lcdSetCursor(0,0); // set the cursor at a row and a column
lcdPrintStr(name); // send your string to the lcd
}
You should see that all four characters in the array ‘name’ are successfully displayed.
Now let’s add another character to the array. We can do this because the array size is 5, so it can hold 5 characters. Add another line to the setup() function to display a character of your choice.
Write down what you find. This is unexpected. What’s going on? Well it turns out that the last entry of a character array has to be left empty, since this empty value tells the lcd that this is the end of the string.
Now let’s add yet another character to the array. This is really bad since we have declared an array of 5 elements and we are assigning an extra one. Note the code compiles, but it does not run. This is quite normal, but it tells us something important, we must keep an eye on our array size, and not assign values beyond this size.
String Functions / There are some useful functions which we can use with strings. A good example is the function strlen(name) which tells us the length of the string, in other words how many characters are in the string. Let’s investigate.
Do / Write the following code in your sketch. Of course you can use different characters if you wish in the assignment of the string. Do this!
#include <LiquidCrystal.h
#include "CBP_Worc.h"
char name[10]; // declare a max of 9 characters for the string
int length;
void setup() {
lcdSetup(); // prepare the Wino to receive your text
name[0] = ‘J’;
name[1] = ‘u’;
name[2] = ‘n’;
name[3] = ‘e’;
}
void loop() {
lcdSetCursor(0,0); // set the cursor at a row and a column
lcdPrintStr(name); // send your string to the lcd
length = strlen(name); // get the length of the string
lcdSetCursor(1,0);
lcdPrintInt(length); // display the length of the string.
}
Hopefully you found that the length of the string was correctly recorded.
Add some more characters to your string and check that your program correctly counts them. But keep an eye on the max length of your string.
Note how there are different print functions according to whether you are printing an int, a char, or a string. If you get these mixed up then your program will not compile. See this by changing the last line of code to
lcdPrintStr(length);
Thinking / Explore / Re-write your above code to use the simpler declaration of a string, char name[] = “June”; (seen in 1. above) and make sure that it correctly records the length of your string. Try other strings, but remember the lcd can only display a string consisting of an array of 16 characters on a row.
Accessing chars from a str / Sometimes it is useful to look at all the characters in a char array (String), one by one, to see if a particular character is actually in a string. This could be useful in checking password security. What we need to do is to get each character from the array of characters ( a string ) and to check it with the particular character we are looking for. So let’s start off with looking for the character ‘n’ in our string “June”. Here we go.
Write this code in your sketch.
#include <LiquidCrystal.h
#include "CBP_Worc.h"
char name[] = "June";
int length;
int i;
char searched;
boolean found;
void setup() {
lcdSetup(); // prepare the Wino to receive your text
searched = 'n'; // character we are searching for
found = false;
}
void loop() {
lcdSetCursor(0,0); // set the cursor at a row and a column
lcdPrintStr(name); // send your string to the lcd
length = strlen(name);
lcdSetCursor(1,0);
for(i=0;i<length;i++) {
if(searched == name[i]) {
found = true;
}
}
if (found == true) {
lcdPrintStr("Found ");
lcdPrintChar(searched);
lcdPrintStr(" at pos ");
lcdPrintInt(i);
}
if(found == false) {
lcdSetCursor(1,0);
lcdPrintStr("Found is false");
}
}
Now change the searched for a character not in June and see what happens.
Reading Code / Read the above code closely. In particular look for and explain
a) a loop
b) one or more selection statements.
It’s vitally important that you become familiar with these constructs. Reading code and explaining what they do is one good way to gain confidence.
Another option is to modify code. For example, to see the effect of a particular statement or block of code, you could comment it out and see what happens. That will give you some information on what the code actually does.
Encryption / Using Arrays / When we type in a password into an application then the characters we type (our password) are not stored directly into the application. So if your password is “TomTom” this could be stored as “upxupx”. This is called encryption. Now we are going to explore this idea, but in a simple way.
In the code below, we shall take an array of characters, which you will supply as your password, and change each character to another character. So the stored password will not be the same as your password and will be more secure.
#include <LiquidCrystal.h
#include "CBP_Worc.h"
char name[] = "June";
char encr[] = " ";
int length;
int i;
char character;
void setup() {
lcdSetup(); // prepare the Wino to receive your text
}
void loop() {
lcdSetCursor(0,0); // set the cursor at a row and a column
lcdPrintStr(name); // send your string to the lcd
length = strlen(name);
for(i=0;i<length;i++) {
character = name[i] + 1;
encr[i] = character;
}
lcdSetCursor(1,0);
lcdPrintStr(encr);
}
Which variable contains your password?
Which lines of code change the array of characters representing your password?
Which variable contains your encrypted password, and which line of code prints this out?
Now think a bit and decide how to decrypt the encrypted password. Add lines of code to do this and get your program to print out your original password and your decrypted password. They should be the same.
Investigate / You should know which line of code does the encryption. Change the number in this line to anything you like. Investigate the effects of encryption and decryption.