Invalid types 'int[int]' for array subscript error in my Arduino code

Hi! Can you please help me with my code? I attach a copy below.

else if (pirState == 1) {

  for (int thisNote = 0; thisNote < 13; thisNote++) {

    int noteDuration = 1000 / noteDuration [thisNote];
    tone(13, melody [thisNote], noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    noTone(13);

I got these error messages:

invalid types 'int[int]' for array subscript

Any help appreciated. Thanks.
-Eli

Don’t post snippets (Snippets R Us!)

Show us how you declared the notes and duration arrays (and the rest of the code and the full compiler error as it will point you at the infringing line)

You also might want to think about the variables naming in this line


int noteDuration = 1000 / noteDuration [thisNote];

My bad here is the full code:

#include <AFMotor.h>  // AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install 
#include <NewPing.h>  // NewPing Library https://github.com/livetronic/Arduino-NewPing
#include <Servo.h>// Servo Library https://github.com/arduino-libraries/Servo.git
#include "pitches.h"

#define TRIG_PIN A0
#define ECHO_PIN A1
#define MAX_DISTANCE 200
#define MAX_SPEED 150
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
Servo myservo;

boolean goesForward = false;
int distance = 100;
int speedSet = 0;
int melody[] = {NOTE_C4, NOTE_G3, NOTE_A3, NOTE_G3, 0 , NOTE_B3, NOTE_C4};
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};
int pirSensor = 2;

void setup() {

myservo.attach(10);
myservo.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
pinMode(pirSensor, INPUT);
}

void loop() {
int distanceR = 0;
int distanceL =  0;
int pirState = digitalRead(pirSensor);
delay(40);

if (distance <= 15)
{
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);
}
 else if (pirState == 1) {

for (int thisNote = 0; thisNote < 13; thisNote++) {

  int noteDuration = 1000 / noteDuration [thisNote];
  tone(13, melody [thisNote], noteDuration);

  int pauseBetweenNotes = noteDuration * 1.30;
  delay(pauseBetweenNotes);

  noTone(13);

  if (distanceR >= distanceL)
  {
    turnRight();
    moveStop();
  } else
  {
    turnLeft();
    moveStop();
  }
}
{
  moveForward();
}
distance = readPing();

}
}

int lookRight()
{
myservo.write(50);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
}

int lookLeft()
{
myservo.write(170);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
delay(100);
}

int readPing() {
delay(70);
int cm = sonar.ping_cm();
if (cm == 0)
{
  cm = 250;
}
return cm;
}

void moveStop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
}

void moveForward() {

if (!goesForward)
{
  goesForward = true;
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2)
  {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    delay(5);
  }
}
}

void moveBackward() {
goesForward = false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2)
{
  motor1.setSpeed(speedSet);
  motor2.setSpeed(speedSet);
  delay(5);
}
}

void turnRight() {
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(450);
motor1.run(FORWARD);
motor2.run(FORWARD);
}

void turnLeft() {
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(450);
motor1.run(FORWARD);
motor2.run(FORWARD);

}

Good, now post the complete error message (there's a button to do that right above Arduino IDE's little black window). It will point to the exact line the compiler is complaining about.

Double Check what I said about the variable names.

The array does have an s at the end: noteDurations

It's spelled "noteDurations [thisNote]".

You only have 7 notes.