Documente Academic
Documente Profesional
Documente Cultură
IEEE-UCSA 2015
Qu es processign?:
What is Processing?
From http://www.processing.org:
Processing is an open source programming language and environment for people
who want to program images, animation, and interactions.
It is used by students, artists, designers, researchers, and hobbyists for learning,
prototyping, and production.
It is created to teach fundamentals of computer programming within a visual
context and to serve as a software sketchbook and professional production tool.
Processing is developed by artists and designers as an alternative to proprietary
software tools in the same domain.
17/09/15
17/09/15
//ellipse(x,y,a,h);
http://processing.org/reference/ellipse_.html
El color de relleno.
PROCESSING 2D.
- SISTEMA DE COORDENADAS 2D.
12
PROCESSING 2D.
- COLOR Y RELLENO.
El color de los trazos se determinan con la funcin stroke()
stroke(255) => RGB(255, 255, 255) especifica un valor
entre 0-256 niveles de gris.
stroke(128, 0, 128) => Cualquier valor RGB
El grosor de los trazos se puede caracterizar con
strokeWeight()
strokeWeight(5) => Grosor 5
El color de relleno de figuras 2D se especifica mediante la
funcin fill()
fill(128) => RGB(128, 128, 128)
fill(200, 120, 90) => RGB(200, 120, 90)
13
PROCESSING 2D.
background()
Borra la ventana con el color especificado
Ejemplos: background(0)
background(128, 100, 128)
noFill()
Las figuras 2D se dibujaran sin relleno
noStroke()
Las figuras 2D se dibujaran sin trazo (especialmente til en
figuras cerradas, pero afecta a todas, incluso a lneas)
14
PROCESSING 2D.
EJEMPLO SOBRE UNA LNEA
Escribimos un programa prog03_linea y vamos
aadiendo el siguiente cdigo.
size(100, 100);
background(0);
stroke(255);
strokeWeight(5);
line(0, 0, 99, 99);
//tamao de la ventana
//color de fondo
//color del trazo
//grosor del trazo
15
PROCESSING 2D.
EJEMPLO SOBRE UNA LNEA
Escribimos un programa prog04_linea y vamos
aadiendo el siguiente cdigo.
size(200, 200);
background(0);
//utilizamos una estructura de control repetitiva
for (int i=0; i<100; i++) {
stroke(random(255), random(255), random(255));
strokeWeight(random(10));
line(0, 0, random(200), random(200));
}
16
PROCESSING 2D.
ELIPSE Y CRCULOS
ellipseMode()
Cambia el modo en el que los parmetros de la elipse son
interpretados
ellipseMode(CENTER) => (x, y) es el centro de la elipse (es
elmodo por defecto).
ellipseMode(RADIUS) => igual que el anterior, pero ancho y
alto son radios y no dimetros
ellipseMode(CORNER) => (x, y) hace referencia a la esquina
superior izquierda del rectngulo envolvente de la elipse
ellipseMode(CORNERS) => los cuatro parmetros de la elipse
hacen referencia a dos puntos opuestos del rectngulo envolvente
de la elipse
17
PROCESSING 2D.
EJEMPLO DE ELIPSE Y CRCULOS
Escribimos un programa prog05_elipse y vamos
aadiendo el siguiente cdigo.
size(200, 200);
background(0);
stroke(255, 0, 0);
strokeWeight(5);
fill(0, 255, 0);
// (x, y) y dimetros
ellipse(100, 100, 100, 50);
// 2 esquinas opuestas
ellipseMode(CORNERS);
ellipse(0, 0, 50, 100);50);
18
PROCESSING 2D.
PRIMITIVAS DE 2D
Adems de las primitivas que hemos visto estn
tambin las siguientes:
Arcos
Rectngulos
Tringulos
Cuadrilteros
Curvas (Bzier y Catmull-Rom)
Shapes (formas libres)
19
MOVIMIENTO 2D.
float x=0; // coordenadas
float y=0;
float vy = 1; // velocidad eje X
float vx = 2; // velocidad eje y
void setup(){
size(300,300);
fill(255);
}
void draw(){
background(0);
x = x + vx;
y = y + vy;
ellipse(x,y,10,10);
}
20
Trabajaremos con el IDE Arduino y con el IDE Processing para Grficos en conjunto.
17/09/15
17/09/15
17/09/15
Firmata:
A generic protocol for communicating
with microcontrollers like the Arduino
from software on a host computer.
For the Arduino Library, it is included
in Arduino since version 0012, so see
the library reference page. For more
on the Firmata protocol, see
http://firmata.org.
17/09/15
17/09/15
REFERENCIAS:
INFORMACIN PARA CONFIGURAR PROCESSING :
http://playground.arduino.cc/Interfacing/Processing
BAJAR EL PROCESSING:
https://processing.org/download/
17/09/15
17/09/15
17/09/15
17/09/15
17/09/15
17/09/15
17/09/15
EMPEZAREMOS A REALIZAR
UNA SERIE DE EJECICIOS
PRACTICOS
17/09/15
/*
* enciende el led cuando se presiona el botn del ratn
* apaga cuando levantamos el botn
*/
import processing.serial.*; //Importamos las libreras necesarias
import cc.arduino.*;
Arduino arduino; // Crea el objeto Arduino
int ledPin = 13; // Designa el numero de PIN para el LED
void setup() { //Configura el puerto y las seales con las que va a trabajar
size(200, 200);
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(ledPin, Arduino.OUTPUT); // Configura el PIN13 como salida
arduino.digitalWrite(ledPin, Arduino.HIGH); //Enciende el LED
}
void draw() { //Dibuja una ventana de interaccin
if (mousePressed == true) { //pregunta si se ha pulsado el botn del ratn
arduino.digitalWrite(13,Arduino.LOW); // Si se ha pulsado apaga el LED
} else {
arduino.digitalWrite(13,Arduino.HIGH); // Si no esta pulsado enciende el LED
}
}
17/09/15
17/09/15
/*
* Al pulsar con el ratn de forma conmutada el LED parpadea
*/
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int ledPin = 13;
boolean blinkLed = false; //Variable que indica si el LED esta parpadeando
void setup() {size(200, 200);
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(ledPin, Arduino.OUTPUT);
arduino.digitalWrite(ledPin, Arduino.LOW); }
void draw() {
if(blinkLed) {
arduino.digitalWrite(ledPin, Arduino.LOW);
delay(50);
arduino.digitalWrite(ledPin, Arduino.HIGH);
delay(50);
} else {arduino.digitalWrite(ledPin, Arduino.LOW);}
}
void mousePressed() { // Detecta si el ratn esta pulsado
blinkLed = !blinkLed;
}17/09/15
17/09/15
17/09/15
/*
* encendido y apagado gradual de un LED - PWM */
import processing.serial.*; import cc.arduino.*; Arduino arduino;
int pwm=0; //Variable nivel de iluminacin del LED
int ledPin=9;
boolean rising=true; // indica si el encendido es hacia arriba de 0-255
void setup() {size(200, 200);
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(ledPin, Arduino.OUTPUT);
arduino.analogWrite(ledPin, pwm);
}
void draw() {
arduino.analogWrite(ledPin, pwm); // Escribe el valor pwm en la salida PIN9 del
LED
println(pwm); // Escribe en la ventana de datos el valor de la variable pwm
if(rising) { // contador ascendente hasta llegar a 255
pwm+=2;
if(pwm>=255) {
rising=false;
}}else {pwm-=2; // contador descendente hasta llegar a 0
if(pwm<=0) {rising=true;}}}
17/09/15
17/09/15
/*
* Gobierno del nivel de encendido de un led mediante un potencimetro
*/
import processing.serial.*;import cc.arduino.*;
Arduino arduino;int ledPin=9; int potPin=0;int val;
void setup() {size(200, 200);
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(ledPin, Arduino.OUTPUT);}
void draw() {
//lee la seal del potenciometro (0..1024), divide por cuatro (0..255)
val = arduino.analogRead(potPin)/4;
/envia al LED el valor leido y ajustado (0..255) seal PWM
arduino.analogWrite(ledPin, val);
//varia la intensidad del color de la ventana de processing
background(255-val,0,0);
}
17/09/15
17/09/15
/**
* Representacin de valor procedente de un sensor.
*/
import processing.serial.*;import cc.arduino.*; int[] xvals; // SAVE valores ledos
int val; // Valor ledo
int arrayindex = 0; // Puntero o ndice del array
Arduino arduino;
int potPin=0;
void setup(){size(256, 256);xvals = new int[width];
arduino = new Arduino(this, Arduino.list()[0], 57600);}
void draw(){background(0); // shift array left by one
for(int i=1; i<width; i++) {xvals[i-1] = xvals[i];}
// aade un nuevo valor al final del array - //lee la entrada analgica (0..1024),
divide su valor por 4 (0..255) // to stay within canvas drawing limits
val = arduino.analogRead(potPin);
xvals[width-1] = val/4;
// dibuja el array
for(int i=1; i<width; i++) {stroke(255);
point(i, 255-xvals[i]); //flip y coordinate so 0 is at bottom
}
textAlign(RIGHT);
text(val, 200, 30);
text(5.0*(xvals[width-1]/255.0)+"V",200,60);
}
17/09/15
17/09/15