Returning integer value
in
Integration and Hardware
•
1 year ago
I Have a processing code which is talking to my arduino. I need to get the xCur, yCur, and zCur values into draw so i can make visuals that are corresponding with the movement of my accelerometer, this code works and gives me results, but not sure how to return them into draw? This project is due tomorrow, and I would love some help thanks soooo much....
Here is the original code...
import processing.serial.*;
Serial myPort;
int delim = (int)('!');
final int maxSize = 400;
int[] xValues = new int[maxSize];
int[] yValues = new int[maxSize];
int[] zValues = new int[maxSize];
int curIndex = 0;
/********************************************************************
Setup routine
********************************************************************/
public void setup() {
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
println("delimiter:" + delim);
frameRate(600);
background(0);
size(maxSize,700);
}
/********************************************************************
Draw routine
********************************************************************/
public void draw() {
fill(10,10,10,40);
rect(0,0,width,height);
//read data from the accelerometer
try{
readDataFromSensor();
}catch(Exception e){}
//start plotting the datasets
plotArray(0);
plotArray(1);
plotArray(2);
}
/********************************************************************
This routine reads data from sensor
********************************************************************/
void readDataFromSensor(){
//This if block manages the data from the accelerometer
if(myPort.available() > 0){
//read bytes from the port until the delimiter is found
String data = myPort.readStringUntil(delim);
if(data != null){
//grab the x,y,z accel values from the string that was just read
int xDelim = data.indexOf(",");
int xCur = Integer.parseInt(data.substring(0,xDelim));
int yDelim = data.lastIndexOf(",");
int yCur = Integer.parseInt(data.substring(xDelim+1,yDelim));
int zDelim = data.length()-1;
int zCur = Integer.parseInt(data.substring(yDelim+1,zDelim));
// println(xCur); println(yCur); println(zCur);
//add these values into the respective arrays
xValues[curIndex] = xCur/5;
yValues[curIndex] = yCur/5;
zValues[curIndex] = zCur/5;
//manage the index and see it doesn't overflow
curIndex++;
if(curIndex == maxSize) curIndex = 0;
}
} //end if block - sensor read
}
/********************************************************************
Plots the specified array
********************************************************************/
void plotArray(int index){
//based on the index, pick a dataset
int[] dataSet = null;
if (index == 0) {dataSet = xValues; stroke(255,255,0);}
else if(index == 1) {dataSet = yValues; stroke(0,255,255);}
else if(index == 2) {dataSet = zValues; stroke(255,0,255);}
//set the baseline for this set using index and draw it
int yBaseLine = ((height/3) * (index+1));
line(0,yBaseLine - 30,width,yBaseLine - 30);
//plot the dataset
for(int i = 1; i < curIndex; i++){
int xCo = i;
int yCo = yBaseLine - dataSet[i];
int xPrev = i - 1;
int yPrev = yBaseLine - dataSet[i-1];
line(xPrev,yPrev,xCo,yCo);
}
}
1