Advertisement
JonD1988

RoboticArmControlsRemoteTestRxRev0

Dec 22nd, 2021
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.65 KB | None | 0 0
  1. //Code to Test Robotic Arm Receiver (Connected to Arm) - Written by Jonathan DeWitt
  2.  
  3. #include <Servo.h>
  4. #include <SoftwareSerial.h>
  5. #include <Wire.h> //Not that this was actually needed.  Was used in Reference 1
  6.  
  7. SoftwareSerial HC12(8,7); // Arduino Pin 8 Rx to Tx on HC-12, Arduino Pin 7 Tx to Rx on HC-12
  8.  
  9. Servo Servo1;
  10. Servo Servo2;
  11. Servo Servo3;
  12. Servo Servo4;
  13. Servo Servo5;
  14. Servo Servo6;
  15.  
  16. int J1 = 5; //J1 Servo Signal Wire Attached to Pin 5
  17. int J2 = 6; //J2 Servo Signal Wire Attached to Pin 6
  18. int J3 = 9; //J3 Servo Signal Wire Attached to Pin 9
  19. int J4 = 10; //J4 Servo Signal Wire Attached to Pin 10
  20. int J5 = 11; //J5 Servo Signal Wire Attached to Pin 11
  21. int J6 = 3; //J6 Servo Signal Wire Attached to Pin 3
  22.  
  23. // int PotPin1 = A0; //Joint 1 Potentiometer is Tied to Pin A0
  24. // int PotPin2 = A1; //Joint 2 Potentiometer is Tied to Pin A1
  25. // int PotPin3 = A2; //Joint 3 Potentiometer is Tied to Pin A2
  26. // int PotPin4 = A3; //Joint 4 Potentiometer is Tied to Pin A3
  27. // int PotPin5 = A4; //Joint 5 Potentiometer is Tied to Pin A4
  28. // int PotPin6 = A5; //Joint 6 Potentiometer is Tied to Pin A5
  29.  
  30. // int PotVal1 = 0; //Stores Potentiometer 1 Value
  31. // int PotVal2 = 0; //Stores Potentiometer 2 Value
  32. // int PotVal3 = 0; //Stores Potentiometer 3 Value
  33. // int PotVal4 = 0; //Stores Potentiometer 4 Value
  34. // int PotVal5 = 0; //Stores Potentiometer 5 Value
  35. // int PotVal6 = 0; //Stores Potentiometer 6 Value
  36.  
  37. // int angle1 = 0; //Stores Angle to Move Servo 1 To
  38. // int angle2 = 0; //Stores Angle to Move Servo 2 To
  39. // int angle3 = 0; //Stores Angle to Move Servo 3 To
  40. // int angle4 = 0; //Stores Angle to Move Servo 4 To
  41. // int angle5 = 0; //Stores Angle to Move Servo 5 To
  42. // int angle6 = 0; //Stores Angle to Move Servo 6 To
  43.  
  44. int angle1; //Stores Angle to Move Servo 1 To
  45. int angle2; //Stores Angle to Move Servo 2 To
  46. int angle3; //Stores Angle to Move Servo 3 To
  47. int angle4; //Stores Angle to Move Servo 4 To
  48. int angle5; //Stores Angle to Move Servo 5 To
  49. int angle6; //Stores Angle to Move Servo 6 To
  50.  
  51. //The following variables are needed only to receive data from the HC-12 remote control
  52. String input;
  53. int boundLow;
  54. int boundHigh;
  55. const char delimiter = ',';
  56.  
  57. void setup() {
  58.   // put your setup code here, to run once:
  59.   Serial.begin(9600);
  60.  
  61.   // pinMode(PotPin1, INPUT); //Sets Pin Potentiometer 1 is Tied to To an Input
  62.   // pinMode(PotPin2, INPUT); //Sets Pin Potentiometer 2 is Tied to To an Input
  63.   // pinMode(PotPin3, INPUT); //Sets Pin Potentiometer 3 is Tied to To an Input
  64.   // pinMode(PotPin4, INPUT); //Sets Pin Potentiometer 4 is Tied to To an Input
  65.   // pinMode(PotPin5, INPUT); //Sets Pin Potentiometer 5 is Tied to To an Input
  66.   // pinMode(PotPin6, INPUT); //Sets Pin Potentiometer 6 is Tied to To an Input
  67.  
  68.   Servo1.attach(J1); //Calls this is like a pinmode command but also does more from the servo library
  69.   Servo2.attach(J2); //Calls this is like a pinmode command but also does more from the servo library
  70.   Servo3.attach(J3); //Calls this is like a pinmode command but also does more from the servo library
  71.   Servo4.attach(J4); //Calls this is like a pinmode command but also does more from the servo library
  72.   Servo5.attach(J5); //Calls this is like a pinmode command but also does more from the servo library
  73.   Servo6.attach(J6); //Calls this is like a pinmode command but also does more from the servo library
  74.  
  75.   //Move the robot to a good home pose
  76.   Servo1.write(90); //Moves Specified Servo to Specified Angle
  77.   Servo2.write(110); //Moves Specified Servo to Specified Angle
  78.   Servo3.write(120); //Moves Specified Servo to Specified Angle
  79.   Servo4.write(50); //Moves Specified Servo to Specified Angle
  80.   Servo5.write(50); //Moves Specified Servo to Specified Angle
  81.   Servo6.write(120); //Moves Specified Servo to Specified Angle
  82.  
  83.    HC12.begin(9600); //Start the HC-12 Module
  84.    delay(2000); //I assume this delay is to give the HC-12 time to read
  85. }
  86.  
  87. void loop() {
  88.   // put your main code here, to run repeatedly:
  89.  
  90. //  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.
  91. //  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.
  92. //  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.
  93. //  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.
  94. //  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.
  95. //  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.
  96. //
  97. //  Serial.print("PotVal1: ");
  98. //  Serial.print(PotVal1);
  99. //  Serial.print(", ");
  100. //  Serial.print("PotVal2: ");
  101. //  Serial.print(PotVal2);
  102. //  Serial.print(", ");
  103. //  Serial.print("PotVal3: ");
  104. //  Serial.print(PotVal3);
  105. //  Serial.print(", ");
  106. //  Serial.print("PotVal4: ");
  107. //  Serial.print(PotVal4);
  108. //  Serial.print(", ");
  109. //  Serial.print("PotVal5: ");
  110. //  Serial.print(PotVal5);
  111. //  Serial.print(", ");
  112. //  Serial.print("PotVal6: ");
  113. //  Serial.print(PotVal6);
  114. //  Serial.println(".");
  115. //  // scale the numbers from the pot
  116. //  angle1 = map(PotVal1, 0, 1023, 20, 150); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  117. //  angle2 = map(PotVal2, 0, 1023, 110, 150); //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
  118. //  angle3 = map(PotVal3, 0, 1023, 100, 180); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  119. //  angle4 = map(PotVal4, 0, 1023, 0, 140); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  120. //  angle5 = map(PotVal5, 0, 1023, 10, 140); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  121. //  angle6 = map(PotVal6, 0, 1023, 50, 120); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  122.  
  123. if(HC12.available()) //This section is receiving data from the HC-12 Module - Purely for Remote Control
  124.    {
  125.    input = HC12.readStringUntil('n');
  126.    if (input.length() > 0)
  127.        {
  128.         Serial.println(input);
  129.  
  130.        boundLow = input.indexOf(delimiter); //Locates index of first delimiter i.e. comma https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/indexof/
  131.        angle1 = input.substring(0, boundLow).toInt(); //.substring creates a sub string of input string starting at index position zero up to but not including first comma https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/substring/
  132.        
  133.        boundHigh = input.indexOf(delimiter, boundLow+1); //This instance is going to search for the next delimiter i.e. comma starting at boundLow+1 i.e. one character after the previous comma https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/indexof/
  134.        angle2 = input.substring(boundLow+1, boundHigh).toInt(); //Creates a substring of input starting one character after the first comma ending one character before the second comma
  135.  
  136.        boundLow = input.indexOf(delimiter, boundHigh+1); //Does the same thing as the previous boundHigh line except this locates third comma's index
  137.        angle3 = input.substring(boundHigh+1, boundLow).toInt(); //Creates a substring from after second comma up to just before third comma
  138.  
  139.        boundHigh = input.indexOf(delimiter, boundLow+1); //Repeats first boundHigh line operation, Locates fourth comma's index
  140.        angle4 = input.substring(boundLow+1, boundHigh).toInt(); //Creates a subtring of input starting one character after third comma up to just before fourth comma
  141.        
  142.        boundLow = input.indexOf(delimiter, boundHigh+1); //Does the same thing as the previous boundHigh line except this locates fifth comma's index
  143.        angle5 = input.substring(boundHigh+1, boundLow).toInt(); //Creates a substring from after fourth comma up to just before fifth comma
  144.  
  145.        angle6 = input.substring(boundLow+1).toInt(); //Creates a substring of input starting one character after fifth comma up to the end of input
  146.  
  147.        delay(10);  
  148.  }
  149.  }
  150.  
  151. //Print out the angle for the servo motor to the serial monitor for debugging purposes
  152.     Serial.print("Angle 1: ");
  153.     Serial.print(angle1);
  154.     Serial.print(", ");
  155.     Serial.print("Angle 2: ");
  156.     Serial.print(angle2);
  157.     Serial.print(", ");
  158.     Serial.print("Angle 3: ");
  159.     Serial.print(angle3);
  160.     Serial.print(", ");
  161.     Serial.print("Angle 4: ");
  162.     Serial.print(angle4);
  163.     Serial.print(", ");
  164.     Serial.print("Angle 5: ");
  165.     Serial.print(angle5);
  166.     Serial.print(", ");
  167.     Serial.print("Angle 6: ");
  168.     Serial.print(angle6);
  169.     Serial.println(".");
  170.  
  171. //Servo.write commands are commented out for the purposes of figuring out where the bugs are in the code.
  172. //In the final version these will not be commented out
  173.   // Servo1.write(angle1); //Moves Specified Servo to Specified Angle
  174.   // Servo2.write(angle2); //Moves Specified Servo to Specified Angle
  175.   // Servo3.write(angle3); //Moves Specified Servo to Specified Angle
  176.   // Servo4.write(angle4); //Moves Specified Servo to Specified Angle
  177.   // Servo5.write(angle5); //Moves Specified Servo to Specified Angle
  178.   // Servo6.write(angle6); //Moves Specified Servo to Specified Angle
  179.  
  180.   delay(15); //Gives Servos time to move to position
  181.  
  182. }
  183.  
  184. //Notes
  185. //Restrict Joint 1 from 20 to 150 degrees.  It can go down to 0 degrees but buzzes.  I haven't taken it over 150.  It might go further
  186. //Restrict Joint 2 from 110 to 150 degrees.  It can go down to 110 degrees but buzzes a little.  Any lower it can really buzz.  Over 150 it really buzzes.
  187. //Restrict Joint 3 from 100 to 180 degrees.  100 seems to be very high up position (arm raised).  180 seems to be the arm lowered.
  188. //Restrict Joint 4 from 0 to 140 degrees.  It can comfortably do this
  189. //Restrict Joint 5 from 10 to 140 degrees.  It can 0 to 150 (I tested these)
  190. //Restrict Joint 6 from 50 to 120 degrees.  50 is closed gripper.  120 is fully open.  
  191.  
  192. //A good resting pose
  193. //  Servo1.write(90); //Moves Specified Servo to Specified Angle
  194. //  Servo2.write(110); //Moves Specified Servo to Specified Angle
  195. //  Servo3.write(120); //Moves Specified Servo to Specified Angle
  196. //  Servo4.write(50); //Moves Specified Servo to Specified Angle
  197. //  Servo5.write(50); //Moves Specified Servo to Specified Angle
  198. //  Servo6.write(120); //Moves Specified Servo to Specified Angle
  199.  
  200. //Reference 1 https://rootsaid.com/long-range-remote-controller/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement