Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.serial.*;
- String buff = "";
- int val = 0;
- // zmienne przechowujące wartości składowe R, G i B.
- int wRed, wGreen, wBlue;
- Serial port;
- void setup(){
- // Ustalenie wielkości okna, w którym wyświetlony zostanie kolor.
- size(900,600);
- // Ustawienie portu na port do którego podłączone jest Arduino.
- port = new Serial(this, "COM8", 9600);
- }
- void draw(){
- //Odczyt wartości z portu jeśli jest on dostępny.
- while (port.available() > 0) {
- serialEvent(port.read());
- }
- }
- void serialEvent(int serial) {
- if(serial != '\n') {
- buff += char(serial);
- }
- else {
- int cRed = buff.indexOf("R");
- int cGreen = buff.indexOf("G");
- int cBlue = buff.indexOf("B");
- if(cRed >=0){
- String val = buff.substring(cRed+3);
- wRed = Integer.parseInt(val.trim());
- }
- if(cGreen >=0){
- String val = buff.substring(cGreen+3);
- wGreen = Integer.parseInt(val.trim());
- }
- if(cBlue >=0){
- String val = buff.substring(cBlue+3);
- wBlue = Integer.parseInt(val.trim());
- }
- // Ustawienie tła okna na wykryty kolor.
- background(wRed,wGreen,wBlue);
- // Wyświetlenie odczytanych wartości do okna konsoli.
- println("RED: " + wRed);
- println("GREEN: " + wGreen);
- println("BLUE: " + wBlue);
- // Wyświetlenie wartości wewnątrz okna programu.
- textSize(32);
- text(String.valueOf(wRed), 10, 30);
- fill(0, 102, 153);
- textSize(32);
- text(String.valueOf(wGreen), 10, 60);
- fill(0, 102, 153);
- textSize(32);
- text(String.valueOf(wBlue), 10, 90);
- fill(0, 102, 153);
- //Wyczyszczenie bufora.
- buff = "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement