xchip lab 2 - 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
- SL01 xChip- Visible light and UVA/UVB sensor
- 3 connector pieces
Click your pieces together using the connector pieces. The xChips can be plugged together in any configuration, and the xSystem will work no matter which chips are attached where.
What is lux?
Lux is a unit of measurement, like a meter or an inch, and it is a way to determine visible light intensity.
The SL01 visible light sensor can detect light from 3 lux to 220,000 lux.
FUN FACT: Direct sunlight is 120,000 lux!
Can you think of an application where a light sensor would be useful?
Sensors like these are practical for automatic lighting applications, such as street lights that automatically turn on at night and your cell phone's 'auto-brightness' feature.
Copy and paste the code from below into your Arduino IDE. Verify and upload the program onto your xSystem.
NOTE: Your IP01 USB module has two switches. Be sure they are set to "B" and "DCE." The xChip will not be able to receive code if it is in any other configuration.
//////////////////////////////////////////////
// This code reads LUX and displays the data on the OLED
// Uses OD01, CW01, IP01, SL01 xChips
// Written by E. Bujold
/////////////////////////////////////////////
#include <xCore.h> //This library makes the processor usable
#include <xSL01.h> //This library makes the light sensor 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); // Start the I2C Communication with xSystem
OLED.begin(); // Turn on the OLED display
SL01.begin(); // Start the SL01 Sensor
OD01.set2X(); // Set the font size to large
OD01.println("===========");
OD01.println("XinaBox LUX");
OD01.println("===========");
delay(5000); //delay before beginning loop function
}
void loop() {
// This is where the body of the program is
// This section will repeat indefinitely
OD01.clear(); // Clear the display
SL01.poll(); // Poll Sensor for collect data
OD01.println("Light Level: ");
OD01.print(SL01.getLUX()); //Read information from sensor and print it
OD01.println(" LUX");
if (SL01.getLUX() < 150){ // If the lux level is less than 150
OD01.println ("It's dark!"); // Then print this information
}
delay(2000); // Two second delay before restarting the loop
}
You will see the "Title Screen" display for a few seconds on the OLED and then the real-time light sensor levels will print to screen repeatedly.
TRY IT: Try varying the amount of light that the sensor receives by shining a light on it or shadowing. How does this affect the value of lux?
Now gently cover the visible light sensor on the SL01 xChip with your finger. This will reduce the amount of light that reaches the sensor to nearly zero.
Wow! How did that happen? How did the sensor display It's dark!
? It did so through the use of conditional statements.
Conditional statements are like true-false questions and when the answer is true you perform a specific set of instructions. These statements follow a very specific syntax. In a very simplified example you can read them like this:
if (statement is true) {Perform these steps};
You do this kind of logic all the time. For example you might say "If it's raining, I'll wear a raincoat". The question becomes "Is it raining?" and if true, you will wear a raincoat.
if (SL01.getLUX() < 150){ // If the lux level is less than 150
OD01.println ("It's dark!"); // Then print this information
}
Using the language of the code, the conditional statement is "If the lux level is less than 150, then print "It's dark!"". The question is "Is the lux less than 150?", if true print "It's dark!".
Continue to Lab 3!
Conditional statements (also called logic statements) are used extremely often in computer programming. Conditional statements are what allow programs to react in certain ways in response to conditions that the programmer can't control--this gives the device being programmed the ability to make decisions on its own. In the Arduino language, we can use if, if/else, and switch conditional statements to control our programs. We will just focus on if and if/else, which are the two more common statements.
The if statement simply says if (statement is true) {Perform these steps}
whereas the if/else says
if (statement is true){perform these steps}
else {perform these steps instead}
The if/else simply gives separate instructions for the program to follow if the if statement instructions can't be executed.
Earlier in this lab, we mentioned how whether to execute a conditional statement is like a true-false question. When writing code, you can only ask these true-false questions in a very specific fashion. Below we are going to look at a few examples of proper syntax for writing the condition.
Evaluating x in relation to zero:
-
>
,if (x > 0)
, if x is greater than zero -
>=
,if (x >= 0)
, if x is greater than or equal to zero -
<
,if (x < 0)
, if x is less than zero -
<=
,if (x <= 0)
, if x is less than or equal to zero -
==
,if (x == 0)
, if x is equal to zero
TRY IT: Using the statements above, would the statements be true or false if x was 2? If x was -2? If x was 0?
NOTE: Notice that "If x is equal to zero" is
if (x == 0)
NOTif (x = 0)
. Using two equal signs represents a logical question (you are asking x what it is). Using a single equal sign represents a definition (you are telling x what it is).
Evaluating a String:
-
if(name=="John")
, if the string variable called "name" holds the word "John"
Evaluating a Boolean:
-
if(myBool==false)
, if the boolean variable called "myBool" is set as false