Back to Parent

import processing.serial.*;

PImage img;
int a = 0;
String buff = "";
String id = "";
boolean on1 = false;
boolean on2 = false;
boolean on3 = false;
boolean on4 = false;
long index = 0;
Serial myPort;  
void setup() {
  size(2736, 1824);
  background(0);
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[2], 9600);
}

void draw() {
  while (myPort.available() > 0) {
    serialEvent(myPort.read()); 
  }
  background(0);
  if (on1 == true){
    long num = (index % 4) + 1;
    img = loadImage(num+".jpg");
    image(img, int((width-img.width)/2), int((height-img.height)/2));
  }
  else if (on2 == true){
    long num = (index % 4) + 1 + 4;
    img = loadImage(num+".jpg");
    image(img, int((width-img.width)/2), int((height-img.height)/2));
  }
  else if (on3 == true){
    long num = (index % 4) + 1 + 8;
    img = loadImage(num+".jpg");
    image(img, int((width-img.width)/2), int((height-img.height)/2));
  }
  else if (on4 == true){
    long num = (index % 4) + 1 + 12;
    img = loadImage(num+".jpg");
    image(img, int((width-img.width)/2), int((height-img.height)/2));
  }
 

}

void serialEvent(int serial) {
  // Get all the information in buffer string until new line
  if(serial != '\n') { 
    buff += char(serial);
  } 
  /* Trim the buff to get the ID Hexadecimal
     This depends on how you send the data */
  else {   
   id = trim(buff.substring(0));
   //println(id);
   if (id.equals("1")) {
     index ++;
   } 
   else if (id.equals("2")) {
     on1 = true;
     on2 = false;
     on3 = false;
     on4 = false;
     index = 0;
   }
   else if (id.equals("3")) {
     on1 = false;
     on2 = true;
     on3 = false;
     on4 = false;
     index = 0;
   }  
   else if (id.equals("4")) {
     on1 = false;
     on2 = false;
     on3 = true;
     on4 = false;
     index = 0;
   }
   else if (id.equals("5")) {
     on1 = false;
     on2 = false;
     on3 = false;
     on4 = true;
     index = 0;
   }  
   else if (id.equals("6")) {
     on1 = false;
     on2 = false;
     on3 = false;
     on4 = false;
     index = 0;
   }  
   
   
   // Reset Buffer String
   buff = "";
  }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0