Okay, I made a program that uses a accelerometer on a Arduino Circuit Playground that tracks where the accelerometer is heading, and now in need to turn the “point(x,y);” into a drawing, where it places a point where the accelerometer goes, kind of like using a mouse to draw on Paint.net, except your holding a arduino. Here is my current code :
int numDataPoints = 50000;
int dataIndex = 1;
String[] dataStrings = new String[numDataPoints]; // Save up to 10k values
import processing.serial.*;
float[] portValues = new float[8];
Serial myPort;
String inString;
void setup()
{
size(750,750);
frameRate(60);
myPort = new Serial(this, "/dev/cu.usbmodem1411", 9600);
for(int i = 0; i<8; i++)
{
portValues[i] = 0;
}
dataStrings[0] = "x,y,z,leftButton,rightButton,lightSensor,soundSensor,tempSensor";
}
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() {
background(255); // make a white background
// this if statement makes sure that Processing is actually
// reading data from the Circuit Playground BEFORE it runs the function
// processSensorValues()
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);
}
// get the x value from the acceleromoter, use to move object horizontally
float x = map(portValues[1],-10,10,0,width);
// get the y value from the accelerometer, use to move object vertically
float y = map(portValues[0],-10,10,0,height);
// draw point using accelerometer values for position
strokeWeight(10);
stroke(1,1,1);
point(x,y);
// Show the data coming in from the Circuit Playground in the
// console. You'll see a stream of numbers "flowing" in the
// console which is below the code window when the code is
// running.
println(inString);
}
// this code gets data from the Circuit Playground
// and packages it up inside of an array. You can go
// here to learn more about arrays in Processing:
// https://processing.org/reference/Array.html
//
// There is some error checking here to make sure the
// Circuit Playground is reporting values
// the code is still a bit buggy. If you have any errors
// in lines 138 - 164, just press stop and try again.
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;
}
void serialEvent(Serial p) {
inString = myPort.readStringUntil(10);
}
Can anyone help me?