Categories
Interactive Fashion

Exercise 3 – wireless circuit XBEE

In this week’s class, we cooperate to make a wireless circuit using sensors and actuators. Our group is responsible for the XBEE A.

First, we program the XBEE and choose 2D1 as our pan id. And then we program the lilypad A and use the code AnalogReadSerial to make sure the circuit is connected in the right way.

After that, we connect lilypad A with the XBEE A. Different from the instruction, we use the computer as the battery. We need to connect the positive and negative poles of the lilypad to the positive and negative poles of the XBEE first, and then connect the XBEE to the computer. One of the connectors fell out during this process and caused problems in the next steps, but we found it later and connected it. 

We use this code to test if the communication works well.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second in your serial port:
  mySerial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A5);
  // print out the value you read in your serial port:
  mySerial.println(sensorValue);
  delay(100);        // delay in between reads for stability
}

After that, we send the signal to XBEE B and use this code on XBEE B (since only it receives the signal) to let the circle change with the sensor.

import processing.serial.*;
int NUM_OF_VALUES_FROM_XBEE = 1;   /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int sensorValues[];      /** this array stores values from the Lilypad1 **/
String myString = null;
Serial myPort;
void setup() {
  setupSerial();
}
void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[ 3 ], 9600);
  // WARNING!
  // You will definitely get an error here.
  // Change the PORT_INDEX to 0 and try running it again.
  // And then, check the list of the ports,
  // find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----" 
  // and replace PORT_INDEX above with the index number of the port.

  myPort.clear();
  // Throw out the first reading,
  // in case we started reading in the middle of a string from the sender.
  myString = myPort.readStringUntil( 10 );  // 10 = '\n'  Linefeed in ASCII
  myString = null;

  sensorValues = new int[NUM_OF_VALUES_FROM_XBEE];
}
void draw() {
  getSerialData();
   printArray(sensorValues);
   
  circle(width/2, height/2, sensorValues[0]);
  // use the values like this!
  // sensorValues[0] 

  // add your code

  //
}
void getSerialData() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil( 10 ); // 10 = '\n'  Linefeed in ASCII
    if (myString != null) {
      String[] serialInArray = split(trim(myString), ",");
      if (serialInArray.length == NUM_OF_VALUES_FROM_XBEE) {
        for (int i=0; i<serialInArray.length; i++) {
          sensorValues[i] = int(serialInArray[i]);
        }
      }
    }
  }
}

I think this circuit can be used for a wearable device and it will become more feasible if we replace the sensors with humidity, temperature, and light sensors. For example, if a top is made from multiple pieces of fabric and each piece of fabric contains a complete circuit like this. Suppose we use light sensors on every piece of fabric so that when people walk into a dark place their tops will glow, and when they enter a bright place the clothes will not change.

Leave a Reply

Your email address will not be published. Required fields are marked *