I have 2 simple questions

Hello.

My first question is: Can i copy-paste my codes that i have written on UNO to Mega? Would it run?
Second question is: We use digital PWM pins to run a servo. Can we use analog pins to run servo?

Thanks.

Yes.
Maybe. (Some pins are analogue input only)

Thanks a lot!

Servo.h Library overrides the default analog functionalities of the A0 - A5 pins of UNO. It has been experimentally verified that these analog pins respond to attach() and write() commands and emit 50 Hz PWM signals.

#include<Servo.h>
Servo myServo;

void setup() 
{
  myServo.attach(A0); //checked for all A0 - A5
  myServo.write(0xCD);
}

void loop() 
{

}

aatabag:
My first question is: Can i copy-paste my codes that i have written on UNO to Mega?

For many things that will work but there are some situations in which the code for a Mega must be different, or, even if the code can be the same, the connections must be different - for example the SPI pins are in a different place on a Mega.

...R

Also that –

To set VREF voltage for the ADC, the following is the valid code for UNO; but, it is not valid for MEGA. There is a compilation error.

analogReference(INTERNAL);

The valid code for MEGA is:

analogReference(INTERNAL1V1);

aatabag:
Second question is: We use digital PWM pins to run a servo. Can we use analog pins to run servo?

Servos are not generally driven using the PWM function of the pins. Standard hobby type servos need a pulse that is between 1000uS and 2000uS wide, repeated at a 20mS interval, although this can vary. You could generate this using PWM, but that would severely limit the number of servos an arduino could control, so the servo library used a timer to generate the timing, and sets the output pins HIGH or LOW at the appropriate time for each servo, so any pin capable of being used as a digital output should work. Doing it that way, the servo library can control 12 servos on an UNO, and 48 on a mega.

david_2018:
Doing it that way, the servo library can control 12 servos on an UNO, and 48 on a mega.

Why not 20 for UNO? Normal 20 DIO lines (0 - 19).

GolamMostafa:
Why not 20 for UNO? Normal 20 DIO lines (0 - 19).

Limitation of the servo library. For some reason the library limits the servos to 12 per timer, and there is only one suitable timer available on the UNO.

GolamMostafa:
Why not 20 for UNO? Normal 20 DIO lines (0 - 19).

Why not look at the code and find out? Its open-source sitting there to be inspected....
Seems to be trying to maintain a 50Hz refresh rate, assuming most channels around 1500us.
12 x 1500us = 18ms, so 20ms is generally attainable - it will slow down if many servos set to long pulses.

~~In UNO, only DPin-0 to DPin-11 respond to Servo.h Library.~~edit based on Post#11.

In UNO, only 12 PWMs are available at one time on any 12 DPins among the available 20 (0 - 19) DPins.

GolamMostafa:
In UNO, only DPin-0 to DPin-11 respond to Servo.h Library.

bylestones:
Nonsense: 12 & 13, as well as the analog pins (A0-A5 (also known as digital pins14-19)) can be used for servos too.

Based on Post#8, I wrote codes for all the DPins: 0 -19; I found PWM signals only at 0 - 11. Based on Post#11, I commented on codes related to DPin-0; now, I get 12 PWM signals on DPin-1 to DPin-12; there is nothing on DPin-0 and the rest.

Now, I have realized that I should say in my Post#10 that only 12 PWMs are available at one time on any 12 DPins among the available 20 (0 - 19) DPins.

Thanks for locating the fault of my Post#10.

But just to recap, most servos respond to signals that come at bigger intervals just fine, and it is not to difficult to use all (and any) available pins even without the use of any timers, by first sorting the pulselength (should take up to 1ms) and then fire all and cut them in order of length, the whole thing should take 3.5ms or so, and then wait for a minimum latch of 20ms

bylestones:
Of course they will respond, that's not an issue, a pulse is a pulse: the servo will respond just as happily to a reminder next week if there's nothing in between. The servo doesn't know or care if the pulse it gets is a reminder the same as the last one, or a new position any time later.

But if the pulses are too far apart, the mechanical loads may cause the servo to move in the meantime.

Of course, although actually if the latch gets big the first pulse may not trigger the servo (the 2nd or the 3rd probably will) What i am saying is that there is something to be said for controlling the time that the pulses are sent manually. Firstly if i have any other part of my code that turns interrupts off, i am expecting to have conflicts (and who doesn't want moving led strip ) And as there are also systems that do use interrupts but my not want to many interruptions by timers. Actually if there is anybody into creating an alternate Servo library let me know.