Programming Question

Hi guys,

I am having trouble translating this sentence '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. ' into a code. Does any of you guys know how to do it?

Here is the datasheet and is on page 4

Can anybody help me out?

This is my current code

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.
}

Please read the two posts at the top of this Forum by Nick Gammon for guidelines on how to post here, especially the use of code tags when posting source code. Also, this seems like a homework assignment. Have you googled for an answer?

Some similar questions and answers have already been provided in the previous inquiry :

http://forum.arduino.cc/index.php?topic=348850.msg2407870#msg2407870

  • dan

Not answering your question, but for future reference you can write your printing more efficiently like this

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