Help using two Maxsonar range finders with arduino

Hi!

I am trying to use two maxsonar ultrasonic range finders to control the brightness of two LEDs. I have successfully done a few experiments using one sensor but am having trouble writing code for the two daisy-chained sensors. I copied most the code below from a book and it works in that it it correctly reads the distances from each sensor but I can’t figure out how to translate the readings from each sensor to separate outputs on two pins.

int ultraSoundSignalPins = {0,1}; // 2 Ultrasound signal pins
int ultraSoundTriggerPin = 6; // output pin to start Ultrasound signals

void setup() {
Serial.begin(9600);
for(int i=0; i < 2; i++) {
pinMode(ultraSoundSignalPins*, INPUT); // Switch signalpin to input*

  • }*
  • pinMode(ultraSoundTriggerPin, OUTPUT); // set this pin to output*
  • //give the sensors time to boot up*
  • delay(250);*
  • // send RX pin high to signal to chain to ping*
  • digitalWrite(ultraSoundTriggerPin, HIGH);*
  • delayMicroseconds(20);*
  • digitalWrite(ultraSoundTriggerPin, LOW);*
  • pinMode(ultraSoundTriggerPin, INPUT); // electrically disconnects the pin*
  • delay(50);*
    }
    void loop()
    {
  • unsigned long ultrasoundValue;*
  • unsigned long echo;*
  • delay(50);*
  • for(int i=0; i < 2; i++)*
  • {*
    //Now the values from each sensor can be read:
    _ echo = analogRead(ultraSoundSignalPins*); //Listen for echo*_
    * ultrasoundValue = (echo / .58138);*
    * Serial.println();*
    * Serial.print("UltraSound Value = ");*
    * Serial.print(ultrasoundValue);*

* delay(50);*

* }*

* for(int i=3; i < 5; i++) { //LEDs attached to pins 3 and 4 indicate change in sensor reading*
* analogWrite(i, ultrasoundValue);*

* }*
}
The code above outputs the reading from one sensor on both pins. Any help really appreciated !

Quite a few ways of doing this - create an array of "ultrasoundValue" and read the values in, or move the analogWrite into the "for" loop that read the ultrasound sensors.

When posting code, can you please use the "#" (Code) button in the editor?

Thanks, moving analogWrite worked nicely. gonna try an array to play around with sampling now.

will also make sure I use the code button from now on.