Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import processing.serial.*;
  2.  
  3. String buff = "";
  4. int val = 0;
  5. // zmienne przechowujące wartości składowe R, G i B.
  6. int wRed, wGreen, wBlue;
  7.  
  8. Serial port;
  9.  
  10. void setup(){
  11.   // Ustalenie wielkości okna, w którym wyświetlony zostanie kolor.
  12.   size(900,600);
  13.   // Ustawienie portu na port do którego podłączone jest Arduino.
  14.   port = new Serial(this, "COM8", 9600);
  15. }
  16.  
  17. void draw(){
  18.   //Odczyt wartości z portu jeśli jest on dostępny.
  19.   while (port.available() > 0) {
  20.     serialEvent(port.read());
  21.   }
  22. }
  23.  
  24. void serialEvent(int serial) {
  25.  
  26.   if(serial != '\n') {
  27.     buff += char(serial);
  28.   }
  29.   else {
  30.     int cRed = buff.indexOf("R");
  31.     int cGreen = buff.indexOf("G");
  32.     int cBlue = buff.indexOf("B");
  33.  
  34.     if(cRed >=0){
  35.       String val = buff.substring(cRed+3);
  36.       wRed = Integer.parseInt(val.trim());
  37.     }    
  38.     if(cGreen >=0){
  39.       String val = buff.substring(cGreen+3);
  40.       wGreen = Integer.parseInt(val.trim());
  41.     }
  42.     if(cBlue >=0){
  43.       String val = buff.substring(cBlue+3);
  44.       wBlue = Integer.parseInt(val.trim());
  45.     }
  46.     // Ustawienie tła okna na wykryty kolor.
  47.     background(wRed,wGreen,wBlue);
  48.     // Wyświetlenie odczytanych wartości do okna konsoli.
  49.     println("RED: " + wRed);
  50.     println("GREEN: " + wGreen);
  51.     println("BLUE: " + wBlue);
  52.  
  53.     // Wyświetlenie wartości wewnątrz okna programu.
  54.     textSize(32);
  55.     text(String.valueOf(wRed), 10, 30);
  56.     fill(0, 102, 153);
  57.  
  58.     textSize(32);
  59.     text(String.valueOf(wGreen), 10, 60);
  60.     fill(0, 102, 153);
  61.  
  62.     textSize(32);
  63.     text(String.valueOf(wBlue), 10, 90);
  64.     fill(0, 102, 153);
  65.     //Wyczyszczenie bufora.
  66.     buff = "";
  67.   }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement