Wednesday, December 3, 2014

Proyecto final Alejandra López

El proyecto consiste en una instalación que posibilita una "interacción física" con una serie de imágenes que se ven animadas por la cantidad de luz que recibe una fotocelda.

Para lograr lo anterior se integraron las plataformas de Arduino y Processing. 
La primera controla los parámetros por lo cuales se regirá la fotocelda y la segunda estará encargada de la animación.

Los códigos son los siguientes:

ARDUINO

int photoRPin = 0; 
int minLight;
int maxLight;
int lightLevel;
int adjustedLightLevel;
int oldLightLevel;

void setup() {
  Serial.begin(9600);
  
  //Setup the starting light level limits
  lightLevel=analogRead(photoRPin);
  minLight=lightLevel-20;
  maxLight=lightLevel;
  oldLightLevel=lightLevel;
}

void loop(){
   lightLevel=analogRead(photoRPin);
   delay(10);
  
  //auto-adjust the minimum and maximum limits in real time   
   if(minLight>lightLevel){
     minLight=lightLevel;
   }
   if(maxLight<lightLevel){
     maxLight=lightLevel;
   }
   
   //Map the light level to produce a result between 1 and 28.
   adjustedLightLevel = map(lightLevel, (minLight+20), (maxLight-20), 1, 124); 
   adjustedLightLevel = constrain (adjustedLightLevel, 1, 124);
   
   /*Only send a new value to the Serial Port if the 
     adjustedLightLevel value changes.*/
   if(oldLightLevel==adjustedLightLevel){
     //do nothing if the old value and the new value are the same.
   }else{
     //Update the oldLightLevel value for the next round
     oldLightLevel=adjustedLightLevel;
     
     /*Send the adjusted Light level result 
       to Serial port (processing)*/
     Serial.println(adjustedLightLevel);
   } 
}     


PROCESSING

import processing.serial.*;
Serial myPort;
String sensorReading="";

PImage[] movieImage = new PImage[124];

/* The frame variable is  used to control which 
 image is displayed */
int frame = 1;



/* Setup the size of the window. Initialise serial communication with Arduino
 and pre-load the images to be displayed later on. This is done only once.
 I am using COM6 on my computer, you may need replace this value with your
 active COM port being used by the Arduino.*/

void setup() 
{
  size(700, 600);
  background(0);

  myPort = new Serial(this, "COM4", 9600);
  myPort.bufferUntil('\n');

  for (int i=0; i<122; i++) 
  {
    movieImage[i] = loadImage((i+3) + ".jpg");
  }
}




// The draw function controls the animation sequence.

void draw() 
{

  image(movieImage[frame-1],0,0,width,height);  

}

void serialEvent (Serial myPort) {
  sensorReading = myPort.readStringUntil('\n');
  if (sensorReading != null) {
    sensorReading=trim(sensorReading);
    if (sensorReading.length()<2) {
      frame = integerFromChar(sensorReading.charAt(0));
    } else {
      frame = integerFromChar(sensorReading.charAt(0))*10;
      frame += integerFromChar(sensorReading.charAt(1));
    }
  }
}



/* This function used to convert the character received from the
 serial port (Arduino), and converts it to a number */

int integerFromChar(char myChar) {
  if (myChar < '0' || myChar > '9') {
    return -1;
  } else {
    return myChar - '0';
  }
}


No comments:

Post a Comment