Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var serial; //variable to hold an instance of the serial port library
- var portName = '/dev/cu.usbmodem1411'; //fill in with YOUR port
- var buttery;
- var x;
- var y;
- var r = 0;
- var g = 0;
- var b = 0;
- var c;
- var sensor1 = 0;
- function setup() {
- createCanvas(600, 400);
- x = width;
- y = height;
- serial = new p5.SerialPort();
- serial.on('connected', serverConnected);
- serial.on('open', portOpen);
- serial.on('data', serialEvent);
- serial.on('error', serialError);
- serial.on('close', portClose);
- serial.open(portName);
- }
- function draw() {
- background(r, g, b);
- stroke(0, 0, 0);
- fill("red");
- rect((width / 5), height - 100, (width / 5), -r);
- fill("green");
- rect(((width / 5) * 2), height - 100, (width / 5), -g);
- fill("blue");
- rect((width / 5) * 3, height - 100, (width / 5), -b);
- stroke("white");
- fill("black");
- rect((width / 5), height - 100, (width / 5), 50);
- fill("black");
- rect((width / 5) * 2, height - 100, (width / 5), 50);
- fill("black");
- rect((width / 5) * 3, height - 100, (width / 5), 50);
- textSize(32);
- fill("white");
- text("Color Analyzer",width/3-10,30);
- text(r, width / 5 + 20, height - 65);
- text(g, ((width / 5)*2)+35 , height - 65);
- text(b, ((width / 5)*3)+30 , height - 65);
- }
- //on mouse event send a byte (0-255) to arduino
- function mousePressed(){
- //we want to send INTERGER
- var outfo = int(map(mouseX,0,width,0,255));
- serial.write(outfo);
- print("Sending out:" + outfo);
- }
- function keyPressed(){
- // serial.write(150);
- if(key === "R"){
- r=255;
- serial.write(150); //send the key
- print("pressed R");
- }else if(key ==="G"){
- g=255;
- serial.write(180);
- print("pressed G");
- }else if(key ==="B"){
- b=255;
- serial.write(190);
- print("pressed B");
- }else if(key ==="L"){
- serial.write(200);
- print("pressed RAINBOW");
- var k=0;
- r=150;
- g=100;
- b=50;
- while(k<100){
- r=int(random(0,255));
- g=int(random(0,255));
- b=int(random(0,255));
- k++;
- }
- }
- }
- function serverConnected() {
- console.log('connected to the server');
- }
- function portOpen() {
- console.log('the serial port opened!');
- }
- //THIS IS WHERE WE RECEIVE DATA!!!!!!
- //make sure you're reading data based on how you're sending from arduino
- function serialEvent() {
- //receive serial data here
- //read a string from the serial port
- var inString = serial.readLine(); //read line listens for carriage return
- //check if we recieve
- /* ONE VALUE
- if(inString.length > 0){
- sensor1 = Number(inString);
- } */
- //MORE THAN ONE VALUE
- //save string to array
- //parse by commas
- if (inString.length > 0) {
- var inputArray = split(inString, ",");
- r = (inputArray[0]);
- g = (inputArray[1]);
- b = (inputArray[2]);
- c = color(inputArray[0], inputArray[1], inputArray[2]);
- }
- //if so, convert and save
- //console.log(sensor1);
- }
- function serialError(err) {
- console.log('something went wrong with the port. ' + err);
- }
- function portClose() {
- console.log('the port was closed');
- }
- // get the list of ports:
- function printList(portList) {
- // portList is an array of serial port names
- for (var i = 0; i < portList.length; i++) {
- // Display the list the console:
- print(i + " " + portList[i]);
- }
- }
Add Comment
Please, Sign In to add comment