Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Libreria para comunicarse con arduino
- import processing.serial.*;
- // Declarando el nombre de nuestro puerto serial
- Serial myPort;
- //Variable para leer el valor recibido de arduino
- String val;
- void setup() {
- size(500, 500);
- background(0);
- // Declarando el puerto Serial y la velocidad de transferencia
- myPort= new Serial(this, "COM3", 9600);
- myPort.bufferUntil('\n');
- }
- //Declarando nuesvos arreglos de objetos
- Agua[] atomos_agua=new Agua[1];
- CO2[] atomos_CO2=new CO2[1];
- Oxigeno[] atomos_oxigeno=new Oxigeno[1];
- void draw() {
- //Inicializando los objetos
- atomos_agua[0]=new Agua();
- atomos_CO2[0]=new CO2();
- atomos_oxigeno[0]=new Oxigeno();
- // Cuando se esten recibiendo datos
- if (myPort.available()>0) {
- val = myPort.readStringUntil('\n');
- // Lee el dato recibido
- if (val!=null) {
- // Cuando el dato es diferente de nulo limpia la pantalla
- println(val);
- background(0);
- }
- } else {
- // cuando no se reciben datos
- if (keyPressed) {
- //si se presiona la letra a envia un dato y muestra en pantalla el atomo de agua
- if (key == 'a') {
- background(0);
- myPort.write("a");
- atomos_agua[0].display();
- }
- //si se presiona la letra d envia un dato y muestra en pantalla el atomo de CO2
- else if (key == 'd') {
- background(0);
- myPort.write("d");
- atomos_CO2[0].display();
- }//si se presiona la letra o envia un dato y muestra en pantalla el atomo de oxigeno
- else if (key == 'o') {
- background(0);
- myPort.write("o");
- atomos_oxigeno[0].display();
- }
- }
- }
- }
- // se declara la clase
- class Agua
- {
- int x;
- int y;
- float tam;
- // constructor de la clase donde se define la posicion y tamaño
- Agua()
- {
- x=width/2;
- y=(height/2);
- tam=50;
- }
- // metodo que se utiliza para dibujar el atomo
- void display() {
- // Coloca los textos descriptivos
- fill(255);
- textSize(15);
- text("Oxigeno",width/2,350);
- text("Hidrogeno",150,100);
- text("Hidrogeno",290,100);
- stroke(255);
- // Dibuja la lineas que uniran a los elipses
- line(x,y,x-150,y-150);
- line(x,y,x+150,y-150);
- fill(0, 255, 0);
- ellipse(x, y, tam+75, tam+75);
- fill(0, 0, 255);
- ellipse(x+150, y-150, tam, tam);
- ellipse(x-150, y-150, tam, tam);
- }
- }
- // se declara la clase
- class CO2
- {
- int x;
- int y;
- float tam;
- // constructor de la clase donde se define la posicion y tamaño
- CO2()
- {
- x=width/2;
- y=(height/2);
- tam=50;
- }
- // metodo que se utiliza para dibujar el atomo
- void display() {
- // Coloca los textos descriptivos
- fill(255);
- textSize(15);
- text("Oxigeno",150,400);
- text("Oxigeno",150,100);
- text("Carbono",350,height/2);
- stroke(255);
- // Dibuja la lineas que uniran a los elipses
- line(x,y,x,y-150);
- line(x,y,x,y+150);
- fill(255, 0, 0);
- ellipse(x, y, tam+50, tam+50);
- fill(0,255, 0);
- ellipse(x, y+150, tam, tam);
- ellipse(x, y-150, tam, tam);
- }
- }
- // se declara la clase
- class Oxigeno
- {
- int x;
- int y;
- float tam;
- // constructor de la clase donde se define la posicion y tamaño
- Oxigeno()
- {
- x=width/2;
- y=(height/2);
- tam=150;
- }
- // metodo que se utiliza para dibujar el atomo
- void display() {
- // Coloca los textos descriptivos
- fill(255);
- text("Oxigeno",width/2,100);
- fill(0, 150, 255);
- ellipse(x, y, tam, tam);
- }
- }
Add Comment
Please, Sign In to add comment