Need help with NewPing, invalid types 'int[int]' error

I am very new to Arduino so please forgive me if this is a dumb question with a simple answer, and please answer in layman terms if you do know of a solution to my problem.

I am trying to use the NewPing library to run three separate sonar sensors. The issue is I am needing the program to do something based off of different distance readings from the sensors. From a little Google work, it is my understanding that the distance values saved in cm are saved as an array, with cm[0] being the first sensor, cm[1] being the second, and cm[2] being the third. I have a code that seems to me should work (but I’m pretty new at this), but I am getting the error “invalid types ‘int[int]’”.

My full code is too long to post in one message, and due to my new account I can not make multiple posts within 5 minutes, so I will cut out most of the unrelated code.

#include <NewPing.h> //Library for sonar

// include the library for LCD:
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

#define SONAR_NUM      3  //number of sonar sensors

//Variables used for sonars at different wheel RPM
int CENTER_DISTANCE;   //max distance for center sensor to read
int SIDE_DISTANCE;  //max distance for side sensors to read
int PING_INTERVAL;  //ping interval in ms


//Pins for the LEDs
#define redLed 9 
#define yellowLed 10

#define buzzPin 11 //pin for buzzer

//variables for speed sensor
volatile byte half_revolutions;
unsigned int rpm;
unsigned long timeold;


String line1 = current_units;
String line2;

  
//Default options
int menu = 0;
int unit_select = 1;
int buzz_select = 1;
int brake_select = 1;

//interrupt flag
int refresh = 0;

//speed sensor numbers
int spdRead;
int wheelFt = 1.416;
int wheelCm = 43.18;

unsigned long pingTimer[SONAR_NUM];
uint8_t currentSensor = 0;
int cm[SONAR_NUM];

NewPing sonar[SONAR_NUM] = {
  //trigger pin, echo pin
  NewPing(7, 6, CENTER_DISTANCE),  //center, 0
  NewPing(5, 4, SIDE_DISTANCE),  //left, 1
  NewPing(3, 8, SIDE_DISTANCE),   //right, 2
};

void setup() {
  Serial.begin(115200);
  pinMode(redLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(buzzPin, OUTPUT);
  pingTimer[0] = millis() + 75;
  for (uint8_t i = 1; i < SONAR_NUM; i++)
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;

    //Initialize the intterrupt pin (Arduino digital pin 2)
   attachInterrupt(0, magnet_detect, RISING);
   half_revolutions = 0;
   rpm = 0;
   timeold = 0;

  lcd.begin(16,2);        // '16,2' is 16 columns, 2 rows on the display

  //Welcome screen
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Welcome, please");
  lcd.setCursor(3,1);
  lcd.print("drive safe");

  delay(2000);

  mphConv(); //Default speed unit

  mainDisplay();  //Default beginning display
}

void loop() {

  //Sonar*********************************************
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    if (millis() >= pingTimer[i]) {
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;
      sonar[currentSensor].timer_stop();
      currentSensor = i;
      sonar[currentSensor].ping_timer(echoCheck);
    }
  }

  //Speed sensor*******************************************
  if (half_revolutions >= 20) { 
     rpm = 30*1000/(millis() - timeold)*half_revolutions;
     rpm = rpm/4;
     timeold = millis();
     half_revolutions = 0;
     showSpeed();
     //Serial.println(rpm,DEC);
   }


  //Resets rpm to 0 when no input is given from speed sensor
   if ((millis() - timeold) > 1000){
    rpm = 0;
   }


  //Begin LCD************************************************************
 
  //End LCD***************************************************************************

  
}

void echoCheck() {
  if (sonar[currentSensor].check_timer())
    pingResult(currentSensor, sonar[currentSensor].ping_result / US_ROUNDTRIP_CM);
}

void pingResult(uint8_t sensor, int cm) {
  
  
  //For low speeds
  if ((rpm < 200) && (rpm > 10)){

    CENTER_DISTANCE = 500;
    SIDE_DISTANCE = 100;
    PING_INTERVAL = 30;
    
    if ((cm > 10) && ((cm[0] < 500) || cm[1,2] < 200)){
      digitalWrite(yellowLed, HIGH);
    }
    
    else {
      digitalWrite(yellowLed, LOW);
    }
    
    if ((cm >10) && ((cm[0] <300) || cm[1,2] < 100)) {
      digitalWrite(redLed, HIGH);
      if (buzz_select == 1) {
        digitalWrite(buzzPin, HIGH);
      }
    }
    
    else {
      digitalWrite(redLed, LOW);
      digitalWrite(buzzPin, LOW);
    }
  }

  //For high speeds
   if (rpm > 400){

    CENTER_DISTANCE = 900;
    SIDE_DISTANCE = 500;
    PING_INTERVAL = 60;
    
    if((cm > 10) && ((cm[0] < 900) || cm[1,2] < 500)){
      digitalWrite(yellowLed, HIGH);
    }
    
    else {
      digitalWrite(yellowLed, LOW);
    }
    
    if ((cm > 10) && ((cm[0] <700) || cm[1,2] < 300)) {
      digitalWrite(redLed, HIGH);
      if (buzz_select == 1) {
        digitalWrite(buzzPin, HIGH);
      }
    }
    
    else {
      digitalWrite(redLed, LOW);
      digitalWrite(buzzPin, LOW);
    }
  }

  //For medium speeds
  if ((rpm > 200) && (rpm < 400)){

    CENTER_DISTANCE = 700;
    SIDE_DISTANCE = 250;
    PING_INTERVAL = 45;
    
    if ((cm > 10) && ((cm[0] < 700) || cm[1,2] < 250)) {
      digitalWrite(yellowLed, HIGH);
    }
    
    else {
      digitalWrite(yellowLed, LOW);
    }
    
    if ((cm > 10) && ((cm[0] < 500) || cm[1,2] < 150)) {
      digitalWrite(redLed, HIGH);
      if (buzz_select == 1) {
        digitalWrite(buzzPin, HIGH);
      }
    }
    
    else {
      digitalWrite(redLed, LOW);
      digitalWrite(buzzPin, LOW);
    }
  }

  Serial.print(cm);
  Serial.print(" cm ");
  Serial.print(rpm);
  Serial.println(" rpm");
 
  
}

void mainDisplay() {

  line1 = current_units;
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(line1);
  lcd.setCursor(0,1);
  lcd.print(spdRead);
}

void menu1Display() {
  
  line1 = menu_1;
  line2 = current_units;

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(line1);
  lcd.setCursor(0,1);
  lcd.print(line2);
}

void menu2Display() {
  
  line1 = menu_2;
  line2 = current_buzzer;

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(line1);
  lcd.setCursor(0,1);
  lcd.print(line2);
}

void menu3Display() {
  
  line1 = menu_3;
  line2 = current_brake;

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(line1);
  lcd.setCursor(0,1);
  lcd.print(line2);
}

void screenRef() {
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(line1);
  lcd.setCursor(0,1);
  lcd.print(line2);
}

void magnet_detect()//This function is called whenever a magnet/interrupt is detected by the arduino
 {
   half_revolutions++;
   delay(10);
 }

 //speed calculations
void mphConv(){
  spdRead = wheelFt*3.14*rpm*60/5280;
}

void fpsConv(){
  spdRead = wheelFt*3.14*rpm/60;
}

void kphConv(){
  spdRead = wheelCm*3.14*rpm*60/100000;
}

void mpsConv(){
  spdRead = wheelCm*3.14*rpm/60/100;
}
 void showSpeed(){
  if (menu < 1){
    lcd.setCursor(0,1);
    lcd.print(" ");
    lcd.print(spdRead);
  }
 }

can you post the whole error message? it contains clues to where the error is!

if ((cm > 10) && ((cm[0] < 500) || cm[1,2] < 200)){

I don’t know if this is the error in question, but it’s certainly an error. You can’t use commas to just cram things together and hope the compiler will know what you meant.

if ((cm > 10) && ((cm[0] < 500) || (cm[1] < 200) || (cm[2] < 200))){

My apologies, I guess that would be useful haha. Here are all of the error messages. I’ve tried a few different approaches on how to solve them, but kept going back to this code. And sorry for the delay, this whole “only one post per 5 minutes” rule makes it rather annoying to carry a conversation.

Arduino: 1.6.6 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\Sage\AppData\Local\Temp\untitled2144490751.tmp\Final_Golfcart\Final_Golfcart.ino: In function 'void pingResult(uint8_t, int)':

Final_Golfcart:327: error: invalid types 'int[int]' for array subscript

     if ((cm > 10) && ((cm[0] < 500) || cm[1,2] < 200)){

                            ^

Final_Golfcart:327: error: invalid types 'int[int]' for array subscript

     if ((cm > 10) && ((cm[0] < 500) || cm[1,2] < 200)){

                                              ^

Final_Golfcart:335: error: invalid types 'int[int]' for array subscript

     if ((cm >10) && ((cm[0] <300) || cm[1,2] < 100)) {

                           ^

Final_Golfcart:335: error: invalid types 'int[int]' for array subscript

     if ((cm >10) && ((cm[0] <300) || cm[1,2] < 100)) {

                                            ^

Final_Golfcart:355: error: invalid types 'int[int]' for array subscript

     if((cm > 10) && ((cm[0] < 900) || cm[1,2] < 500)){

                           ^

Final_Golfcart:355: error: invalid types 'int[int]' for array subscript

     if((cm > 10) && ((cm[0] < 900) || cm[1,2] < 500)){

                                             ^

Final_Golfcart:363: error: invalid types 'int[int]' for array subscript

     if ((cm > 10) && ((cm[0] <700) || cm[1,2] < 300)) {

                            ^

Final_Golfcart:363: error: invalid types 'int[int]' for array subscript

     if ((cm > 10) && ((cm[0] <700) || cm[1,2] < 300)) {

                                             ^

Final_Golfcart:383: error: invalid types 'int[int]' for array subscript

     if ((cm > 10) && ((cm[0] < 700) || cm[1,2] < 250)) {

                            ^

Final_Golfcart:383: error: invalid types 'int[int]' for array subscript

     if ((cm > 10) && ((cm[0] < 700) || cm[1,2] < 250)) {

                                              ^

Final_Golfcart:391: error: invalid types 'int[int]' for array subscript

     if ((cm > 10) && ((cm[0] < 500) || cm[1,2] < 150)) {

                            ^

Final_Golfcart:391: error: invalid types 'int[int]' for array subscript

     if ((cm > 10) && ((cm[0] < 500) || cm[1,2] < 150)) {

                                              ^

exit status 1
invalid types 'int[int]' for array subscript

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

Did I pick out the right line or what?

Delta_G:
Did I pick out the right line or what?

You did, but it is also having an issue with the cm[0]. I am currently separating the cm[1,2] lines and will update this thread with the results, but I think it may still have issues.

Delta_G:
Did I pick out the right line or what?

nailed it!

triumph_013, you cannot access array elements this way:

cm[1,2]

Did I pick out the right line or what?

Possibly. But, you can't have an array called cm and a scalar called cm at the same scope. so comparing cm to 10 doesn't make sense. So, there is still a problem with your "corrected" version.

void pingResult(uint8_t sensor, int cm) {
  
  
  //For low speeds
  if ((rpm < 200) && (rpm > 10)){

    CENTER_DISTANCE = 500;
    SIDE_DISTANCE = 100;
    PING_INTERVAL = 30;
    
    if ((cm > 10) && ((cm[0] < 500) || cm[1,2] < 200)){
      digitalWrite(yellowLed, HIGH);
    }

Problem is that in addition to the global array variable named cm, you also have named a function parameter cm which is just a regular old int and won’t take brackets or be treated like an array. Guess which one takes precedence in the function? Try giving the function parameter a unique name.

PaulS:
Possibly. But, you can’t have an array called cm and a scalar called cm at the same scope. so comparing cm to 10 doesn’t make sense. So, there is still a problem with your “corrected” version.

Is there a way to select all of the values within “cm” without having to compare each element separately? This is what I am trying to do with cm > 10, so that nothing happens when an object is less than 10 cm in front of any of the three sensors.

All of the logic I have tried to use in this code has come from trying to understand other’s code. I do not have any formal coding experience, so it has been rather difficult.

Delta_G:

void pingResult(uint8_t sensor, int cm) {

//For low speeds
  if ((rpm < 200) && (rpm > 10)){

CENTER_DISTANCE = 500;
    SIDE_DISTANCE = 100;
    PING_INTERVAL = 30;
   
    if ((cm > 10) && ((cm[0] < 500) || cm[1,2] < 200)){
      digitalWrite(yellowLed, HIGH);
    }




Problem is that in addition to the global array variable named cm, you also have named a function parameter cm which is just a regular old int and won't take brackets or be treated like an array. Guess which one takes precedence in the function? Try giving the function parameter a unique name.

How do I solve this issue? I can give the function parameter a new name, but how would I tie it to the global variable? I’m still trying to understand what this code is doing with the function parameters and why they are needed.

Is there a way to select all of the values within “cm” without having to compare each element separately?

No. But, you could set a flag, tooClose, to false, and then iterate over the values in the array using a for loop, setting tooClose to true if any value is too close (or tooFar, if that is what you want).

Ok, I renamed the global variable and used the global variable for all of the arguments inside the function. I also deleted the (cm < 10) arguments for now. This was able to compile, but I will not be back on campus in order to test this code until tomorrow.

I will update tomorrow. Thanks for the help so far.