16th INTERFACE AND APPLICATION PROGRAMMING

Write the program with processing 3.1.1

import processing.serial.*;

 

Serial serial;

int val;

int x = 0;

 

void setup(){

  size(720,640);

  serial = new  Serial(this,"COM3",2400);

}

 

void draw(){

  background(80,80,80);

 

  if(serial.available()>0){

    val = serial.read();

}

 

  textSize(50);

  text(val,width/2-60,height/2+150);

 

  fill(255-(val-110) * 5,(val-110) * 5,0);

  noStroke();

 

  rect(width/2-50,height/2+100,80,-(val-100)*4);

}

 

First I set the size and color of the window, then I set the coordinate position to make sure the rectangle and the text in the middle of the display.

 

 

Because the sensor's value is vary small, I increased the number of variables, to making the change of the rectangular more prominent.

This app is use my final project procedures and distance sensor. When the bullet magazine is full, there is a green rectangle in the middle of the display.

 

If the bullets decrease the amount, the rectangular will be shorter, and the color will turn yellow→orange→finally→red.

download data

 

 

EMAIL: 626919025@qq.com    HOMEPAGE: www.li-shang.cc