Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int b = 1;
- int numDataPoints = 50000;
- int dataIndex = 1;
- String[] dataStrings = new String[numDataPoints]; // Save up to 10k values
- // loads a library needed to establish a connection to a serial device
- // in our case, the serial device is a Circuit Playground
- import processing.serial.*;
- // create a new array that will hold on to the sensor values
- // from the Circuit Playground
- float[] portValues = new float[8];
- // create a new serial connection
- Serial myPort;
- String inString;
- void setup (){
- size (300,300);
- background(255,255,255);
- myPort = new Serial(this, "/dev/cu.usbmodem1421", 9600);
- for(int i = 0; i<8; i++)
- {
- portValues[i] = 0;
- }
- dataStrings[0] = "x,y,z,leftButton,rightButton,lightSensor,soundSensor,tempSensor";
- }
- // convert float data to string data in order to save to a file
- String buildDataString(float[] v) {
- String result = "";
- for(int i = 0; i<v.length-1; i++) {
- result += str(v[i]) + ",";
- }
- result += str(v[7]);
- return result;
- }
- void draw()
- {
- // changes background color
- if(portValues[4] == 1) { // if the left button is pressed...
- background (random(255),random(255),random(255));
- } else {
- background(255,255,255);// if no buttons are pressed
- }
- // Mondrian Code
- line(0,100,167,100);
- line(57,0,57,100);
- line(167,0,167,190);
- line(0,190,250,190);
- line(130,190,130,250);
- line(250,143,300,143);
- line(72,250,72,300);
- line(250,250,250,300);
- line(142,175,175,175);
- line(67,175,67,190);
- line(67,153,0,153);
- line(285,0,285,143);
- line(142,0,142,250);
- line(230,230,0,230);
- line(180,250,180,190);
- line(130,200,130,175);
- line(250,190,300,190);
- line(67,33,142,33);
- line(117,58,117,175);
- line(117,58,67,58);
- line(117,33,117,0);
- line(142,33,167,33);
- line(142,58,167,58);
- line(285,10,250,10);
- line(285,75,250,75);
- line(230,230,230,165);
- fill(255,0,0);
- rect(0,0,67,100);
- fill(0,0,255);
- rect(67,100,75,75);
- fill(0,0,255);
- rect(0,190,130,60);
- fill(0,255,0);
- rect(167,0,83,190);
- fill(255,0,0);
- rect(230,230,70,70);
- fill(0,255,0);
- rect(130,250,50,50);
- fill(255,255,0);
- rect(142,33,-25,25);
- fill(255,255,0);
- rect(300,10,-15,65);
- // Recognizing and stores data from arduino.
- if (inString != null) {
- portValues = processSensorValues(inString); // get data
- // manage data points
- dataIndex++;
- if(dataIndex > numDataPoints - 1) {
- dataIndex = 1;
- }
- dataStrings[dataIndex] = buildDataString(portValues);
- saveStrings("values.csv",dataStrings);
- text(dataIndex,width-80,40);
- }
- // Change the color of recangles based on the button pressed.
- if(portValues[3] == 1) { // if the right button is pressed...
- fill (0,0,255); //rearange color patter
- fill(random(255),random(255),random(255));
- rect(0,0,67,100);
- fill(random(255),random(255),random(255));
- rect(67,100,75,75);
- fill(random(255),random(255),random(255));
- rect(0,190,130,60);
- fill(random(255),random(255),random(255));
- rect(167,0,83,190);
- fill(random(255),random(255),random(255));
- rect(230,230,70,70);
- fill(random(255),random(255),random(255));
- rect(130,250,50,50);
- fill(random(255),random(255),random(255));
- rect(142,33,-25,25);
- fill(random(255),random(255),random(255));
- rect(300,10,-15,65);
- }
- }
- float[] processSensorValues(String valString) {
- String[] temp = {"0", "0", "0", "0", "0", "0", "0", "0"};
- temp = split(valString,"\t");
- if(temp == null) {
- for(int i = 0; i<8; i++) {
- temp[i] = "0";
- }
- }
- float[] vals = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
- for(int i = 0; i<8; i++)
- {
- if(temp != null)
- {
- vals[i] = float(temp[i]);
- }
- else
- {
- vals[i] = 0;
- }
- }
- return vals;
- }
- // read new data from the Circuit Playground
- void serialEvent(Serial p) {
- inString = myPort.readStringUntil(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement