Hi i am connecting three ultra sonic sensors(MB 1261) to arduino board of three analog pins(A0,A4 and A7) and trigger pin to digital pin 2.The values are fluctuating more for the same obstacle position and also some random values are coming in serial monitor after disconnecting sensor from the analog pin.So can you please help me out in this.Please find below my source code:
/Feel free to use this code.
Author: Tom Bonar
Date: 12-11-2013
Analog pin 1 for reading in the analog voltage from the MaxSonar device.
This variable is a constant because the pin will not change throughout execution of this code./
const int triggerPin = 2;
long anVolt1, anVolt2,anVolt3,anVolt4,sensor1,sensor2,sensor3,sensor4;
long anVolt5,sensor5;
void setup() {
//This opens up a serial connection to shoot the results back to the PC console
Serial.begin(9600);
pinMode(triggerPin,OUTPUT);
delay(200); //Gives time for the sensors to boot and calibrate
}
void start_sensor(){
digitalWrite(triggerPin,HIGH);
delay(200);
//delayMicroseconds(20);
digitalWrite(triggerPin,LOW);
}
void read_sensor(){
//Used to read in the analog voltage output that is being sent by the XL-MaxSonar device.
//Scale factor is (Vcc/1024) per 2 centimeter. A 5V supply yields ~4.9mV/2 cm for long range sensors
anVolt1 = analogRead(A0);
anVolt2 = analogRead(A4);
anVolt3 = analogRead(A7);
/anVolt4 = analogRead(A3);
anVolt5 = analogRead(A4);/
sensor1 = anVolt12;
sensor2 = anVolt22;
sensor3 = anVolt32;
/sensor4 = anVolt42;
sensor5 = anVolt52;*/
}
void printall() {
//Serial.print(anVolt1);
//Serial.print(",");
Serial.print(sensor1);
Serial.print(",");
Serial.print(sensor2);
Serial.print(",");
Serial.print(sensor3);
/* Serial.print(",");
Serial.print(sensor4);
Serial.print(",");
Serial.print(sensor5);*/
Serial.println();
}
void loop () {
start_sensor();
read_sensor();
printall();
delay(300); // This delay time changes by 100 for every sensor in the chain. For 4 sensors this will be 400
}