Adjusting code from Arduino Uno to Mega 2560

The following code is used for Arduino Uno to control servos in a robotic arm, and I have the Arduino MEGA 2560. It needs to be updated so I am able to control the arm itself. Any help would be appreciated!

#include <Servo.h>
Servo servo1; // 建立Servo实例
Servo servo2;
const int servo1Pin = 8; // servo1 接 Pin 8
const int servo2Pin = 9; // servo2 接 Pin 9
const int every1 = 15;
const int every2 = 15;
int servo1Target = 0;
int servo2Target = 0;

int servo1Dir = 10;
int servo2Dir = 1;

int posOfServo1 = 90;
int posOfServo2 = 90;
void setup()
{
servo1.attach(servo1Pin, 500, 2500); // pin8,
servo2.attach(servo2Pin, 500, 2500); // pin9
Serial.begin(9600);
servo1.write(posOfServo1);
servo2.write(posOfServo2);
Serial.println("2 Servo test");
delay(500);
}
void loop( ) {
checkServo1();
checkServo2();

if (posOfServo1 > 179)
servo1Target = 0;
if (posOfServo1 < 1 )
servo1Target = 180;

if (posOfServo2 > 179)
servo2Target = 0;
if (posOfServo2 < 1 )
servo2Target = 180;
}

void checkServo1( ) {
static uint32_t Timer;
if (Timer > millis())
return;
Timer = millis() + every1;
if (servo1Target > posOfServo1) {
posOfServo1 += servo1Dir;
} else if (servo1Target == posOfServo1) {
return;
} else {
posOfServo1 += -servo1Dir;
}
servo1.write(posOfServo1);
}
void checkServo2( ) {
static uint32_t Timer;
if (Timer > millis())
return;
Timer = millis() + every2;
if (servo2Target > posOfServo2) {
posOfServo2 += servo2Dir;
} else if (servo2Target == posOfServo2) {
return;
} else {
posOfServo2 += -servo2Dir;
}
servo2.write(posOfServo2);
}

void checkServo3(){
//依Servo1,servo2,以同样方法添加更多舵机
}

Welcome to the forum.

The same sketch should run on the Arduino Uno and Arduino Mega.

There are a few problems:
When millis() is used for timing, then "millis - previousMillis" should be used, as you can see in the Blink Without Delay example.

This line might not work:

} else if (servo1Target == posOfServo1) {

The 'posOfServo1' is incremented with 10, so it can go passed the value of 'servo1Target'.

I haven't used servos on a 2560 but pins 8 and 9 are PWM so it should work as it is.

The Servo library uses a internal timer with a interrupt and handles the servo motors from within that interrupt. Any digital pin can be used. I made this yesterday (click on the start-button in the middle-upper of the screen):

How do you want to control the arm? Knobs? Commands over Serial? Some other way?

I wanted to control the arm using sensors on a human arm that would mirror the movement.

OK. Have you figured out your position sensors yet? One cheap and fairly accurate method would be potentiometers at each joint. You would need some levers connected to the arm to turn each potentiometer as the arm joint moves.

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