How to change the stroke of my triangles with serial port data?

edited June 2017 in Arduino

Hi, I'm new to processing and I'm working with this code.. while I accomplished to fade the background I wonder how to change the stroke weight with the same data the background changes. Thanks in advance. This is my code:

import processing.serial.*;
import ddf.minim.*;
import ddf.minim.signals.*;

Minim minim;
AudioPlayer mySound;

Serial port;
float brightness;



void setup() {
size(1000, 500,P3D);


String portName = Serial.list()[2];
port = new Serial(this, "/dev/cu.usbmodem1421", 9600);
port.bufferUntil('\n');
noFill();
strokeWeight(1);

minim = new Minim(this);
mySound = minim.loadFile("bb.mp3");
mySound.play();
mySound.loop();  
}
int n=0;

void draw ()
{
background (brightness, 0, 25);


translate(width/2,height/2);
for(int i = 0; i < mySound.bufferSize() - 1; i++)  {

stroke(random(250),random(250),random(250));

rotateX(n*-PI/6*0.05);
rotateY(n*-PI/6*0.05);
rotateZ(n*-PI/6*0.05);


triangle (i,i,mySound.left.get(i)*50,mySound.left.get(i)*50,i,i);
}
n++;

}

void serialEvent (Serial port)
{
brightness = float (port.readStringUntil ('\n'));



}

Answers

  • @realdapsis -- without testing your code, but just looking at it:

    Currently your brightness is a variable declared at the top:

    float brightness;
    

    ...and you assign a value to it in your serialEvent() function:

    void serialEvent (Serial port) {
      brightness = float (port.readStringUntil ('\n'));
    }
    

    ...and you use that value in draw() to change the background of the screen:

    background (brightness, 0, 25);
    

    So. Currently in draw you set the stroke like this:

    stroke(random(250),random(250),random(250));
    

    Based on the example of how your brightness works: How could you change this code so that stroke() uses a variable -- and the variable is assigned during your serialEvent?

Sign In or Register to comment.