Can someone Help me, Why i cant compile and upload this code?

C:\Users\Lenovo\AppData\Local\Temp\ccZpHkWe.ltrans0.ltrans.o: In function `handle_interrupts(timer16_Sequence_t, unsigned int volatile*, unsigned int volatile*)':
C:\Users\Lenovo\AppData\Local\Arduino15\libraries\Servo\src\avr/Servo.cpp:58: undefined reference to `digitalWrite'
C:\Users\Lenovo\AppData\Local\Arduino15\libraries\Servo\src\avr/Servo.cpp:65: undefined reference to `digitalWrite'
C:\Users\Lenovo\AppData\Local\Temp\ccZpHkWe.ltrans0.ltrans.o: In function `NewPing::NewPing(unsigned char, unsigned char, unsigned int) [clone .constprop.5]':
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:16: undefined reference to `digital_pin_to_bit_mask_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:16: undefined reference to `digital_pin_to_bit_mask_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:17: undefined reference to `digital_pin_to_bit_mask_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:17: undefined reference to `digital_pin_to_bit_mask_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:19: undefined reference to `digital_pin_to_port_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:19: undefined reference to `digital_pin_to_port_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:19: undefined reference to `port_to_output_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:19: undefined reference to `port_to_output_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:20: undefined reference to `digital_pin_to_port_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:20: undefined reference to `digital_pin_to_port_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:20: undefined reference to `port_to_input_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:20: undefined reference to `port_to_input_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:22: undefined reference to `port_to_mode_PGM'
c:\Users\Lenovo\OneDrive\Documents\Arduino\libraries\NewPing\src/NewPing.cpp:22: undefined reference to `port_to_mode_PGM'
c:/users/lenovo/appdata/local/arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/../lib/gcc/avr/7.3.0/../../../../avr/lib/avr6/crtatmega2560.o:(.init9+0x0): undefined reference to `main'
collect2.exe: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

Welcome to the forum

Please provide more details

Please post the sketch that you were compiling, using code tags when you do

Which Arduino board were you using and which board was selected in the IDE when the error occurred ?

1 Like

Oh ya sorry i forgot to say about that
i use Arduino Mega and i use Arduino IDE 2.2.1
and this is my sketch:

#include <Servo.h>
#include <NewPing.h>

Servo myservo;
#define pir 22
#define SONAR_NUM 4     // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
int pos = 0;

  
NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(24,25, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. 
  NewPing(26,27, MAX_DISTANCE), 
  NewPing(28,29, MAX_DISTANCE),
  NewPing(30,31, MAX_DISTANCE)
};

const int Motorkiri1 = 32;  // Assign the appropriate pin numbers for your motor driver.
const int Motorkiri2 = 33;
const int Motorkanan1 = 34;
const int Motorkanan2 = 35;
const int ENA = 3;
const int ENB = 4;

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pinMode(Motorkiri1, OUTPUT);
  pinMode(Motorkiri2, OUTPUT);
  pinMode(Motorkanan1, OUTPUT);
  pinMode(Motorkanan2, OUTPUT);
  myservo.attach(2);
  pinMode(pir, INPUT);

}

void loop() { 
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    int distance = sonar[i].ping_cm();

    Serial.print("Sensor ");
    Serial.print(i);
    Serial.print(": ");
    Serial.print(distance);
    Serial.println(" cm");

    // Implementasi logika gerakan berdasarkan sensor
    if (i == 0) {
      handleSensorKiri(distance);
      Serial.println("Halangan kiri terdektesi, Berbelok ke kanan");
    } else if (i == 1) {
      handleSensorKanan(distance);
      Serial.println("Halangan Di Kanan Terdeteksi, Berbelok ke kiri");
    } else if (i == 2) {
      handleSensorDepan(distance);
      Serial.println("Oh awas ada halangan!");
    } else if (i == 3) {
      // Tambahkan logika untuk sensor keempat jika diperlukan
      handleSensorBelakang(distance);
    } 
  }

  int hasilpir = digitalRead(pir);
  if (hasilpir == HIGH ) {
    stopMotors();
    myservo.write(90);
    delay(5000);
    myservo.write(0);
  }
}



void handleSensorKiri(int distance) {
  // Logika untuk sensor kiri
  if (distance < 10) {
    stopMotors();
  }
}

void handleSensorKanan(int distance) {
  // Logika untuk sensor kanan
  if (distance < 10) {
    moveRight();
    delay(1000);
    stopMotors();
  }
}
void handleSensorDepan(int distance) {
  //Logika depan
  if (distance < 15)
  stopMotors();
  delay(1000);
  putarkanan();

}
void handleSensorBelakang(int distance){
  if (distance < 15)
  stopMotors();
  delay(1000);
  putarkiri();
}
// Fungsi gerakan motor tetap sama seperti sebelumnya
void stopMotors() {
  digitalWrite(Motorkiri1, LOW);
  digitalWrite(Motorkiri2, LOW);
  digitalWrite(Motorkanan1, LOW);
  digitalWrite(Motorkanan2, LOW);
}

void moveRight() {
  digitalWrite(Motorkiri1, HIGH);
  digitalWrite(Motorkiri2, LOW);
  digitalWrite(Motorkanan1, HIGH);
  digitalWrite(Motorkanan2, LOW);
}
void moveLeft(){
  digitalWrite(Motorkiri1, LOW);
  digitalWrite(Motorkiri2, HIGH);
  digitalWrite(Motorkanan1, LOW);
  digitalWrite(Motorkanan2, HIGH);
}
void putarkanan(){
  digitalWrite(Motorkiri1, HIGH);
  digitalWrite(Motorkiri2, LOW);
  digitalWrite(Motorkanan1, LOW);
  digitalWrite(Motorkanan2, HIGH);
}
void putarkiri(){
  digitalWrite(Motorkiri1, LOW);
  digitalWrite(Motorkiri2, HIGH);
  digitalWrite(Motorkanan1, HIGH);
  digitalWrite(Motorkanan2, LOW);
}

You posted in the IDE 1.x category of the forum. Your topic has been moved to the Programming category of the forum

Oh so i should make new topics?, (sorry im new)

There is no need for you to create another topic for this question. It has already been moved but please be careful in future when starting new topics

Are you sure the libraries are upto date?

use library manager to install NewPing library.

Possibly a bad installation of the IDE or an antivirus has deleted some file???

You might want to have a look at this discussion https://forum.arduino.cc/t/arduino-2-0-error-undefined-reference-to-main-compilation-error-exit-status-1/1036198

2 Likes

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