xchip lab 1 - VCSFA-MARS/ThinSat-Program GitHub Wiki
To perform this lab you will need:
- IP01 xChip- USB module
- CW01 xChip- WiFi module and processor
- OD01 xChip- OLED display module
- 2 connector pieces
Let's put together our first xSystem!
Take IP01, CW01, and OD01 and click them together using the connectors.
It should look something like this:
Plug it into your USB port, which will power your circuit. The LEDs should flash momentarily while it boots up.
Copy this code into the Arduino IDE, erasing any code that is already present.
///////////////////////////////////////////////
// This program prints "Hello World" on xChip OD01
// Uses OD01, CW01, IP01 xChips
//////////////////////////////////////////////
#include "xCore.h" //This library makes the processor usable
#include "xOD01.h" //This library makes the OLED screen usable
void setup() {
// These first commands prepare the xSystem, by starting the chips
// This section will run only once
Wire.begin(2,14); // Starts the communication with the xSystem
OLED.begin(); // Turn on the OLED Display OD01
}
void loop() {
// This is where the body of the program is
// This is the section we will be modifying
// This section will repeat indefinitely
OD01.set1X(); // set display text to normal size
OD01.println("Hello World") ; // Display text on the OLED Screen
delay(2000); // two second delay before executing the next command
OD01.clear(); // Sends a command to the OD01 to clear the display
//The program will now repeat from the top of void loop()
}
Next you will verify the code by pressing the blue arrow in the upper left corner of the Arduino IDE. After all of your errors (if any) are remedied, go ahead and upload the code to your xSystem.
If you need a reminder about coding fundamentals, go back to xChip Programming Basics.
Great! If everything worked out you should see this and every 2 seconds the words should flicker:
Note: We will only be editing the
void loop()
function for now, so I am only going to display that portion of the code in the tutorial. The unaltered setup sections still need to be part of your complete code.
Let's see what else we can do with our program.
Comment out OD01.clear();
by adding //
to the front of the command, like so: // OD01.clear();
. The text should change from red to gray.
Your void loop()
section should look like this now:
void loop() {
// This is where the body of the program is
// This is the section we will be modifying
// This section will repeat indefinitely
OD01.set1X(); // set display text to normal size
OD01.println("Hello World") ; // Display text on the OLED Screen
delay(2000); // two second delay before executing the next command
// OD01.clear(); // Sends a command to the OD01 to clear the display
//The program will now repeat from the top of void loop()
}
Verify the code and remedy any errors before uploading to your xSystem.
Now we have Hello World
scrolling down our display. Wow!
The OD01.clear();
statement cleared the OD01 screen from all text. When we commented out the statement, the program ignored the command and the screen stopped being cleared each time the void loop()
section repeated.
Next we are going to modify the delay(2000);
line. Change 2000
to 1000
, so that the line reads delay(1000);
.
Your modified void loop()
should look like this now:
void loop() {
// This is where the body of the program is
// This is the section we will be modifying
// This section will repeat indefinitely
OD01.set1X(); // set display text to normal size
OD01.println("Hello World") ; // Display text on the OLED Screen
delay(1000); // one second delay before executing the next command
// OD01.clear(); // Sends a command to the OD01 to clear the display
//The program will now repeat from the top of void loop()
}
Verify the code and remedy any errors, if any, before uploading to your xSystem.
The text looks about the same, but now a new line is printed every one second instead of two. The delay()
command is a timed pause in the code, counted in milliseconds.
TRY IT: Try changing the delay time to larger numbers and smaller numbers to see how it affects the xChip.
THINK ABOUT IT: When do you think it would be advantageous to have a scrolling text output? When do you think we would only want one line of text at a time?
OD01.print("Hello World");
is the command that says we are going to 'print' information onto the OLED screen. The syntax of the command (" ")
tells the program what to print. Whatever is in the (" ")
will be printed.
Hint: You can think of this like reading a sentence where someone is speaking:
Emily said "Hello World!".
You know what Emily said because it is in quotation marks.
Change the text to print your first and last name.
As an example, it says OD01.println("First Last");
but put your first name and last name there!
Your modified void loop()
should look like this now:
void loop() {
// This is where the body of the program is
// This is the section we will be modifying
// This section will repeat indefinitely
OD01.set1X(); // set display text to normal size
OD01.println("First Last") ; // Display text on the OLED Screen
delay(1000); // one second delay before executing the next command
// OD01.clear(); // Sends a command to the OD01 to clear the display
//The program will now repeat from the top of void loop()
}
Verify the code and remedy any errors before uploading to your xSystem.
TRY IT: Try changing the text to something else. What happens if you put too many characters in a line?
Modify the line that says OD01.println("First Last") ;
to say
OD01.println("First");
and then add a line of text below that says OD01.println("Last");
.
Your modified void loop()
should look like this now:
void loop() {
// This is where the body of the program is
// This is the section we will be modifying
// This section will repeat indefinitely
OD01.set1X(); // set display text to normal size
OD01.println("First") ; // Display first line on the OLED Screen
OD01.println("Last"); //Display second line
delay(1000); // one second delay before executing the next command
// OD01.clear(); // Sends a command to the OD01 to clear the display
//The program will now repeat from the top of void loop()
}
Verify the code and remedy any errors before uploading to your xSystem.
TRY IT: Try adding a
delay(1000);
between your lines of text.
For our final modification, we are going to change the command OD01.set1X();
to OD01.set2X();
. This will set our text to be twice as large!
Your modified void loop()
should look like this now:
void loop() {
// This is where the body of the program is
// This is the section we will be modifying
// This section will repeat indefinitely
OD01.set2X(); // set display text to large size
OD01.println("First Last") ; // Display text on the OLED Screen
delay(1000); // one second delay before executing the next command
// OD01.clear(); // Sends a command to the OD01 to clear the display
//The program will now repeat from the top of void loop()
}
We did a lot of coding!
Using everything you've learned in this lesson, see what interesting things you can get the xChip to print. Have fun with it!
Don't worry about making mistakes as you code -- you won't break the xChip with bad code and you'll learn a lot! When you're ready to move to Lab 2, you will learn to add a new xChip to the xSystem, read and display data, and use logic statements in your programs.
Have you ever wondered how we produce light on a screen? In the world of electronics, there are many ways of creating light from an electrical circuit, all of which involve the conversion of energy from electrical energy to light energy. Some examples include lasers, light bulbs, and LCDs, but in our case, we use an LED (actually an OLED, a specific type of LED--more on that soon enough) to make our OD01 xChip display colors, text, and images for us.
To understand how an LED works, we must first understand its key component-the diode. A diode is an electronic device that, when connected with a circuit, only allows current to flow in one direction. A diode is typically made from a junction of two different types of semiconductor materials, one whose crystals contain an excess of electrons (n-type material) and one whose crystals contain an excess of holes that electrons can move into (p-type material). This combination of n-type and p-type materials only allows electrons from the n-type side to flow to the holes on the p-type side, so current in the circuit that the diode is connected to can thus only move in one direction.
LED stands for "Light-Emitting Diode." An LED is just a diode where the semiconductor material dissipates energy in the form of light rather than heat. When the right amount of current flows through the LED, light is emitted. OLED stands for "Organic Light-Emitting Diode," which is a specific type of LED that is used in our OD01 xChip. The "Organic" term refers to the semiconductor material, which is made from an organic compound. Our OLED screen is made up of many controllable pixels that our xChip uses to display our text. While we can control most of the display parameters of the screen, you may notice that we cannot change the colors. This is not because we don't have the coding ability to do this, but because the hardware itself can't be changed. Most LEDs can only display one color because of the lens it was built with, and our OLED was built with a combination of only blue and yellow LEDs.