Monday, January 4, 2016

Processing Code Ultrasonic Project

import processing.serial.*;       //processing serial library

Serial myPort;                       // The serial port
int[] serialInArray = new int[9]; // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
int x;                               //the x coordinate
int y;                               //the y coordinate
int px = x;                          //px and py equal to x and y
int py = y;
int prx;                              //previous x/y for other loops
int pry;
int lx;                               //more x/y alternates
int ly;
int lpx;                              //more px/py alternates
int lpy;                              
int button1val;                       //buton values
int button2val;
int button3val;
int button0val;
int r1 = 0;                        //radius values
int r2 = 0;
int w1= 0;                         //width values
int w2 = 0;
int counter = 0;                   //counters in loops
int counter1 = 0;
int colorval;                      //color values
int colorval1;
int colorval2;
int colorval3;
float colorvala;                   //color float for weird situations

boolean firstContact = false;        // Whether we've heard from the microcontroller

void setup() {
  size(600, 400);  // Stage size


  //println(Serial.list()[0]);

  //String portName = Serial.list()[3];
  myPort = new Serial(this, "COM5", 9600); //setting up communication

  x=300;
  y=300;
  background(0, 0, 0);
}

void draw() {
  
  
  //COLOR SECTION-------------------------------------------------------------------
  colorvala = map(colorval, 0, 255, 0, 1024);
  if(colorvala >0 && colorvala<171) //WHITE
  {
    colorval1 = 255;
    colorval2 = 255;
    colorval3 = 255;
  }
  else if(colorvala >171 && colorvala < 342) //GREY
  {
   colorval1 = 96;
   colorval2 = 96;
   colorval3 = 96;
  }
   else if(colorvala >342 && colorvala < 513) //BLUE
  {
   colorval1 = 0;
   colorval2 = 0;
   colorval3 = 255;
  }
  
    else if(colorvala >513 && colorvala < 684) //BLUE
  {
   colorval1 = 0;
   colorval2 = 153;
   colorval3 = 0;
  }
    else if(colorvala >684 && colorvala < 855) //BLUE
  {
   colorval1 = 255;
   colorval2 = 255;
   colorval3 = 0;
  }
    else if(colorvala >855 && colorvala < 1024) //BLUE
  {
   colorval1 = 255;
   colorval2 = 0;
   colorval3 = 0;
  }
  
  
  fill(colorval1, colorval2, colorval3); 
  stroke(255);
  strokeWeight(3);
  ellipse(575,375, 15, 15);
  println(colorval);
  
  //END COLOR SECTION-------------------------------------------------------------------
  
  
  if (x >= 2 && x <= 35 && y >= 2 && y <= 35)
  {
    lx=x;
    ly=y;
    lpx=px;
    lpy=py;

    if (button2val==0)
    {
      

      if (counter <1)
      {
        prx = x;
        pry = y;
        println(counter);
        counter = counter + 1;
      }
      if (counter>=1)
      {
        println(counter);
        stroke(0);
        ellipse(prx*20, pry*20, r1, r2);
        r1=r1+5;
        r2=r2+5;
        fill(255);
      }
     
     
    } 
    else
    {     
      r1=0;
      r2=0;
     counter=0;
          
    }


    if (button0val == 0)  //if the button is pressed
    {
      if (counter1 < 1)      //if the counter is less than zero
     {
        prx = x;            //this other previous x/y is x/y
        pry = y;            
        counter1 = counter1 + 1; //this pretty much locks the shape into place,
        //so it doesn't move
      }
      if (counter1 >= 1) //if the counter is greater than one
      {
        rectMode(CENTER);    //make a rectangle
        stroke(0);
        rect((prx*20), (pry*20), w1, w2);  
        w1=w1+5;           //that keeps expanding
        w2=w2+5;
        fill(255);
        
      }
    }
   else {
      w1=0;        //otherwise no circle 4 u
      w2=0;
     counter1 = 0; //unlock
    }


    if (button1val == 0)
    {
      //alternate line code if I ever want to use it
      //this is where px and py come in. only really used here.
      //stroke(255, 255, 255);
      //strokeWeight(1);
      //line(((lx+lpx)/2)*20, ((lx+lpx)/2)*20, lpy*20, lpx*10);
      stroke(0);
      fill(colorval1, colorval2, colorval3);
      ellipse(((lx+lpx)/2)*20, ((ly+lpy)/2)*20, 20, 20); //turn the x and y into variables with that formula.
      //then do previous variables with that same formula

      print(ly); //printing for bug checking
      print(" ");
      print(lx);
      print(" ");
      print(lpy);
      print(" ");
      println(lpx);
      print("b1 =");
      println(button1val);
      print("b2 =");
      println(button2val);
    }

    if (button3val == 0) { //if I press the clear button 
      background(0);  //reset the background. This covers everthing up
      //looks like it is being all erased, but we just cover it up
    }
  }
}


void serialEvent(Serial myPort) { //communication setup from arduino
//see video description to the link that I used some code for this section from
  // read a byte from the serial port:
  int inByte = myPort.read();
  // if this is the first byte received, and it's an A,
  // clear the serial buffer and note that you've
  // had first contact from the microcontroller.
  // Otherwise, add the incoming byte to the array:
  if (firstContact == false) {
    if (inByte == 'A') {
      myPort.clear();          // clear the serial port buffer
      firstContact = true;     // you've had first contact from the microcontroller
      myPort.write('A');       // ask for more
    }
  } else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    serialCount++;

    // If we have 3 bytes:
    if (serialCount > 8) {   //if it is divisible by 3
      x = serialInArray[0];
      y = serialInArray[1];
      px = serialInArray[2];
      py = serialInArray[3];
      button1val = serialInArray[4];
      button2val = serialInArray[5];
      button3val = serialInArray[6];
      button0val = serialInArray[7];
      colorval = serialInArray[8];


      // Send a capital A to request new sensor readings:
      myPort.write('A');
      // Reset serialCount:
      serialCount = 0;
    }
  }
}

No comments:

Post a Comment