/****************************************************************************
   Week12 Output device  
 *****************************************************************************/

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
int cnt= 0;


void setup() 
{
  size(300,300);
  myPort = new Serial(this, "/dev/cu.usbserial-A90808PQ", 9600);
  background(255);             // Set background to white
}

void draw()
{
  background(255);  
  
  if((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150)){
    myPort.write('a'); 
    text("Motor ON ", 50,25);
    fill(255, 0, 0);
    
  }else{
    myPort.write('s'); 
    text("Motor OFF ", 50,25);
    fill(0, 0, 0);
    
  }
  rect(50, 50, 100, 100); 
    
}


void serialEvent(Serial myPort) {
 // readLight();
}

/*void readLight(){
  
 val = myPort.read();         // read it and store it in val
   // println(val);
   // println ("cnt = " + cnt);  
  
  switch(cnt){
    case 0:
        if(val==1){ cnt = 1;}
        break;      
    case 1:
        if(val==2){ cnt = 2;
        }else if(val!=1) { 
          cnt=0;
        }
       break;
    case 2:
            if(val==3){ cnt = 3;
        }else{
          cnt=0;
        }
       break;
    case 3:
            if(val==4){ cnt = 4;
        }else{
          cnt=0;
        }
       break;
   case 4:
      ltL = val;
      cnt = 5;
      break;
   case 5:
      ltH = val;     
      ltDat = 256*ltH + ltL;
      filter = (1-eps)*filter + eps*ltDat;
      println ("filter = " + filter);  
      cnt = 0;
    
   break;   
  }
  
}
*/
