School Project-Error compiling for board Arduino Nano

I'm trying to upload my sketch but every time I do it keeps saying 'error compiling for board Arduino Nano'. I have changed my port and changed the board the Arduino Nano. What else do I have to do?????
I am making a soil moisture detector using this tutorial Arduino Soil Moisture Sensor : 6 Steps - Instructables
This is my code,:
#define Moisture Sensor A0
// Set analog input
const int moistureSensor = A0;
int D2;
int D3;
int D4;
int D5;
int D6;
// Set leds numbers
const int green1 = D2;
const int green2 = D3;
const int green3 = D4;
const int yellow = D5;
const int red = D6;
int Led1 = green1;
int Led2 = green2;
int Led3 = green3;
int Led4 = yellow;
int Led5 = red;
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(moistureSensor);
// print out the value you read:

Serial.println(sensorValue);

if (sensorValue >= 820)
{
digitalWrite(green1, HIGH);
digitalWrite(green2, HIGH);
digitalWrite(green3, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
}
else if (sensorValue >= 615 && sensorValue < 820)
{
digitalWrite(green1, LOW);
digitalWrite(green2, HIGH);
digitalWrite(green3, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
}
else if (sensorValue >= 410 && sensorValue < 615)
{
digitalWrite(green1, LOW);
digitalWrite(green2, LOW);
digitalWrite(green3, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
}
else if (sensorValue >= 250 && sensorValue < 410)
{
digitalWrite(green1, LOW);
digitalWrite(green2, LOW);
digitalWrite(green3, LOW);
digitalWrite(yellow, HIGH);
digitalWrite(red, LOW);
}
else if (sensorValue >= 0 && sensorValue < 250)
{
digitalWrite(green1, LOW);
digitalWrite(green2, LOW);
digitalWrite(green3, LOW);
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
}
delay(1000); // delay 1 second between reads
}


Your improperly posted code is missing the, required, setup() function. See bare minimum sketch.

Read the forum guidelines to see how to post code.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) to make code more readable before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

How exactly do I do that??

Do what?

Insert the setup() function as shown below.

int Led2 = green2;
int Led3 = green3;
int Led4 = yellow;
int Led5 = red;

// insert setup() here
void setup()
{
   // setup() must be included even if it is empty
}

// the loop routine runs over and over again forever:
void loop()
{
   // read the input on analog pin 0:

Arduino: 1.8.15 (Windows Store 1.8.49.0) (Windows 10), Board: "Arduino Nano, ATmega168"

C:\Users\Krishla\AppData\Local\Temp\ccLO6zvi.ltrans0.ltrans.o: In function `main':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/main.cpp:43: undefined reference to `setup'

collect2.exe: error: ld returned 1 exit status

exit status 1

Error compiling for board Arduino Nano.

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

That is telling you that it expects to see a reference to setup but does not see setup cause setup does not exist (is not referenced).

how do i add it in

Arduino: 1.8.15 (Windows Store 1.8.49.0) (Windows 10), Board: "Arduino Nano, ATmega168"

C:\Users\Krishla\Documents\Arduino\Final_Code\Final_Code.ino: In function 'void loop()':

Final_Code:27:3: error: expected initializer before 'int'

int sensorValue = analogRead(moistureSensor);

^~~

Final_Code:30:18: error: 'sensorValue' was not declared in this scope

Serial.println(sensorValue);

              ^~~~~~~~~~~

exit status 1

expected initializer before 'int'

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

Use the IDE or a text editor to insert void setup() {} where indicated. Reply #5 is not clear enough?

when i did that a lot of errors started to come up

I did as reply 5 said and now this came up Arduino: 1.8.15 (Windows Store 1.8.49.0) (Windows 10), Board: "Arduino Nano, ATmega168"

Sketch uses 2278 bytes (15%) of program storage space. Maximum is 14336 bytes.

Global variables use 198 bytes (19%) of dynamic memory, leaving 826 bytes for local variables. Maximum is 1024 bytes.

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avrdude -CC:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/etc/avrdude.conf -v -patmega168 -carduino -PCOM4 -b19200 -D -Uflash:w:C:\Users\Krishla\AppData\Local\Temp\arduino_build_150974/Final_Code.ino.hex:i

An error occurred while uploading the sketch

avrdude: Version 6.3-20190619

     Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

     Copyright (c) 2007-2014 Joerg Wunsch



     System wide configuration file is "C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/etc/avrdude.conf"



     Using Port                    : COM4

     Using Programmer              : arduino

     Overriding Baud Rate          : 19200

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

Post the code and the errors.

Any time that you change code, post the new version so that we can keep up. Post the errors.

Arduino: 1.8.15 (Windows Store 1.8.49.0) (Windows 10), Board: "Arduino Nano, ATmega168"

Sketch uses 2278 bytes (15%) of program storage space. Maximum is 14336 bytes.

Global variables use 198 bytes (19%) of dynamic memory, leaving 826 bytes for local variables. Maximum is 1024 bytes.

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avrdude -CC:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/etc/avrdude.conf -v -patmega168 -carduino -PCOM4 -b19200 -D -Uflash:w:C:\Users\Krishla\AppData\Local\Temp\arduino_build_150974/Final_Code.ino.hex:i

An error occurred while uploading the sketch

avrdude: Version 6.3-20190619

     Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

     Copyright (c) 2007-2014 Joerg Wunsch



     System wide configuration file is "C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/etc/avrdude.conf"



     Using Port                    : COM4

     Using Programmer              : arduino

     Overriding Baud Rate          : 19200

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

Here is your original code with the setup() inserted. It compiles fine with no warnings or errors.

#define Moisture Sensor A0
// Set analog input
const int moistureSensor = A0;
int D2;
int D3;
int D4;
int D5;
int D6;
// Set leds numbers
const int green1 = D2;
const int green2 = D3;
const int green3 = D4;
const int yellow = D5;
const int red = D6;
int Led1 = green1;
int Led2 = green2;
int Led3 = green3;
int Led4 = yellow;
int Led5 = red;

// insert setup() here
void setup()
{
   // setup() must be included even if it is empty
}

// the loop routine runs over and over again forever:
void loop()
{
   // read the input on analog pin 0:
   int sensorValue = analogRead(moistureSensor);
   // print out the value you read:

   Serial.println(sensorValue);

   if (sensorValue >= 820)
   {
      digitalWrite(green1, HIGH);
      digitalWrite(green2, HIGH);
      digitalWrite(green3, HIGH);
      digitalWrite(yellow, LOW);
      digitalWrite(red, LOW);
   }
   else if (sensorValue >= 615 && sensorValue < 820)
   {
      digitalWrite(green1, LOW);
      digitalWrite(green2, HIGH);
      digitalWrite(green3, HIGH);
      digitalWrite(yellow, LOW);
      digitalWrite(red, LOW);
   }
   else if (sensorValue >= 410 && sensorValue < 615)
   {
      digitalWrite(green1, LOW);
      digitalWrite(green2, LOW);
      digitalWrite(green3, HIGH);
      digitalWrite(yellow, LOW);
      digitalWrite(red, LOW);
   }
   else if (sensorValue >= 250 && sensorValue < 410)
   {
      digitalWrite(green1, LOW);
      digitalWrite(green2, LOW);
      digitalWrite(green3, LOW);
      digitalWrite(yellow, HIGH);
      digitalWrite(red, LOW);
   }
   else if (sensorValue >= 0 && sensorValue < 250)
   {
      digitalWrite(green1, LOW);
      digitalWrite(green2, LOW);
      digitalWrite(green3, LOW);
      digitalWrite(yellow, LOW);
      digitalWrite(red, HIGH);
   }
   delay(1000); // delay 1 second between reads
}

i copied this one and this came up Arduino: 1.8.15 (Windows Store 1.8.49.0) (Windows 10), Board: "Arduino Nano, ATmega168"

The sketch name had to be modified.
Sketch names must start with a letter or number, followed by letters,
numbers, dashes, dots and underscores. Maximum length is 63 characters.

Sketch uses 2278 bytes (15%) of program storage space. Maximum is 14336 bytes.

Global variables use 198 bytes (19%) of dynamic memory, leaving 826 bytes for local variables. Maximum is 1024 bytes.

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avrdude -CC:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/etc/avrdude.conf -v -patmega168 -carduino -PCOM4 -b19200 -D -Uflash:w:C:\Users\Krishla\AppData\Local\Temp\arduino_build_51804/Online_code-final.ino.hex:i

avrdude: Version 6.3-20190619

     Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

     Copyright (c) 2007-2014 Joerg Wunsch



     System wide configuration file is "C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/etc/avrdude.conf"



     Using Port                    : COM4

     Using Programmer              : arduino

     Overriding Baud Rate          : 19200

avrdude: ser_open(): can't open device "\.\COM4": Access is denied.

avrdude done. Thank you.

Problem uploading to board. See Upload – Arduino Help Center for suggestions.

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

is there a problem with my board?

Does your Nano have the ATmega168 chip? The newer Nanos have the ATmega328 chip. And there are 2 different 328 bootloaders, old and new. You need to choose the right one in Tools, Boards. See this post.

i changed to ATmega328
Arduino: 1.8.15 (Windows Store 1.8.49.0) (Windows 10), Board: "Arduino Nano, ATmega328P"

Sketch uses 2278 bytes (7%) of program storage space. Maximum is 30720 bytes.

Global variables use 198 bytes (9%) of dynamic memory, leaving 1850 bytes for local variables. Maximum is 2048 bytes.

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avrdude -CC:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM4 -b115200 -D -Uflash:w:C:\Users\Krishla\AppData\Local\Temp\arduino_build_316257/online.ino.hex:i

An error occurred while uploading the sketch

avrdude: Version 6.3-20190619

     Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

     Copyright (c) 2007-2014 Joerg Wunsch



     System wide configuration file is "C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/etc/avrdude.conf"



     Using Port                    : COM4

     Using Programmer              : arduino

     Overriding Baud Rate          : 115200

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

I missed that part before, sorry. That says that the PC cannot communicate with the Nano. Are you sure that you have the right serial port chosen in Tools, Ports? Is there something else connected to com4?

I suggest that you go through the troubleshooting tools.

Here is some further information on the error "avrdude: ser_open(): can't open device ".\COM4": Access is denied."