Components and supplies
USB-A to Micro-USB Cable
Arduino Nano R3
Apps and platforms
Processing
Project description
Code
tachi_train4
processing
processing source code
1/** Example of selecting a particular Serial Port 2 3* pautax: ripete la porta seriale selezionata sull'area nera 4* gauge 3 scan continuo 5 6 * from the list of all possibilities returned 7 * in Serial.list(). 8 * 9 * Uses showInputDialog box kindly suggested to 10 * me by GoToLoop. Thank you, GoToLoop! 11*/ 12 import processing.serial.*; 13 import static javax.swing.JOptionPane.*; 14 15Serial myPort; // Create object from Serial class 16String StringFromSerialLine; // Data received from the serial port 17String OldStringFromSerialLine; // Last data Data received from the serial port 18String ExtractStr; // stringa del valore esempi da "PWM1=20| DIR1=1 | PWM2=0 | DIR2=0 | MOTORON=1" PWM1 vale "20" 19int ExtractVal; //come sopra ma convertito in intero 20int LenSFSL; // LenghtStringFromSerialLine 21 22final boolean debug = true; 23 24Gauge Power1, minute, hour; 25int Power1old, minold, hourold; 26int Power1_val = 0; 27 28PImage img; 29PImage img1; 30PImage img2; 31 32void setup(){ 33 size(790, 600); 34 Power1 = new Gauge(250, 0, 255, 25, "Power1"); //(width, begin, end, quotation scale, name gauge) 35 minute = new Gauge(250, 0, 255, 25, "Power2"); 36 hour = new Gauge(250, 0, 255, 25, "Power3"); 37 Power1old = 100; //val can never be 100, first run will always draw 38 minold = 100; 39 hourold = 100; 40 41 42 43 String COMx, COMlist = ""; 44/* 45 Other setup code goes here - I put this at 46 the end because of the try/catch structure. 47*/ 48 try { 49 if(debug) printArray(Serial.list()); 50 int i = Serial.list().length; 51 if (i != 0) { 52 if (i >= 2) { 53 // need to check which port the inst uses - 54 // for now we'll just let the user decide 55 for (int j = 0; j < i;) { 56 COMlist += char(j+'a') + " = " + Serial.list()[j]; 57 if (++j < i) COMlist += ", "; 58 } 59 COMx = showInputDialog("Which COM port is correct? (a,b,..):\ 60"+COMlist); 61 if (COMx == null) exit(); 62 if (COMx.isEmpty()) exit(); 63 i = int(COMx.toLowerCase().charAt(0) - 'a') + 1; 64 } 65 String portName = Serial.list()[i-1]; 66 if(debug) println(portName); 67 myPort = new Serial(this, portName, 115200); // change baud rate to your liking 68 myPort.bufferUntil('\n'); // buffer until CR/LF appears, but not required.. 69 } 70 else { 71 showMessageDialog(frame,"Device is not connected to the PC"); 72 exit(); 73 } 74 } 75 catch (Exception e) 76 { //Print the type of error 77 showMessageDialog(frame,"COM port is not available (may\ 78be in use by another program)"); 79 println("Error:", e); 80 exit(); 81 } 82 83 84 85 86 87 88 89 90 // I know that the first port in the serial list on my mac 91 // is Serial.list()[0]. 92 // On Windows machines, this generally opens COM1. 93 // Open whatever port is the one you're using. 94// String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port 95// myPort = new Serial(this, portName, 115200); 96 97 98 99//draw grid 100// translate(width/2, height/2); 101/*B 102 for ( int i = 0 ; i < width ; i+=49) { 103 for ( int j = 0 ; j < height ; j+=49) { 104 // 105 line(i, j, i+25, j); 106 line(i, j, i, j+25); 107 line(i+25, j, i+25, j+25); 108 line(i, j+25, i+25, j+25); 109 //line(i, j, 10, 10); 110 } 111 } 112*/ 113 114 115 116 117 // spia moton 118 fill( 50, 50, 50 ); // colore grigio Drives off 119 //strokeWeight( 3 ); 120 stroke( 255 ); 121 ellipse( 50, 250, 40, 40 ); 122 textSize(18); 123 fill(0, 0, 0); 124 text("POWER", 17, 295); 125 126 // spia boost 127 fill( 50, 50, 50 ); // colore grigio Drives off 128 ellipse( 160, 250, 40, 40 ); 129 textSize(18); 130 fill(0, 0, 0); 131 text("BOOST", 130, 295); 132 133 134 135 //scambio 136 img = loadImage( "../scambio.jpg" ); 137 image( img, 250, 250 ); 138 image( img, 500, 250 ); 139 image( img, 250, 425 ); 140 image( img, 500, 425 ); 141 text("SCAMBIO 2-1", 270, 360); 142 text("SCAMBIO 4-3", 520, 360); 143 text("SCAMBIO 6-5", 270, 535); 144 text("SCAMBIO 8-7", 520, 535); 145 146 img1 = loadImage( "../scambio1.jpg" ); 147 img2 = loadImage( "../scambio2.jpg" ); 148 149 //frecce della direzione 150 fill( 50, 50, 50 ); // colore grigio Drives off 151 152 // indietro 1 153 //strokeWeight( 1 ); 154 beginShape(); 155 vertex(75,325); 156 vertex(75,375); 157 vertex(40,350); 158 endShape(CLOSE); 159 160 // avanti 1 161 beginShape(); 162 vertex(100,325); 163 vertex(100,375); 164 vertex(135,350); 165 endShape(CLOSE); 166 167 // indietro 2 168 beginShape(); 169 vertex(75,450); 170 vertex(75,500); 171 vertex(40,475); 172 endShape(CLOSE); 173 174 // avanti 2 175 beginShape(); 176 vertex(100,450); 177 vertex(100,500); 178 vertex(135,475); 179 endShape(CLOSE); 180 181 fill(0, 0, 0); 182 text("LINEA 1", 40, 400); 183 text("LINEA 2", 40, 525); 184 185 186} 187 188void draw(){ 189 190 /* 191 Power1_val++; 192 if (Power1_val > 59){ 193 Power1_val = 0; 194 } 195 */ 196 197 // drw3gauge(); 198 199 if ( myPort.available() > 0) 200 { // If data is available, 201 StringFromSerialLine = myPort.readStringUntil('\n'); // read it and store it in StringFromSerialLine 202 } 203 204 if (StringFromSerialLine != null) { 205 206 if (StringFromSerialLine != OldStringFromSerialLine){ 207 println(StringFromSerialLine); //print it out in the console 208 OldStringFromSerialLine = StringFromSerialLine; //save 209 210 LenSFSL = StringFromSerialLine.length(); 211 //ora si decodificano i valori 212 //estrae valore di PWM1 da stringa tipo "PWM1=20| DIR1=1 | PWM2=0 | DIR2=0 | MOTORON=1" 213 if (LenSFSL > 3){ 214 ExtractStr = StringFromSerialLine.substring(0, 4); 215 print("LenSFSL "); 216 println(LenSFSL); 217 218 switch(ExtractStr) { 219 220 case "PWM1": 221 print(", ExtractStr "); 222 print(ExtractStr); 223 print(", Trovato PWM1 "); // 224 ExtractStr = StringFromSerialLine.substring(5); 225 print(", ExtractStr "); 226 print(ExtractStr); 227 ExtractVal = Integer.parseInt(ExtractStr.trim()); 228 //ExtractVal = int(ExtractStr); 229 print(" , ExtractVal "); 230 println(ExtractVal); // 231 Power1_val = ExtractVal; 232 break; 233 234 case "MOTN": 235 print(", ExtractStr "); 236 print(ExtractStr); 237 print(", Trovato MOTN "); // 238 ExtractStr = StringFromSerialLine.substring(5); 239 print(", ExtractStr "); 240 print(ExtractStr); 241 ExtractVal = Integer.parseInt(ExtractStr.trim()); 242 //ExtractVal = int(ExtractStr); 243 print(" , ExtractVal "); 244 println(ExtractVal); // 245 if (ExtractVal == 0){ 246 fill( 50, 50, 50 ); // colore grigio Drives off 247 } else { 248 //fill( 255, 0, 0 ); // colore rosso Drives on 249 fill( 0, 255, 0 ); // colore verde Drives on 250 } 251 //strokeWeight( 3 ); 252 stroke( 255 ); 253 ellipse( 50, 250, 40, 40 ); 254 break; 255 256 257 258 case "Boos": 259 print(", ExtractStr "); 260 print(ExtractStr); 261 print(", Trovato Boost "); // 262 ExtractStr = StringFromSerialLine.substring(5); 263 print(", ExtractStr "); 264 print(ExtractStr); 265 ExtractVal = Integer.parseInt(ExtractStr.trim()); 266 //ExtractVal = int(ExtractStr); 267 print(" , ExtractVal "); 268 println(ExtractVal); // 269 if (ExtractVal == 0){ 270 fill( 50, 50, 50 ); // colore grigio Drives off 271 } else { 272 fill( 255, 0, 0 ); // colore rosso boost on 273 //fill( 0, 255, 0 ); // colore verde Drives on 274 } 275 //strokeWeight( 3 ); 276 stroke( 255 ); 277 ellipse( 160, 250, 40, 40 ); 278 break; 279 280 //PROCESSA GLI SCAMBI 281 case "RLON": 282 //strokeWeight( 3 ); 283 print(", ExtractStr "); 284 print(ExtractStr); 285 print(", Trovato RLON "); // 286 ExtractStr = StringFromSerialLine.substring(5); 287 print(", ExtractStr "); 288 print(ExtractStr); 289 ExtractVal = Integer.parseInt(ExtractStr.trim()); 290 //ExtractVal = int(ExtractStr); 291 print(" , ExtractVal "); 292 println(ExtractVal); // 293 if (ExtractVal == 0){ 294 image( img1, 250, 250 ); 295 } 296 if (ExtractVal == 1){ 297 image( img2, 250, 250 ); 298 } 299 if (ExtractVal == 2){ 300 image( img1, 500, 250 ); 301 } 302 if (ExtractVal == 3){ 303 image( img2, 500, 250 ); 304 } 305 306 if (ExtractVal == 4){ 307 image( img1, 250, 425 ); 308 } 309 if (ExtractVal == 5){ 310 image( img2, 250, 425 ); 311 } 312 if (ExtractVal == 6){ 313 image( img1, 500, 425 ); 314 } 315 if (ExtractVal == 7){ 316 image( img2, 500, 425 ); 317 } 318 break; 319 320 //processa le direzioni frecce gialle 321 case "DIR1": 322 //strokeWeight( 3 ); 323 print(", ExtractStr "); 324 print(ExtractStr); 325 print(", Trovato DIR1 "); // 326 ExtractStr = StringFromSerialLine.substring(5); 327 print(", ExtractStr "); 328 print(ExtractStr); 329 ExtractVal = Integer.parseInt(ExtractStr.trim()); 330 //ExtractVal = int(ExtractStr); 331 print(" , ExtractVal "); 332 println(ExtractVal); // 333 334 if (ExtractVal == 1){ 335 println("Avanti 1"); // 336 fill( 10, 3, 255 ); // colore grigio Drives off 337 beginShape(); 338 vertex(100,325); 339 vertex(100,375); 340 vertex(135,350); 341 endShape(CLOSE); 342 343 fill( 50, 50, 50 ); // colore grigio Drives off 344 // indietro 1 grigio 345 beginShape(); 346 vertex(75,325); 347 vertex(75,375); 348 vertex(40,350); 349 endShape(CLOSE); 350 351 352 } 353 if (ExtractVal == 0){ 354 println("Indietro 1"); // 355 fill( 255, 255, 0 ); // colore grigio Drives off 356 // indietro 1 357 beginShape(); 358 vertex(75,325); 359 vertex(75,375); 360 vertex(40,350); 361 endShape(CLOSE); 362 363 fill( 50, 50, 50 ); // colore grigio Drives off 364 // avanti 1 365 beginShape(); 366 vertex(100,325); 367 vertex(100,375); 368 vertex(135,350); 369 endShape(CLOSE); 370 } 371 break; 372 373 374 375 376 377//DIR1=1 avanti DIR1=0 indietro 378 379 380 381 //default: 382 //println("Zulu"); // Does not execute 383 //break; 384 } // switch(ExtractStr) 385 } // if (LenSFSL > 3) 386 } // if (StringFromSerialLine != OldStringFromSerialLine) 387 388 } //if (StringFromSerialLine != null) 389 390 391 //------------------------------------------------- 392 393 394 395 396 397 drw3gauge(); 398 399} 400 401 402void drw3gauge(){ 403 //to save resource, only redraw when time has changed 404 if (Power1_val != Power1old){ 405 Power1.update(Power1_val); 406 pushMatrix(); 407 translate(530, 10); 408 Power1.display(); 409 popMatrix(); 410 Power1old = Power1_val; 411 } 412 if (minute() != minold){ 413 minute.update(minute()); 414 pushMatrix(); 415 translate(270, 10); 416 minute.display(); 417 popMatrix(); 418 minold = minute(); 419 } 420 if (hour() != hourold){ 421 //time received is 24 hour notation, gauge has scale of 12 422 if (hour() <= 11){ 423 hour.update(hour()); 424 } else { 425 hour.update(hour()-12); 426 } 427 pushMatrix(); 428 translate(10, 10); 429 hour.display(); 430 popMatrix(); 431 hourold = hour(); 432 } 433} 434 435class Gauge{ 436 PVector gsize; //width & height gauge 437 PVector gscale; //start & end value scale 438 PVector gneedle; //length & angle needle 439 int gsteps; 440 float gline; 441 String gname; 442 443 Gauge(float tempx, float templow, float temphigh, float templine, String tempname){ 444 float gwidth = tempx; 445 float gheight = 26*tempx/35; 446 float glow = templow; 447 float ghigh = temphigh; 448 gline = templine; 449 gname = tempname; 450 gsteps = int(temphigh - templow) + 1; 451 gsize = new PVector(gwidth, gheight); 452 gscale = new PVector(glow, ghigh); 453 gneedle = new PVector(5*gsize.y/8, map(second(), 0, 59, radians(35), radians(145))); 454 } 455 456 void display(){ 457 noStroke(); 458 //backcover 459 fill(50); 460 rect(0, 0, gsize.x, gsize.y); 461 fill(255); 462 textAlign(CENTER); 463 textSize(14); 464 text(gname, gsize.x/2, 15); 465 //scale 466 stroke(255, 200); 467 //strokeWeight(2); 468 for (int i = 0; i < gsteps; i++){ 469 pushMatrix(); 470 translate(gsize.x/2, 11*gsize.y/12); 471 rotate(PI + map(i, gscale.x, gscale.y, radians(35), radians(145))); 472 if (i%gline == 0){ 473 line(gneedle.x-5, 0, gneedle.x+5, 0); 474 translate(gneedle.x+10, 0); 475 rotate(HALF_PI); 476 textSize(9); 477 text(i, 0, 0); 478 } else { 479 point(gneedle.x, 0); 480 } 481 popMatrix(); 482 } 483 noStroke(); 484 //needle 485 stroke(255, 0, 0); 486 //strokeWeight(3); 487 pushMatrix(); 488 translate(gsize.x/2, 11*gsize.y/12); 489 rotate(PI + gneedle.y); 490 line(0, 0, gneedle.x, 0); 491 popMatrix(); 492 noStroke(); 493 //frontcover 494 fill(150, 180); 495 rect(0, 4.5*gsize.y/6, gsize.x, 1.5*gsize.y/6); 496 fill(255, 0, 0); 497 ellipseMode(CENTER); 498 ellipse(gsize.x/2, 11*gsize.y/12, 10, 10); 499 } 500 501 void update(float tempgval){ 502 float gvalue = tempgval; 503 gneedle.y = map(gvalue, gscale.x, gscale.y, radians(35), radians(145)); 504 } 505} 506
Downloadable files
the job
the job
the job
the job
Comments
Only logged in users can leave comments
pautax
0 Followers
•0 Projects
Table of contents
Intro
4
0