Tuesday, March 5, 2013

Project 2 Results

Here is a result video from my theoretical project described in the previous blog:

I left the inherent noise in the readings from the accelerometer to give the image more visual interest.
You can see the lines being drawn here change color as I squeeze the pressure sensor and the lines move around the screen as I tilt the accelerometer.

Some suggestions from class that would be nice to add to this are that in order to incorporate time into the generated piece I could have the color palette shift from cool to warm as the day progresses so that the older lines will push back into the page and the newer,warmer lines will come forward. Also playing with the thickness of the line being drawn would be more interesting. However, this was just a theoretical project. I would have to actually make a wearable device and try it over the course of an entire day to see if it generated something that I was happy with.

Here is a schematic of my Arduino board setup:


Here is my code for this project:

This is the Arduino file:


int R = 14;
int G = 16;
int B = 15;

int X = A8;
int Y = A9;
int Z = A10;

int x_val = 0;
int y_val = 0;
int z_val = 0;

int prev_x = 0;
int prev_y = 0;
//int base_z = 798;

int r_val = 0;
int g_val = 0;
int b_val = 0;
 int inc_val = 5;
 int r_inc = inc_val;
 int g_inc = 0;
 int b_inc = 0;

int pressPin = A12;
int press_val = 0;
int prev_press_val = 0;

int r = 0;
int g = 0;
int t = 0;

/* 5V input
int base_x = 518;
int base_y = 515;
int base_z = 798;
*/

//3.3 Volt
int base_x = 365;
int base_y = 335;
int base_z = 492;

int scale = 100;

String send_to_serial = "";

const int ledPin[10] =  {
  4,5,6,7,8,9,10,11,12,13};      // the number of the LED pins
int ledState = 0;

void setup()
{

  Serial.begin(9600);

  // initialize the digital pin as an output.
  pinMode(R, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(B, OUTPUT);

   // set the digital pin as output:
  for(int i = 0; i < 10 ;i++){
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop()
{
  send_to_serial = "";
 
  //might not get anything from tilt in this case...
 
  //read the accelerometer and subtract off base/rest value
  x_val = analogRead(X) - base_x;
  y_val = analogRead(Y) - base_y;
  z_val = analogRead(Z) - base_z;
 
  //take the difference between the last time and this time
  int delta_x = x_val - prev_x;
  int delta_y = y_val - prev_y;
 
 //If the pressure sensor is squeezed change the color
 press_val = analogRead(pressPin);

 //calculate the current color
 if(press_val>prev_press_val)
 {
    //increment the color
    if((r_inc==inc_val && r_val>=255) || (r_inc==-inc_val && r_val<=0))
    {
       if(r_val>=255)
       {
         r_val=255;r_inc=0;
         if(b_val>=255){ b_inc=-inc_val;}
         else{g_inc=inc_val; b_inc = 0;}
       }
       if(r_val<=0){r_val=0;r_inc=0;b_inc=inc_val;}
    }
    if((g_inc==inc_val && g_val>=255) || (g_inc==-inc_val && g_val<=0))
    {
      if(g_val>=255)
      {
        g_val=255;g_inc=0;
        if(r_val>=255){r_inc=-inc_val;}
        else {b_inc = inc_val;r_inc=0;}
      }
      if(g_val<=0){g_val=0;g_inc=0;r_inc=inc_val;}
     }
      if((b_inc==inc_val && b_val>=255) || (b_inc==-inc_val && b_val==0))
      {
        if(b_val>=255)
        {
         b_val=255;b_inc=0;
         if(g_val>=255){g_inc=-inc_val;}
         else{ r_inc=inc_val;g_inc=0;}
        }
        if(b_val<=0){b_val=0;b_inc=0;g_inc=inc_val;}
       }
       
      r_val+=r_inc;
      g_val+=g_inc;
      b_val+=b_inc;
       
 }

 //Send the information to Processing using the Serial Port
 send_to_serial.concat(x_val);
 send_to_serial.concat(' ');
 send_to_serial.concat(y_val);
 send_to_serial.concat(' ');
 send_to_serial.concat(z_val);
 send_to_serial.concat(' ');
 send_to_serial.concat(r_val);
 send_to_serial.concat(' ');
 send_to_serial.concat(g_val);
 send_to_serial.concat(' ');
 send_to_serial.concat(b_val);

 int press_led = map(press_val,0,200,0,10);
 for(int i=0; i<10; i++)
 {
   if(i<=press_led){digitalWrite(ledPin[i],HIGH);}
   else{digitalWrite(ledPin[i],LOW);}
 }
// Serial.print(press_led); Serial.print(" "); Serial.println(press_val);

 //This is actually how Processing gets the values
 Serial.println(send_to_serial);
 delay(100); //small delay to let buffer settle and slow down draw

 //show what color is currently being drawn through the first LED
 digitalWrite(R,r_val);
 digitalWrite(G,g_val);
 digitalWrite(B,b_val);

 //reset the prev values for next loop
 prev_x = x_val;
 prev_y = y_val;
 prev_press_val = press_val;
}





This is the Processing file:


import processing.serial.*;

 Serial myPort;        // The serial port
 int xPos = 1;         // horizontal position on screen
 int yPos = 1;         // vertical position on screen

 int w = 400;
 int h = 300;

 float prev_x = random(width);
 float prev_y = random(height);

 void setup () {
 // set the window size:
 size(400, 300);      

 // List all the available serial ports
 println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
   // don't generate a serialEvent() unless you get a newline character:
   myPort.bufferUntil('\n');
   // set inital background:
   background(0);
 }

 void draw () {
 // everything happens in the serialEvent()
 }

 void serialEvent (Serial myPort) {
   // get the ASCII string:
   //We get a line that contains x y z r g b
   String inString = myPort.readStringUntil('\n');
 
   if (inString != null) {
       // trim off any whitespace:
       inString = trim(inString);
       String[] list = split(inString, ' ');
     
       int x,y,z,r,g,b;
     
       // convert to ints
       if(list.length==6)
       {
         x = int(list[0]);
         y = int(list[1]);
         z = int(list[2]);
         r = int(list[3]);
         g = int(list[4]);
         b = int(list[5]);
       }
       else
       {
         //info incorrect so skip this one
         return;
       }
     
       noFill();
       stroke(r,g,b); //we will set this color using the pressure
     
       x = int(map(x, -180,180,0,width));
       y = int(map(y, -180,180,0,height));
       line(prev_x, prev_y, x, y);
       //arc(random(width), random(height) , width, height, roll, pitch);
     
       prev_x = x;
       prev_y = y;

   }
 }




No comments:

Post a Comment