I got an Arduino Diecemilla and a MaxSonar EZ0. My code under arduino is:
#define anIn 0
#define LEDpin 13
int iAnVal;
int iAnValAnterior;
void setup() {
Serial.begin(9600);//See note at the Serial.write line.
pinMode(LEDpin, OUTPUT); // prepare the pin "LEDpin" for output
iAnVal = 0;
}
void loop() {
iAnVal = analogRead(1); // read a value from the sensor under analog 1
delay(500);
Serial.println(iAnVal);
digitalWrite(LEDpin,HIGH);
delay(500);
digitalWrite(LEDpin,LOW);
}
And my processing code is:
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(200, 200);
String portName = "COM38";
myPort = new Serial(this, portName, 9600);
}
void draw()
{
if (val != 10) {
print ("Val = " + val + "\n");
}
}
The thing is: My output is something like this:
50
51
13
And I have to convert it to an int value. The other (and more serious) problem is that I can´t get accurate values. If I dont touch the sensor, I get different values, and sometimes, REALLY different values.
This value I get, is the distance in inchs, right? Or do I need to convert it somehow?
Thanks a lot.