Arduino Mega 2560 upload error

This is one of my first projects and when I try to upload, the progress bar reaches over 90% before the following error occurs: Arduino: 1.8.12 (Mac OS X), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

/Users/philipliu/Documents/Arduino/Ultrasonic_sensor_with_buzzer/Ultrasonic_sensor_with_buzzer.ino: In function 'loop':
/Users/philipliu/Documents/Arduino/Ultrasonic_sensor_with_buzzer/Ultrasonic_sensor_with_buzzer.ino:40:53: warning: iteration 2 invokes undefined behavior [-Waggressive-loop-optimizations]
int noteDuration = 1000 / noteDurations[thisNote];
~~~~~~~~~~~~~~~~~~~~~~^
/Users/philipliu/Documents/Arduino/Ultrasonic_sensor_with_buzzer/Ultrasonic_sensor_with_buzzer.ino:36:37: note: within this loop
for (int thisNote = 0; thisNote < 8; thisNote++) {
~~~~~~~^
/Users/philipliu/Library/Arduino15/packages/arduino/hardware/avr/1.8.3/cores/arduino/main.cpp: In function 'main':
/Users/philipliu/Documents/Arduino/Ultrasonic_sensor_with_buzzer/Ultrasonic_sensor_with_buzzer.ino:40:53: warning: iteration 2 invokes undefined behavior [-Waggressive-loop-optimizations]
int noteDuration = 1000 / noteDurations[thisNote];
^
/Users/philipliu/Documents/Arduino/Ultrasonic_sensor_with_buzzer/Ultrasonic_sensor_with_buzzer.ino:36:37: note: within this loop
for (int thisNote = 0; thisNote < 8; thisNote++) {
^
Sketch uses 4608 bytes (1%) of program storage space. Maximum is 253952 bytes.
Global variables use 53 bytes (0%) of dynamic memory, leaving 8139 bytes for local variables. Maximum is 8192 bytes.
avrdude: ser_open(): can't open device "/dev/cu.Endurance-SerialPort-1": Resource busy
ioctl("TIOCMGET"): Inappropriate ioctl for device
ioctl("TIOCMGET"): Inappropriate ioctl for device
avrdude: ser_send(): write error: Bad file descriptor
avrdude: stk500_send(): failed to send command to serial port
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: Bad file descriptor
avrdude: stk500_send(): failed to send command to serial port
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: Bad file descriptor
avrdude: stk500_send(): failed to send command to serial port
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: Bad file descriptor
avrdude: stk500_send(): failed to send command to serial port
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: Bad file descriptor
avrdude: stk500_send(): failed to send command to serial port
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: Bad file descriptor
avrdude: stk500_send(): failed to send command to serial port
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
the selected serial port avrdude: stk500v2_getsync(): timeout communicating with programmer
does not exist or your board is not connected

My code is as follows:
#include <pitches.h>

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
3, 3
};

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
while (distance < 100) {
for (int thisNote = 0; thisNote < 8; thisNote++) {

// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);

// to distinguish the notes, set a minimum time between them.
// the note's duration
int pauseBetweenNotes = noteDuration * 1.10;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}

}
}

Maybe it's an error in my code? Thanks for your help

How many elements are there in your array? How many are you trying to access in your for-loop?

Did you select the correct port? Do you have other programs that might be accessing the serial port?

I closed down all other programs. The image is a screenshot of Tools.
image

I'm only trying to access one item in my for-loop. What do you mean by how many elements in an array?

That doesn't look like the correct serial port for an Arduino.

melody is an array of one element

Here you're trying to access the (nonexistent) second, third ... eighth elements, which is not allowed.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.