SHOW:
|
|
- or go back to the newest paste.
| 1 | import processing.serial.*; | |
| 2 | Serial arduinoport; | |
| 3 | ||
| 4 | int pressionado = 1; | |
| 5 | float x, y; //coordenadas dos vértices | |
| 6 | int raio = 350; | |
| 7 | int graus = 0; | |
| 8 | int w = 300; | |
| 9 | int valor = 0; | |
| 10 | int vaiVolta = 0; | |
| 11 | int[] novosValores = new int[181]; | |
| 12 | int[] velhosValores = new int[181]; | |
| 13 | PFont minhaFonte; | |
| 14 | int radarDist = 0; | |
| 15 | int firstRun = 0; | |
| 16 | ||
| 17 | void setup(){
| |
| 18 | size(750, 450); | |
| 19 | background (0); | |
| 20 | minhaFonte = createFont("verdana", 12);
| |
| 21 | textFont(minhaFonte); | |
| 22 | arduinoport = new Serial(this, Serial.list()[0], 9600); | |
| 23 | } | |
| 24 | ||
| 25 | void draw(){
| |
| 26 | fill(0); | |
| 27 | noStroke(); | |
| 28 | ellipse(raio, raio, 750, 750); | |
| 29 | rectMode(CENTER); | |
| 30 | rect(350,402,800,100); | |
| 31 | if (graus >= 179) { //faz a animação seguir o caminho inverso
| |
| 32 | vaiVolta = 1; | |
| 33 | } | |
| 34 | if (graus <= 1) {
| |
| 35 | vaiVolta = 0; | |
| 36 | } | |
| 37 | strokeWeight(7); | |
| 38 | if(vaiVolta == 0){
| |
| 39 | for(int i = 0; i<=20; i++){ //cria o efeito de radar
| |
| 40 | stroke(0, (10*i), 0); | |
| 41 | line(raio, raio, raio + cos(radians(graus+(180+i)))*w, raio + sin(radians(graus+(180+i)))*w); | |
| 42 | } | |
| 43 | }else{
| |
| 44 | for(int i = 20; i>=0; i--){ //cria o efeito de radar
| |
| 45 | stroke(0, 200-(10*i), 0); | |
| 46 | line(raio, raio, raio + cos(radians(graus+(180+i)))*w, raio + sin(radians(graus+(180+i)))*w); | |
| 47 | } | |
| 48 | } | |
| 49 | noStroke(); | |
| 50 | fill(0,50,0); | |
| 51 | //primeira varredura | |
| 52 | beginShape(); //inicia o desenho do semicírculo | |
| 53 | for(int i = 0; i<180; i++){
| |
| 54 | x = raio + cos(radians((180+i)))*((velhosValores[i])); | |
| 55 | y = raio + sin(radians((180+i)))*((velhosValores[i])); | |
| 56 | vertex(x, y); | |
| 57 | } | |
| 58 | endShape(); //termina o desenho do semicírculo | |
| 59 | //segunda varredura | |
| 60 | fill(0,110,0); | |
| 61 | beginShape(); | |
| 62 | for(int i = 0; i<180; i++){
| |
| 63 | x = raio + cos(radians((180+i)))*((novosValores[i])); | |
| 64 | y = raio + sin(radians((180+i)))*((novosValores[i])); | |
| 65 | vertex(x, y); | |
| 66 | } | |
| 67 | endShape(); | |
| 68 | //media | |
| 69 | fill(0,170,0); | |
| 70 | beginShape(); | |
| 71 | for (int i = 0; i < 180; i++) {
| |
| 72 | x = raio + cos(radians((180+i)))*((novosValores[i]+velhosValores[i])/2); | |
| 73 | y = raio + sin(radians((180+i)))*((novosValores[i]+velhosValores[i])/2); | |
| 74 | vertex(x, y); | |
| 75 | } | |
| 76 | endShape(); | |
| 77 | //anel vermelho após duas voltas | |
| 78 | if (firstRun >= 360) {
| |
| 79 | stroke(150,0,0); | |
| 80 | strokeWeight(1); | |
| 81 | noFill(); | |
| 82 | for (int i = 0; i < 180; i++) {
| |
| 83 | if (velhosValores[i] - novosValores[i] > 35 || novosValores[i] - velhosValores[i] > 35) {
| |
| 84 | x = raio + cos(radians((180+i)))*(novosValores[i]); | |
| 85 | y = raio + sin(radians((180+i)))*(novosValores[i]); | |
| 86 | ellipse(x, y, 10, 10); | |
| 87 | } | |
| 88 | } | |
| 89 | } | |
| 90 | // distância entre os anéis do radar | |
| 91 | for(int i = 0; i<=6; i++){
| |
| 92 | noFill(); | |
| 93 | strokeWeight(1); | |
| 94 | stroke(0, 255-(30*i), 0); | |
| 95 | ellipse(raio, raio, (100*i), (100*i)); | |
| 96 | fill(0, 100, 0); | |
| 97 | noStroke(); | |
| 98 | text(Integer.toString(radarDist+50), 380, (305-radarDist), 50, 50); | |
| 99 | radarDist+=50; | |
| 100 | } | |
| 101 | radarDist = 0; | |
| 102 | //desenha as linhas da grade a cada 30 graus | |
| 103 | for(int i = 0; i <= 6; i++){
| |
| 104 | strokeWeight(1); | |
| 105 | stroke(0,55,0); | |
| 106 | line(raio, raio, raio + cos(radians(180+(30*i)))*w, raio + sin(radians(180+(30*i)))*w); | |
| 107 | fill(0, 55, 0); | |
| 108 | noStroke(); | |
| 109 | if (180+(30*i) >= 300) {
| |
| 110 | text(Integer.toString(180+(30*i)), (raio+10) + cos(radians(180+(30*i)))*(w+10), (raio+10) + sin(radians(180+(30*i)))*(w+10), 25,50); | |
| 111 | } else {
| |
| 112 | text(Integer.toString(180+(30*i)), raio + cos(radians(180+(30*i)))*w, raio + sin(radians(180+(30*i)))*w, 60,40); | |
| 113 | } | |
| 114 | } | |
| 115 | //escreve as informações na tela | |
| 116 | noStroke(); | |
| 117 | fill(0); | |
| 118 | rect(350,402,800,100); | |
| 119 | fill(0, 100, 0); | |
| 120 | text("Graus: "+Integer.toString(graus), 100, 380, 100, 50);
| |
| 121 | text("Distância: "+Integer.toString(valor), 100, 400, 100, 50);
| |
| 122 | text("Radar screen code ", 540, 380, 250, 50);
| |
| 123 | fill(0); | |
| 124 | rect(70,60,150,100); | |
| 125 | fill(0, 100, 0); | |
| 126 | text("Legenda:", 100, 50, 150, 50);
| |
| 127 | fill(0,50,0); | |
| 128 | rect(30,53,10,10); | |
| 129 | text("1º varredura:", 115, 70, 150, 50);
| |
| 130 | fill(0,110,0); | |
| 131 | rect(30,73,10,10); | |
| 132 | text("2º varredura:", 115, 90, 150, 50);
| |
| 133 | fill(0,170,0); | |
| 134 | rect(30,93,10,10); | |
| 135 | text("Média", 115, 110, 150, 50);
| |
| 136 | noFill(); | |
| 137 | stroke(150,0,0); | |
| 138 | strokeWeight(1); | |
| 139 | ellipse(29, 113, 10, 10); | |
| 140 | fill(150,0,0); | |
| 141 | text("Movido", 115, 130, 150, 50);
| |
| 142 | stroke(0, 0, 110); | |
| 143 | fill(0, 0, 110); | |
| 144 | rect(630, 53, 10, 10); | |
| 145 | text("start", 715, 70, 150, 50);
| |
| 146 | fill(pressionado); | |
| 147 | } | |
| 148 | ||
| 149 | void mouseClicked(){
| |
| 150 | arduinoport.write(pressionado); | |
| 151 | } | |
| 152 | ||
| 153 | //recolhe dados do radar | |
| 154 | void serialEvent (Serial arduinoport) {
| |
| 155 | String xString = arduinoport.readStringUntil('\n'); // lê uma linha por vez
| |
| 156 | if (xString != null) {
| |
| 157 | xString = trim(xString); //retira os espaços em branco | |
| 158 | String getX = xString.substring(1, xString.indexOf("V")); // pega a posição do servo
| |
| 159 | String getV = xString.substring(xString.indexOf("V")+1, xString.length()); // pega a distância
| |
| 160 | graus = Integer.parseInt(getX); | |
| 161 | valor = Integer.parseInt(getV); | |
| 162 | velhosValores[graus] = novosValores[graus]; | |
| 163 | novosValores[graus] = valor; | |
| 164 | //valor para duas voltas | |
| 165 | firstRun++; | |
| 166 | if (firstRun > 360) {
| |
| 167 | firstRun = 360; | |
| 168 | } | |
| 169 | } | |
| 170 | } |