Im supplying the arduino with a 12v external power supply and running it out of the Vin pin, would it not work from that?
The code is in a library in this website - GitHub - Circuito-io/Robotic-Arm
That is the main code below.
// Include Libraries
#include "Arduino.h"
#include "Joystick.h"
#include "Servo.h"
// Pin Definitions
#define JOYSTICK_PIN_SW 2
#define JOYSTICK_PIN_VRX A3
#define JOYSTICK_PIN_VRY A4
#define SERVOSM1_1_PIN_SIG 3
#define SERVOSM2_2_PIN_SIG 4
#define SERVOSM3_3_PIN_SIG 5
// Global variables and defines
const int servoSM1_1RestPosition = 20; //Starting position
const int servoSM1_1TargetPosition = 150; //Position when event is detected
const int servoSM2_2RestPosition = 20; //Starting position
const int servoSM2_2TargetPosition = 150; //Position when event is detected
const int servoSM3_3RestPosition = 20; //Starting position
const int servoSM3_3TargetPosition = 150; //Position when event is detected
// object initialization
Joystick joystick(JOYSTICK_PIN_VRX,JOYSTICK_PIN_VRY,JOYSTICK_PIN_SW);
Servo servoSM1_1;
Servo servoSM2_2;
Servo servoSM3_3;
// define vars for testing menu
const int timeout = 10000; //define timeout of 10 sec
char menuOption = 0;
long time0;
// Setup the essentials for your circuit to work. It runs first every time your circuit is powered with electricity.
void setup()
{
// Setup Serial which is useful for debugging
// Use the Serial Monitor to view printed messages
Serial.begin(9600);
while (!Serial) ; // wait for serial port to connect. Needed for native USB
Serial.println("start");
servoSM1_1.attach(SERVOSM1_1_PIN_SIG);
servoSM1_1.write(servoSM1_1RestPosition);
delay(100);
servoSM1_1.detach();
servoSM2_2.attach(SERVOSM2_2_PIN_SIG);
servoSM2_2.write(servoSM2_2RestPosition);
delay(100);
servoSM2_2.detach();
servoSM3_3.attach(SERVOSM3_3_PIN_SIG);
servoSM3_3.write(servoSM3_3RestPosition);
delay(100);
servoSM3_3.detach();
menuOption = menu();
}
// Main logic of your circuit. It defines the interaction between the components you selected. After setup, it runs over and over again, in an eternal loop.
void loop()
{
if(menuOption == '1') {
// PS2 X Y Axis Joystick Module - Test Code
// Read Joystick X,Y axis and press
int joystickX = joystick.getX();
int joystickY = joystick.getY();
int joystickSW = joystick.getSW();
Serial.print(F("X: ")); Serial.print(joystickX);
Serial.print(F("\tY: ")); Serial.print(joystickY);
Serial.print(F("\tSW: ")); Serial.println(joystickSW);
}
else if(menuOption == '2') {
// Servo - Generic Metal Gear (Micro Size) #1 - Test Code
// The servo will rotate to target position and back to resting position with an interval of 500 milliseconds (0.5 seconds)
servoSM1_1.attach(SERVOSM1_1_PIN_SIG); // 1. attach the servo to correct pin to control it.
servoSM1_1.write(servoSM1_1TargetPosition); // 2. turns servo to target position. Modify target position by modifying the 'ServoTargetPosition' definition above.
delay(500); // 3. waits 500 milliseconds (0.5 sec). change the value in the brackets (500) for a longer or shorter delay in milliseconds.
servoSM1_1.write(servoSM1_1RestPosition); // 4. turns servo back to rest position. Modify initial position by modifying the 'ServoRestPosition' definition above.
delay(500); // 5. waits 500 milliseconds (0.5 sec). change the value in the brackets (500) for a longer or shorter delay in milliseconds.
servoSM1_1.detach(); // 6. release the servo to conserve power. When detached the servo will NOT hold it's position under stress.
}
else if(menuOption == '3') {
// Servo - Generic Metal Gear (Micro Size) #2 - Test Code
// The servo will rotate to target position and back to resting position with an interval of 500 milliseconds (0.5 seconds)
servoSM2_2.attach(SERVOSM2_2_PIN_SIG); // 1. attach the servo to correct pin to control it.
servoSM2_2.write(servoSM2_2TargetPosition); // 2. turns servo to target position. Modify target position by modifying the 'ServoTargetPosition' definition above.
delay(500); // 3. waits 500 milliseconds (0.5 sec). change the value in the brackets (500) for a longer or shorter delay in milliseconds.
servoSM2_2.write(servoSM2_2RestPosition); // 4. turns servo back to rest position. Modify initial position by modifying the 'ServoRestPosition' definition above.
delay(500); // 5. waits 500 milliseconds (0.5 sec). change the value in the brackets (500) for a longer or shorter delay in milliseconds.
servoSM2_2.detach(); // 6. release the servo to conserve power. When detached the servo will NOT hold it's position under stress.
}
else if(menuOption == '4') {
// Servo - Generic Metal Gear (Micro Size) #3 - Test Code
// The servo will rotate to target position and back to resting position with an interval of 500 milliseconds (0.5 seconds)
servoSM3_3.attach(SERVOSM3_3_PIN_SIG); // 1. attach the servo to correct pin to control it.
servoSM3_3.write(servoSM3_3TargetPosition); // 2. turns servo to target position. Modify target position by modifying the 'ServoTargetPosition' definition above.
delay(500); // 3. waits 500 milliseconds (0.5 sec). change the value in the brackets (500) for a longer or shorter delay in milliseconds.
servoSM3_3.write(servoSM3_3RestPosition); // 4. turns servo back to rest position. Modify initial position by modifying the 'ServoRestPosition' definition above.
delay(500); // 5. waits 500 milliseconds (0.5 sec). change the value in the brackets (500) for a longer or shorter delay in milliseconds.
servoSM3_3.detach(); // 6. release the servo to conserve power. When detached the servo will NOT hold it's position under stress.
}
if (millis() - time0 > timeout)
{
menuOption = menu();
}
}
// Menu function for selecting the components to be tested
// Follow serial monitor for instrcutions
char menu()
{
Serial.println(F("\nWhich component would you like to test?"));
Serial.println(F("(1) PS2 X Y Axis Joystick Module"));
Serial.println(F("(2) Servo - Generic Metal Gear (Micro Size) #1"));
Serial.println(F("(3) Servo - Generic Metal Gear (Micro Size) #2"));
Serial.println(F("(4) Servo - Generic Metal Gear (Micro Size) #3"));
Serial.println(F("(menu) send anything else or press on board reset button\n"));
while (!Serial.available());
// Read data from serial monitor if received
while (Serial.available())
{
char c = Serial.read();
if (isAlphaNumeric(c))
{
if(c == '1')
Serial.println(F("Now Testing PS2 X Y Axis Joystick Module"));
else if(c == '2')
Serial.println(F("Now Testing Servo - Generic Metal Gear (Micro Size) #1"));
else if(c == '3')
Serial.println(F("Now Testing Servo - Generic Metal Gear (Micro Size) #2"));
else if(c == '4')
Serial.println(F("Now Testing Servo - Generic Metal Gear (Micro Size) #3"));
else
{
Serial.println(F("illegal input!"));
return 0;
}
time0 = millis();
return c;
}
}
}