Ping Sensor on Arduino Robot

I have attached 5 Ping Sonar sensors to the Arduino robot. Although it works on TKD1 it does not work on B_TK1 to B_TK4, I am not sure why since they are all analog and digital ports. I have pasted my code below. When the program runs only the first sensor gives data, the other 4 show 0 inches all the time.

/* Ping))) Sensor

This sketch reads a PING))) ultrasonic rangefinder attached to an Arduino Robot on pin D1,D7,D8,D9,D10

The circuit:

  • +V connection of the PING))) attached to +5V
  • GND connection of the PING))) attached to ground
  • SIG connection of the PING))) attached to digital pin D9 (B_TK2)

*/

#include <ArduinoRobot.h>
#include "Wire.h"
#include "SPI.h"

//int sonarPin = TKD1;//pin number where the sonar is attached (D1 = TKD1)
//int sonar1Pin = B_TK1;//pin number where the sonar is attached (D10 = B_TK1)
//int sonar2Pin = B_TK2;//pin number where the sonar is attached (D9 = B_TK2)
//int sonar3Pin = B_TK3;//pin number where the sonar is attached (D8 = B_TK4)
//int sonar4Pin = B_TK4;//pin number where the sonar is attached (D7 = B_TK3)

int myPins[] = {TKD1,B_TK1, B_TK2, B_TK3, B_TK4};

void setup() {
// initialize the Robot, SD card, and display
//Serial.begin(9600);
Robot.begin();
Robot.beginTFT();
Robot.beginSD();
Robot.clearScreen();
//Serial.println("initialize...");
}

void loop() {

long duration, inches;//variables for pin duration, distance in inches
for(int i = 0;i<5;i++){
pinMode(myPins*, OUTPUT);//set te PING pin as an output*
_ Robot.digitalWrite(myPins*, LOW);//set te PING pin low first to get a clean high pulse*_
* delayMicroseconds(2);//wait 2 microseconds *
_ Robot.digitalWrite(myPins*, HIGH);//PING sonar is triggered by a 2 or more microsecond HIGH PULSE*
* delayMicroseconds(5);//wait 5 microseconds
Robot.digitalWrite(myPins, LOW);//set te PING pin low first to get a clean high pulse*

pinMode(myPins*, INPUT);//set te pin as an input whose duration is the time for reception*
duration = pulseIn(myPins*, HIGH);//measures how long the pin is high*
* inches = duration/74/2; // convert the time into a distance*
Robot.debugPrint(inches, 5,25i+10);//print the inches to the LCD_
_ Robot.text("in: ", 50,25i+10);//print the inches to the LCD_

* delay(100);//wait 100 miilliseconds*
* }*

* //Serial.print(inches);//print inches to the serial monitor*
* //Serial.println(" in, ");//print inches value to the serial monitor*
* //Serial.print(inch2);//print inches to the serial monitor*
* //Serial.println(" in, ");//print inches value to the serial monitor*
* //Robot.text("inches: ", 5,50);//print the inches to the LCD*
* //Robot.debugPrint(inches, 50,50);//print the inches to the LCD*

}
Ping_Arduino_Robot.ino (2.46 KB)

pinMode(myPins, OUTPUT);//set te PING pin as an output
The myPins variable is an array, from which you have to fetch just one element, e.g. myPins*.*

Hi DrDietrich,

Actually I did have the elements, it got stripped for some reason when I pasted the code so that can't be the problem. The program actually runs as written, it just only prints the distance values for the first element in the array and not the rest.

/* Ping))) Sensor
  
   This sketch reads a PING))) ultrasonic rangefinder attached to an Arduino Robot on pin D9
        
   The circuit:
	* +V connection of the PING))) attached to +5V
	* GND connection of the PING))) attached to ground
	* SIG connection of the PING))) attached to digital pin D9 (B_TK2)

*/

#include <ArduinoRobot.h>
#include "Wire.h"
#include "SPI.h"



//int sonarPin = TKD1;//pin number where the sonar is attached (D1 = TKD1)
//int sonar1Pin = B_TK1;//pin number where the sonar is attached (D10 = B_TK1)
//int sonar2Pin = B_TK2;//pin number where the sonar is attached (D9 = B_TK2)
//int sonar3Pin = B_TK3;//pin number where the sonar is attached (D8 = B_TK4)
//int sonar4Pin = B_TK4;//pin number where the sonar is attached (D7 = B_TK3)

int myPins[] = {TKD1,B_TK1, B_TK2, B_TK3, B_TK4};


void setup() {
    // initialize the Robot, SD card, and display
    //Serial.begin(9600);
    Robot.begin();
    Robot.beginTFT();
    Robot.beginSD();
    Robot.clearScreen();
    //Serial.println("initialize...");
}

void loop() {
  

    long duration, inches;//variables for pin duration, distance in inches
    for(int i = 0;i<5;i++){
         pinMode(myPins[i], OUTPUT);//set te PING pin as an output
         Robot.digitalWrite(myPins[i], LOW);//set the PING pin low first to get a clean high pulse
         delayMicroseconds(2);//wait 2 microseconds    
         Robot.digitalWrite(myPins[i], HIGH);//PING sonar is triggered by a 2 or more microsecond HIGH PULSE
         delayMicroseconds(5);//wait 5 microseconds  
         Robot.digitalWrite(myPins[i], LOW);//set te PING pin low first to get a clean high pulse

          pinMode(myPins[i], INPUT);//set te pin as an input whose duration is the time for reception
          duration = pulseIn(myPins[i], HIGH);//measures how long the pin is high
          inches = duration/74/2;    // convert the time into a distance

          Robot.debugPrint(inches, 5,25*i+10);//print the inches to the LCD
          Robot.text("in: ", 50,25*i+10);//print the inches to the LCD
          delay(100);//wait 100 miilliseconds
    }
  



    //Serial.print(inches);//print inches to the serial monitor
    //Serial.println(" in, ");//print inches value to the serial monitor
    //Serial.print(inch2);//print inches to the serial monitor
    //Serial.println(" in, ");//print inches value to the serial monitor
    //Robot.text("inches: ", 5,50);//print the inches to the LCD
    //Robot.debugPrint(inches, 50,50);//print the inches to the LCD

    
}