analogRead function

So I have a arduino desktop ide on windows and here is the code
void setup(){
}
void loop(){
Serial.begin(9600);
int a = analogRead(A0);
Serial.println(a);
delay(1000);
}

sorry if i misspelled anything but all seemed right when I check it but whenever I tried uploading it to the atmega 2560 it said the selected serial port avrdude: stk500v2_getsync():timeout
and, the selected serial port avrdude: stk500v2_getsync(): timeout communicating with programmer does not exist or your board is not connected

I know my board was connected so what is going on?

8675300:
I know my board was connected so what is going on?

Nothing wrong with your program, that’s a board communications failure.
So, if you are sure you have the board connected and configured correctly*, the logical conclusion is some problem with the board....

Obvious questions:

  • How do you KNOW it is connected correctly?
    Have you tried another board?
    Have you tried the board without anything wired/connected to it?

There are several other possible causes, such as a bad USB cable (yeah, it seems to happen, frequently) or choosing the wrong serial port.

Did you choose the right board and serial port in the Tools, Boards menu?

Troubleshooting connection.

You do not need to initialize the serial port (Serial.begin()) every time through loop(). Put the Serial.begin() in setup(). It only needs to begin once.

Serial.begin(9600); // should be in setup

Ok thanks I thought it was connected correctly if the boards lights start working I am using the atmega 2560 and maybe I didn't choose that. I thought serial.begin should be at loop thanks for that.

8675300:
I thought serial.begin should be at loop thanks for that.

Umm, how many times do you want serial to begin?

-jim lee

No nevermind I found out that I had to go in tools press port and then select an option and if that didn't work change the port the usb cable was on. Thanks