Skip to content

xchip bonus 3

Nick Counts edited this page Jan 21, 2018 · 2 revisions

Bonus Lab 3: Average Altitude

To perform this lab you will need:

  1. IP01 xChip- USB module
  2. CW01 xChip- WiFi module and processor
  3. OD01 xChip- OLED display module
  4. SW01 xChip- Humidity, Pressure, and Temperature Sensor
  5. 3 Connector pieces

Assembly

Click your pieces together using the connector pieces.

Lab 3 and 4 Setup

The Code

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 displays altitude and its moving average
// Uses SW01, CW01, OD01 and IP01 xChips
// Written by J. Singh
/////////////////////////////////////////////////////

#include <xCore.h>                //This library allows us to use the CW01 processor
#include <xOD01.h>                //This library allows us to use the OD01 screen
#include <xSW01.h>                //This library allows us to use the weather sensor

void setup() {
  // This setup code will prepare our xSystem for use. It will only run once.

  Wire.begin(2,14);               //Start I2C communication with the xSystem
  OLED.begin();                   //Turn on the OLED Screen
  SW01.begin();                   //Turn on the SW01 sensor

  OD01.set2X();                   //Set big font size
  OD01.println("===========");    
  OD01.println("AvgAltitude");    //Print opening message on OLED Screen
  OD01.println("===========");

  delay(5000);                    //Allow SW01 sensor to normalize before starting loop

}

void loop() {
  // This is the body of the code. It will repeat indefinitely.
  // Declare variables and initialize at zero

  int counter;             //Used for determining sample size
  float altitude = 0;      //Used for instant sensor value
  float sum = 0;           //Used for average calculations
  float avg_alt = 0;       //Holds average calulated value

for (counter=1; counter>0; counter++){
    //create a for loop to take a moving average
  OD01.clear();
  SW01.poll();                  //Tell the SW01 sensor to collect instantaneous data

  altitude = (SW01.getAltitude()); //Set pressure variable equal to the instantaneous altitude reading
  sum = sum + altitude;         //Update sum value to include new altitude measurement
  avg_alt = sum/counter;        //Calculate the average

  OD01.print("Count: ");        //Display total amount of data points taken
  OD01.println(counter);

  OD01.print("Alt ");           //Display the instantaneous altitude
  OD01.print(altitude);
  OD01.println(" m");

  OD01.print("Avg ");           //Display the moving average
  OD01.print(avg_alt);
  OD01.println(" m");

  delay(2000);                  //Pause for two seconds before repeating loop
  }

}

The OLED Screen will now display a continuous average value of the measured altitude. "Count" signifies the amount of data points collected, "alt" signifies the current altitude, and "avg" signifies the average value.