import processing.serial.*;

Serial port;

boolean DEBUG = false;
int[][] values = new int[2][300];
color[] colors = {color(0,255,255),color(255,255,0)};

void setup(){
  
  size(800,600);
  frameRate(50);
  String[] ports = Serial.list();
  
  if (DEBUG) {
    for (int i = 0; i < ports.length; i++) {
      println(i + ": " + ports[i]);
    }
  } else {
    port = new Serial(this, ports[3], 9600);
  }
  
  pixelDensity(displayDensity());
  
}

void draw(){
  
  background(color(30,30,30));
  stroke(255,255,255);
  stroke(255,255,255);
  textSize(16);
  text("Input Device Value", 30,20);
  
  // Hall Effect sensor Plot
  line(30,250,750,250);
  line(30,30,750,30);
  line(30,30,30,250);
  line(750,30,750,250);
  
  
  
  // Light Sensor plot
  line(30,550,750,550);
  line(30,300,750,300);
  line(30,300,30,550);
  line(750,300,750,550);
  
  for(int i=0;i<2;i++){
     stroke(colors[i]);
     
     for (int j=0;j<299;j++){
       float a = 0.0;
       float b = 0.0;
       
       float divide = 2.4;
       println(divide);
       
       String disp = "";
       if(i==0){
          a = map(values[i][j],0,1023,250,0);
          b = map(values[i][j+1],0,1023,250,0);          
          line(30+(divide * j),a, 30+(divide * (j+1)),b);
       }else{
          a = map(values[i][j],0,1023,550,300);
          b = map(values[i][j+1],0,1023,550,300);
          line(30+(divide * j),a, 30+(divide * (j+1)),b);       
       }
       
       textSize(12);
        disp = "Hall Effect =" + str(values[0][299]);
          text(disp, 30,275);
        disp = "Light =" + str(values[1][299]);
          text(disp, 30,575);   
          
  }
  
}

}


void serialEvent(Serial p){
  
  if (p.available() > 0) {
    try {
      String input = p.readStringUntil('\n');
      if (input != null) {
        input = trim(input);
        String [] value = split(input, ',');
        
        for (int i = 0; i < 2; i++) {
          values[i] = append(subset(values[i], 1), int(value[i]));
        }
      }
    } catch (RuntimeException e) {
    }
  }
  
}