Sequentially Read Each MaxSonar

Hi all,

I was wondering how do you create a code that sequentially read each MaxSonar and display the reading on the Serial Monitor. Between I am using the MB1010 LV-MaxSonar-EZ1.

I have read the datasheet which says 'To sequentially read each sensor, connect your triggering device to pin 4 (RX) of the first sensor, then connect pin 5 (TX) output of the first sensor to the RX pin of the next sensor that is to be ranged in sequence. Do this with however many sensors are to be used in the chain. '

I have understand what it means but the problem for me now is to write the code. Can anyone help me out?

Please post a link to the datasheet so that we can read it.

...R

Its on page 4 and I am using the first method which is AN Output Commanded Loop.

Have you read this

/*
This code reads the Analog Voltage output from the
LV-MaxSonar sensors
If you wish for code with averaging, please see
playground.arduino.cc/Main/MaxSonar
Please note that we do not recommend using averaging with our sensors.
Mode and Median filters are recommended.
*/

const int anPin1 = 0;
const int anPin2 = 1;
const int anPin3 = 2;
const int anPin4 = 3;
int triggerPin1 = 13;
long distance1, distance2, distance3, distance4;

void setup() {
  Serial.begin(9600);  // sets the serial port to 9600
  pinMode(triggerPin1, OUTPUT);
}

void start_sensor(){
  digitalWrite(triggerPin1,HIGH);
  delay(1);
  digitalWrite(triggerPin1,LOW);
}

void read_sensors(){
  /*
  Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
  Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
  */
  distance1 = analogRead(anPin1)/2;
  distance2 = analogRead(anPin2)/2;
  distance3 = analogRead(anPin3)/2;
  distance4 = analogRead(anPin4)/2;
}

void print_all(){
  Serial.print("S1");
  Serial.print(" ");
  Serial.print(distance1);
  Serial.print("in");
  Serial.print(" ");
  Serial.print(" ");
  Serial.print("S2");
  Serial.print(" ");
  Serial.print(distance2);
  Serial.print("in");
  Serial.print(" ");
  Serial.print(" ");
  Serial.print("S3");
  Serial.print(" ");
  Serial.print(distance3);
  Serial.print("in");
  Serial.print(" ");
  Serial.print(" ");
  Serial.print("S4");
  Serial.print(" ");
  Serial.print(distance4);
  Serial.print("in");
  Serial.println();
}

void loop() {
  start_sensor();
  read_sensors();
  print_all();
  delay(200); //This is the equivant of the amount of sensors times 50.  If you changed this to 5 sensors the delay would be 250.
}

Hi thx for showing me the code. So how does the code relate to 'connect triggering device to pin 4 (RX) of the first sensor, then connect pin 5 (TX) output of the first sensor to the RX pin of the next sensor that is to be ranged in sequence'? I cant seems to relate how the hardware and software relate to each other.

Sry I'm still a beginner of arduino and still trying to understand it.

Have you read the link I posted?

The triggerPin1 was to pin 4 (RX) of the first sensor. as show in this image

Hi, does int triggerPin1 = 13; means the variable, triggerPin1, assign to pin 13? and why does const int anPin2 = 1; and not anPin2= A1 since it is connected to A1?

bump up~