Hi everyone! I'm new here and have some questions regarding my project.
I have 2 ultrasonic sensors to get X and Y distance and I'm plotting that data via processing.
The first issue is that Processing script will randomly open, sometimes it gives "Error, disabling serialEvent() for COM3", the other times console just gives null. Not sure what is the problem here?
The other thing the plot in processing (my code). I want it to start drawing in the middle, which it is but it "slides" in from other coordinate.
Its always random but anyway it start drawing then it slides to center.
I'm pretty new to java, have experience in other languages,but processing does some stuff differently.
In the processing code there are some "hacks" so I could store some position.
Arduino code
const int TRIGX = 4;
const int ECHOX = 5;
const int TRIGY = 6;
const int ECHOY = 7;
int startX;
int startY;
const int numReadings = 10;
int readingsX[numReadings];
int readingsY[numReadings];
int readIndex = 0;
int totalx = 0;
int totaly = 0;
int averagex = 0;
int averagey = 0;
void setup (){
Serial.begin(9600);
pinMode(TRIGX, OUTPUT);
pinMode(ECHOX, INPUT);
pinMode(TRIGY, OUTPUT);
pinMode(ECHOY, INPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++){
readingsX[thisReading]=0;
readingsY[thisReading]=0;
}
}
double GetUltra ( int trig , int echo){
digitalWrite(trig , LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
double distance = ( pulseIn(echo, HIGH,32760) )* 343.2 / 2000; return distance;
}
void loop (){
/*
if (!startX){
startX = GetUltra(TRIGX,ECHOX);
}
if (!startY){
startY = GetUltra(TRIGY,ECHOY);
}
int newX = GetUltra(TRIGX,ECHOX);
int dataX = startX - newX;
*/
int dataY = GetUltra(TRIGY,ECHOY);
int dataX = GetUltra(TRIGX,ECHOX);
totalx = totalx - readingsX[readIndex];
totaly = totaly - readingsY[readIndex];
readingsX[readIndex]= dataX;
readingsY[readIndex]= dataY;
/*
if (dataX < 3000 ){
readingsX[readIndex]= dataX;
}
if (dataY < 3000 ){
readingsY[readIndex]= dataY;
}
*/
totalx = totalx + readingsX[readIndex];
totaly = totaly + readingsY[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings){
readIndex = 0;
}
averagex = totalx / numReadings;
averagey = totaly / numReadings;
Serial.print(averagex);Serial.print(' ');Serial.print(averagey);
Serial.println();
delay(20);
}
processing code
import processing.serial.*;
private int PORT_NUMBER = 0;
private int SERIAL_RATE = 9600;
Serial inputPort;
private String inputString;
private String start = "false";
private float rawX;
private float curX;
private float curY;
private float LX;
private float LY;
private float startX = 0.01;
void setup(){
size(2000,1000);
String portName = Serial.list()[PORT_NUMBER];
inputPort = new Serial(this, portName, SERIAL_RATE);
drawAxes();
delay(1000);
}
void draw(){
rawX = curX - startX;
strokeWeight(5);
if (rawX < -50 && rawX > -100 ){
stroke(157,24,100);
}
else if (rawX < -100 && rawX > -200 ){
stroke(177,14,50);
}
else if (rawX < -200){
stroke(200,0,0);
}
else {
stroke(127,34,255);
}
line(width/2 - rawX, height - curY,width/2 - LX, height - LY);
LX = rawX;
LY = curY;
}
void drawAxes() {
background(0);
stroke(255,255,255,150);
strokeWeight(1);
line(width/2,0,width/2,height);
stroke(127,34,255);
line(0,(height/4*3.5),width,(height/4*3.5));
}
void mouseReleased(){
start = "false";
drawAxes();
}
void serialEvent(Serial inputPort){
if (inputPort.available() > 0) {
inputString = inputPort.readStringUntil('\n');
if (inputString != null){
float[] nums = float(splitTokens(inputString));
curX = nums[0];
curY = nums[1];
if (start == "false"){
startX = curX;
start = "true";
}
//curX = map(curX,0, 2000,0,width);
curY = map(curY,0, 2000,0,height);
}
}
}
I would appreciate any help for these two issues.
Thank you very much!
