Interactive Fashion-recitation3

This week, we worked in group of 4 and made a software-hardware communicating device using the soft switch we made last week.

We was struggling a bit at the beginning with the correct code and correct circuit but no reception, and later we discovered that the problem was at the beginning stage in the XCTU, we didn’t switch the “MY”and”DL”. After we changed that, the system worked smoothly.

Below is our code:

XBee-A:

#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
}

XBee-B:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
#define NUM_OF_VALUES_FROM_XBEE 1    /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/

int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

/** DO NOT REMOVE THESE **/
int tempValue = 0;
int valueIndex = 0;
int xbee_values[NUM_OF_VALUES_FROM_XBEE]; /** create an array to store the data from Processing**/
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(9, OUTPUT);
  mySerial.begin(9600);
}
void loop() {
  getSerialData();
  xbee_values[0] = constrain(xbee_values[0], 0, 255);
//  xbee_values[0] = map(xbee_values[0], 0, 200, 0, 255);
  analogWrite(9, xbee_values[0]);
  if (xbee_values[0] > 100) {
   digitalWrite(led, HIGH); 
  }else{
    digitalWrite(led, LOW); 
    }
  Serial.println(xbee_values[0]);// print out the value you read:
  delay(1);  
  // set the brightness of pin 9:
//  analogWrite(led, brightness);
  // change the brightness for next time through the loop:
//  brightness = brightness + fadeAmount;
//
//  // reverse the direction of the fading at the ends of the fade:
//  if (brightness <= 0 || brightness >= 255) {
//    fadeAmount = -fadeAmount;
//  }
//  // wait for 30 milliseconds to see the dimming effect
//  delay(30);
}

//receive serial data from Processing
void getSerialData() {
  while (mySerial.available()) {
    char c = mySerial.read();
    //switch - case checks the value of the variable in the switch function
    //in this case, the char c, then runs one of the cases that fit the value of the variable
    //for more information, visit the reference page: https://www.arduino.cc/en/Reference/SwitchCase
    switch (c) {
      //if the char c from Processing is a number between 0 and 9
      case '0'...'9':
        //save the value of char c to tempValue
        //but simultaneously rearrange the existing values saved in tempValue
        //for the digits received through char c to remain coherent
        //if this does not make sense and would like to know more, send an email to me!
        tempValue = tempValue * 10 + c - '0';
        break;
      //if the char c from Processing is a comma
      //indicating that the following values of char c is for the next element in the values array
      case ',':
        xbee_values[valueIndex] = tempValue;
        //reset tempValue value
        tempValue = 0;
        //increment valuesIndex by 1
        valueIndex++;
        break;
      //if the char c from Processing is character 'n'
      //which signals that it is the end of data
      case '\n':
        //save the tempValue
        //this will b the last element in the values array
        xbee_values[valueIndex] = tempValue;
        Serial.println(tempValue);
        //reset tempValue and valueIndex values
        //to clear out the values array for the next round of readings from Processing
        tempValue = 0;
        valueIndex = 0;
        break;
    }
  }
}

Processing:

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() {
  size(500,500)
  setupSerial();
}
void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[ 8 ], 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]);
}
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]);
        }
      }
    }
  }
}

Below is the video:

This is the video with only the led and the soft switch:

This is the video with led/soft switch/Processing:

I think from what I learned today, I think of a wearable pieces as a screen on one’s face, which shows the graph drawn by another person’s movement or feeling. For example, assume that the person who wears the screen is A, and the another one without the screen is B. B will wear a device that can sense different touching texture, and the screen will show different graphs accordingly. It can have a meaning of teaching kids, like if the kid (as B) touches a flower, another kid (A) will see character “flower”. It encourages team work as well as helps the kids understanding the world. Or, it can have the meaning of a crazy couple, where A is out there exploring the world and testing what is dangerous and what is not. whatever that person touches, A will see and know. But since the screen is worn on B’s face, it means that A can and only can see and know what B has seen and touched, they are closely bonded and A is entirely relied on B, which is romantic but dangerous at the same time.

Leave a Reply

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