I'm am using a ultrasonic sensor make a sonar on processing. I have it all working except for the serial communications. processing thinks that the reading is always 57 then 17 then 10. I tested it out on Arduino serial moniter and that works but for some reaso processing cant read it.
Processing:
import processing.serial.*;
import ddf.minim.*;
AudioPlayer sonar;
Minim minim;
float sensor;
Serial s;
void setup()
{
s = new Serial (this,9600);
minim= new Minim(this);
background(31,88,8);
size(720,360);
sonar = minim.loadFile("teamgrab.wav");
}
void draw()
{
if(s.read() == 0)
{
sensor = s.read();
}
if(s.read() == 1)
{
sensor = s.read() + (s.read() * 10);
}
float s = map(90, 0, 180, -PI, 0) - TWO_PI;
float lineX = cos(s) * 357.5 + height;
float lineY = sin(s) * 357.5 + width/2;
stroke(255);
strokeWeight(1.5);
line(height,width/2,lineX,lineY);
float pointX = cos(s) * sensor + height;
float pointY = sin(s) * sensor + width/2;
ellipseMode(CENTER);
noStroke();
fill(46,46,46);
//ellipse(height,width/2,720,720);
fill(13,39,3);
noStroke();
ellipse(height,width/2,40,40);
noStroke();
fill(31,88,8);
ellipse(pointX,pointY,4,4);
}
void setY()
{
}
void setX()
{
}
void placepoint()
{
noStroke();
fill(31,88,8);
ellipse(360,300,4,4);
}
arduino:
#include <NewPing.h>
#include <Servo.h>
NewPing sonar(A1,13,200);
Servo rotate;
int pos = 0;
int direction = 1;
int sense;
void setup()
{
rotate.attach(10);
Serial.begin(9600);
}
void loop()
{
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
sense = uS / US_ROUNDTRIP_CM;
Serial.println(sense);
/* if (sense > 0 && sense < 10)
{
Serial.print(0);
}
if (sense > 9 && sense < 21)
{
Serial.print(1);
}
if(sense> 0 && sense < 20)
{
Serial.print(sense);
}
if(sense > 20 || sense == 0)
{
Serial.print("u");
}// Convert ping time to distance in cm and print result (0 = outside set distance range)
if (rotate.read() < 10)
{
Serial.print(0);
}
if (rotate.read() > 9 && rotate.read() < 100)
{
Serial.print(1);
}
if (rotate.read() > 99)
{
Serial.print(2);
}
Serial.println(rotate.read());*/
if(rotate.read() >= 180)
{
direction = -1;
}
if (rotate.read() == 0)
{
direction = 1;
}
pos += direction;
rotate.write(pos);
delay(10);
}