Advertisement
JonD1988

DIY Arduino Robotic Arm Testing Rev 2

Sep 4th, 2021
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Rev 2 changed Joint 2 Minimum Axis Limit
  2.  
  3. #include <Servo.h>
  4.  
  5. Servo Servo1;
  6. Servo Servo2;
  7. Servo Servo3;
  8. Servo Servo4;
  9. Servo Servo5;
  10. Servo Servo6;
  11.  
  12. int J1 = 5; //Joint 1 Servo Attached to Pin 5
  13. int J2 = 6; //Joint 2 Servo Attached to Pin 6
  14. int J3 = 7; //Joint 3 Servo Attached to Pin 7
  15. int J4 = 8; //Joint 4 Servo Attached to Pin 8
  16. int J5 = 9; //Joint 5 Servo Attached to Pin 9
  17. int J6 = 10; //Joint 6/Gripper Servo Attached to Pin 10
  18.  
  19. int PotPin1 = A0; //Joint 1 Potentiometer is Tied to Pin A0
  20. int PotPin2 = A1; //Joint 2 Potentiometer is Tied to Pin A1
  21. int PotPin3 = A2; //Joint 3 Potentiometer is Tied to Pin A2
  22. int PotPin4 = A3; //Joint 4 Potentiometer is Tied to Pin A3
  23. int PotPin5 = A4; //Joint 5 Potentiometer is Tied to Pin A4
  24. int PotPin6 = A5; //Joint 6 Potentiometer is Tied to Pin A5
  25. int PotVal1 = 0; //Stores Potentiometer 1 Value
  26. int PotVal2 = 0; //Stores Potentiometer 2 Value
  27. int PotVal3 = 0; //Stores Potentiometer 3 Value
  28. int PotVal4 = 0; //Stores Potentiometer 4 Value
  29. int PotVal5 = 0; //Stores Potentiometer 5 Value
  30. int PotVal6 = 0; //Stores Potentiometer 6 Value
  31. int angle1 = 0; //Stores Angle to Move Servo 1 To
  32. int angle2 = 0; //Stores Angle to Move Servo 2 To
  33. int angle3 = 0; //Stores Angle to Move Servo 3 To
  34. int angle4 = 0; //Stores Angle to Move Servo 4 To
  35. int angle5 = 0; //Stores Angle to Move Servo 5 To
  36. int angle6 = 0; //Stores Angle to Move Servo 6 To
  37.  
  38. void setup() {
  39.   // put your setup code here, to run once:
  40.   Serial.begin(9600);
  41.   pinMode(PotPin1, INPUT); //Sets Pin Potentiometer 1 is Tied to To an Input
  42.   pinMode(PotPin2, INPUT); //Sets Pin Potentiometer 2 is Tied to To an Input
  43.   pinMode(PotPin3, INPUT); //Sets Pin Potentiometer 3 is Tied to To an Input
  44.   pinMode(PotPin4, INPUT); //Sets Pin Potentiometer 4 is Tied to To an Input
  45.   pinMode(PotPin5, INPUT); //Sets Pin Potentiometer 5 is Tied to To an Input
  46.   pinMode(PotPin6, INPUT); //Sets Pin Potentiometer 6 is Tied to To an Input
  47.   Servo1.attach(J1); //Calls this is like a pinmode command but also does more from the servo library
  48.   Servo2.attach(J2); //Calls this is like a pinmode command but also does more from the servo library
  49.   Servo3.attach(J3); //Calls this is like a pinmode command but also does more from the servo library
  50.   Servo4.attach(J4); //Calls this is like a pinmode command but also does more from the servo library
  51.   Servo5.attach(J5); //Calls this is like a pinmode command but also does more from the servo library
  52.   Servo6.attach(J6); //Calls this is like a pinmode command but also does more from the servo library
  53. }
  54.  
  55. void loop() {
  56.   // put your main code here, to run repeatedly:
  57.   PotVal1 = analogRead(PotPin1); //Reads Voltage Between 0 and 5 V and Gives Number Between 0 and 1023 Where 2^8 = 1024.  Starts at 0 therefore 1023.
  58.   PotVal2 = analogRead(PotPin2); //Reads Voltage Between 0 and 5 V and Gives Number Between 0 and 1023 Where 2^8 = 1024.  Starts at 0 therefore 1023.
  59.   PotVal3 = analogRead(PotPin3); //Reads Voltage Between 0 and 5 V and Gives Number Between 0 and 1023 Where 2^8 = 1024.  Starts at 0 therefore 1023.
  60.   PotVal4 = analogRead(PotPin4); //Reads Voltage Between 0 and 5 V and Gives Number Between 0 and 1023 Where 2^8 = 1024.  Starts at 0 therefore 1023.
  61.   PotVal5 = analogRead(PotPin5); //Reads Voltage Between 0 and 5 V and Gives Number Between 0 and 1023 Where 2^8 = 1024.  Starts at 0 therefore 1023.
  62.   PotVal6 = analogRead(PotPin6); //Reads Voltage Between 0 and 5 V and Gives Number Between 0 and 1023 Where 2^8 = 1024.  Starts at 0 therefore 1023.
  63.   Serial.print("PotVal1: ");
  64.   Serial.print(PotVal1);
  65.   Serial.print(", ");
  66.   Serial.print("PotVal2: ");
  67.   Serial.print(PotVal2);
  68.   Serial.print(", ");
  69.   Serial.print("PotVal3: ");
  70.   Serial.print(PotVal3);
  71.   Serial.print(", ");
  72.   Serial.print("PotVal4: ");
  73.   Serial.print(PotVal4);
  74.   Serial.print(", ");
  75.   Serial.print("PotVal5: ");
  76.   Serial.print(PotVal5);
  77.   Serial.print(", ");
  78.   Serial.print("PotVal6: ");
  79.   Serial.print(PotVal6);
  80.   Serial.println(".");
  81.   // scale the numbers from the pot
  82.   angle1 = map(PotVal1, 0, 1023, 0, 180); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  83.   angle2 = map(PotVal2, 0, 1023, 50, 170); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range - Default Value 100 to 170 - But if leave at 100 it cannot lift the arm back up
  84.   angle3 = map(PotVal3, 0, 1023, 20, 180); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  85.   angle4 = map(PotVal4, 0, 1023, 0, 160); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  86.   angle5 = map(PotVal5, 0, 1023, 20, 150); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  87.   angle6 = map(PotVal6, 0, 1023, 45, 125); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  88.   // print out the angle for the servo motor
  89.   Serial.print("Angle 1: ");
  90.   Serial.print(angle1);
  91.   Serial.print(", ");
  92.   Serial.print("Angle 2: ");
  93.   Serial.print(angle2);
  94.   Serial.print(", ");
  95.   Serial.print("Angle 3: ");
  96.   Serial.print(angle3);
  97.   Serial.print(", ");
  98.   Serial.print("Angle 4: ");
  99.   Serial.print(angle4);
  100.   Serial.print(", ");
  101.   Serial.print("Angle 5: ");
  102.   Serial.print(angle5);
  103.   Serial.print(", ");
  104.   Serial.print("Angle 6: ");
  105.   Serial.print(angle6);
  106.   Serial.println(".");
  107.  
  108.   Servo1.write(angle1); //Moves Specified Servo to Specified Angle
  109.   Servo2.write(angle2); //Moves Specified Servo to Specified Angle
  110.   Servo3.write(angle3); //Moves Specified Servo to Specified Angle
  111.   //Servo4.write(angle4); //Moves Specified Servo to Specified Angle
  112.   Servo5.write(angle5); //Moves Specified Servo to Specified Angle
  113.   Servo6.write(angle6); //Moves Specified Servo to Specified Angle
  114.  
  115.   delay(15); //Gives Servos time to move to position
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement