hi all, so I got my maxsonar EZ0, and i am getting readings from it using Analogue, I found this example which lights up the LED as u get closer and gave it a try
/*
From the datasheet of the The LV-MaxSonar-EZ0 the output analog
voltage with a scaling factor of (Vcc/512) per inch.
A supply of 5V yields ~9.8mV/in. and 3.3V yields ~6.4mV/in.
The output is buffered and corresponds to the most recent range data.
http://www.maxbotix.com/uploads/LV-MaxSonar-EZ0-Datasheet.pdf*/
// pin connected to analog output on maxsonar sensor
int sonarPin = 0;
// pin connected to digital output on red led
int ledPin = 7;
void setup() {
Serial.begin(9600); // sets the serial port to 9600
pinMode(ledPin, OUTPUT);
}
void loop() {
// The Arduino’s analog-to-digital converter (ADC) has a range of 1024,
// which means each bit is ~4.9mV.
// Therefore, to convert the number returned by the ADC to inches, wet
// have to divide by 2.
// To convert to cm is necessary to divide by 0.3937.
// 1 centimeter = 0.3937 inch
int distance = analogRead(sonarPin)/(2*0.3937);
Serial.print(distance, DEC); // prints the value read
Serial.println("cm");
// If something is at a distance less than 20 cm than LED lights up.
if(distance > 300) {
digitalWrite(ledPin, HIGH);
delayMicroseconds(500);
digitalWrite(ledPin, LOW);
delayMicroseconds(500);
}
}
it seems to work well, but when I look at the numbers in the serial monitor, the numbers get higher as I get closer, also the readings can be a little erratic, can someone help me modify this to a PW, when I tried i just got no readings at all