W8-Project2-idea and tests

The goal for this week is to finish the basic setup and wire up all the components.


Material: 4-pin RGB LED. BMP180 Pressure sensor. Balloons.

Additional Materials: Touch sensing switch. Accelerometer.


Arduino Part

IMG_2378.jpeg

First of all, I connected the RGB LED to the board. I figured each pin represents one color and will connect to one of the PWM pins from researching online. Here is the code for testing.

#define rPin 9
#define gPin 10
#define bPin 11

//int scl = 0;
int sda = 0;

void setup() {
  pinMode (rPin, OUTPUT);
  pinMode (gPin, OUTPUT);
  pinMode (bPin, OUTPUT);
  Serial.begin(9600);

}

void loop() {
  analogWrite(rPin, 0);
  analogWrite(gPin, 255);
  analogWrite(bPin, 0);
//  scl = analogRead(A0);
//  delay(10);
  sda = analogRead(A1);
//  Serial.print(scl);
//  Serial.print(',');
  Serial.println(sda);
  

}

void setColors( int red, int green, int blue) {
  analogWrite(rPin, red);
  analogWrite(gPin, green);
  analogWrite(bPin, blue);

}
 
I soldered pins to the sensor. I think I did a decent job.

I soldered pins to the sensor. I think I did a decent job.

The pressure sensor took awhile to make it work. I had a copy of the sample code from the library from the sensor. I don't quite understand a lot of stuff, but I managed to extract out the pressure sensing part that I need. From reading the code, I also realized the sensor couldn't read pressure directly. It measures temperature and then calculated the air pressure. This mechanism causes a problem for my project, which I will explain below.

After I managed to print out the pressure, I found the lowest air pressure in the room around 1016. I put the sensor into a balloon and start blowing air. The pressure jumped to a very high number, around 1050, and then slowly came down to 1032. I realized the sensor would read the air pumping into the sensor first instead of reading the overall air pressure. When the balloon was decently large, the reading was a lot more accurate because air pumping into the balloon wasn't interfering with the sensor.

IMG_2384.jpeg

To change the LED color. I created a setColors() function. It takes into three byte variables. With mapped values range from 0 to 100, I can change the color of the LED like

setColors(130+mappedValue,185+mappedValue,0);

The light will be green at first. When the mappedValue is above 70, the light will be red because the maximum of a byte is 255, and it will go back to 0 after 255.

Below is the Arduino code:

#include <SFE_BMP180.h>
#include <Wire.h>

#define rPin 9
#define gPin 10
#define bPin 11

// You will need to create an SFE_BMP180 object, here called "pressure":
SFE_BMP180 pressure;

int mappedValue = 0;


void setup() {
  pinMode (rPin, OUTPUT);
  pinMode (gPin, OUTPUT);
  pinMode (bPin, OUTPUT);
  Serial.begin(9600);

  // Initialize the sensor (it is important to get calibration values stored on the device).
  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.

    Serial.println("BMP180 init fail\n\n");
    while (1); // Pause forever.
  }

}

void loop() {
  char status;
  double T, P;

  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);

    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Function returns 1 if successful, 0 if failure.

    status = pressure.getTemperature(T);
    if (status != 0)
    {


      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.

      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.

        status = pressure.getPressure(P, T);
        if (status != 0)
        {
          // Print out the measurement:
          //Serial.print("absolute pressure: ");
          mappedValue =  map(P,1016,1037,0,100);
          //Serial.println(mappedValue);
          Serial.write(mappedValue);
//          Serial.print(" , ");
//          Serial.println(P, 2);
         // Serial.print(" mb, ");
          //Serial.print(P * 0.0295333727, 2);
          //Serial.println(" inHg");

          // The pressure sensor returns abolute pressure, which varies with altitude.
          // To remove the effects of altitude, use the sealevel function and your current altitude.
          // This number is commonly used in weather reports.
          // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
          // Result: p0 = sea-level compensated pressure in mb


        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");

  delay(500);  // Pause for half second

  setColors(130+mappedValue,185+mappedValue,0);

}

//Changing color for LED
void setColors( byte red, byte green, byte blue) {
  analogWrite(rPin, red);
  analogWrite(gPin, green);
  analogWrite(bPin, blue);

}
 

P5 Part

For the P5 sketch, I did a quick and simple test. I used Serial.write(mappedValue) to send data since the value is less than 255. The P5 sketch will read the value and use it as the size of the circle drawing in the middle of the canvas.

let serial; // variable to hold an instance of the serialport library
let fromSerial = 0; //variable to hold the data



function setup() {
    createCanvas(800, 800);
    serial = new p5.SerialPort(); // make a new instance of  serialport librar  
    serial.on('list', printList); // callback function for serialport list event
    serial.on('data', serialEvent); // callback for new data coming in  
    serial.list(); // list the serial ports
    serial.open("/dev/tty.usbmodem14301"); // open a port

}

function draw() {
  background(0);
  noStroke();
  fill('aqua');
  ellipse(width/2, height/2,10+2*fromSerial);
}

// get the list of ports:
function printList(portList) {
    for (var i = 0; i < portList.length; i++) {
        // Display the list the console:
        print(i + " " + portList[i]);
    }
}

function serialEvent() {
    // this is called when data is recieved, data will then live in fromSerial  
    fromSerial = serial.read();
}
 

However, I do not know what happened. All of a sudden, my laptop couldn't find the port. The green light on my nano was on, but the orange light wasn't. I didn't film or take more photos during my tests.

Previous
Previous

W9-Project2-Balloon

Next
Next

W7-Two-Way Serial Communication