Advertisement
JonD1988

RoboticArmControlsRemoteTestTxRev3

Dec 23rd, 2021
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Code to Test Robotic Arm Remote Control - Written by Jonathan DeWitt
  2. //Difference Between Rev 0 and Rev 1
  3. //Rev 0 used Reference 1 as Technique for Transmitting data via the HC-12
  4. //Rev 1 used Reference 2 as Technique for Transmitting data via the HC-12
  5.  
  6. #include <Servo.h>
  7. #include <SoftwareSerial.h>
  8. #include <Wire.h> //Not that this was actually needed.  Was used in Reference 1
  9.  
  10. SoftwareSerial HC12(8,7); // Arduino Pin 8 Rx to Tx on HC-12, Arduino Pin 7 Tx to Rx on HC-12
  11.  
  12. // Servo Servo1;
  13. // Servo Servo2;
  14. // Servo Servo3;
  15. // Servo Servo4;
  16. // Servo Servo5;
  17. // Servo Servo6;
  18.  
  19. // int J1 = 5; //J1 Servo Signal Wire Attached to Pin 5
  20. // int J2 = 6; //J2 Servo Signal Wire Attached to Pin 6
  21. // int J3 = 9; //J3 Servo Signal Wire Attached to Pin 9
  22. // int J4 = 10; //J4 Servo Signal Wire Attached to Pin 10
  23. // int J5 = 11; //J5 Servo Signal Wire Attached to Pin 11
  24. // int J6 = 3; //J6 Servo Signal Wire Attached to Pin 3
  25.  
  26. int PotPin1 = A0; //Joint 1 Potentiometer is Tied to Pin A0
  27. int PotPin2 = A1; //Joint 2 Potentiometer is Tied to Pin A1
  28. int PotPin3 = A2; //Joint 3 Potentiometer is Tied to Pin A2
  29. int PotPin4 = A3; //Joint 4 Potentiometer is Tied to Pin A3
  30. int PotPin5 = A4; //Joint 5 Potentiometer is Tied to Pin A4
  31. int PotPin6 = A5; //Joint 6 Potentiometer is Tied to Pin A5
  32.  
  33. int PotVal1; //Stores Potentiometer 1 Value
  34. int PotVal2; //Stores Potentiometer 2 Value
  35. int PotVal3; //Stores Potentiometer 3 Value
  36. int PotVal4; //Stores Potentiometer 4 Value
  37. int PotVal5; //Stores Potentiometer 5 Value
  38. int PotVal6; //Stores Potentiometer 6 Value
  39.  
  40. int angle1; //Stores Angle to Move Servo 1 To
  41. int angle2; //Stores Angle to Move Servo 2 To
  42. int angle3; //Stores Angle to Move Servo 3 To
  43. int angle4; //Stores Angle to Move Servo 4 To
  44. int angle5; //Stores Angle to Move Servo 5 To
  45. int angle6; //Stores Angle to Move Servo 6 To
  46.  
  47. //The following variables are needed only to receive data from the HC-12 remote control
  48. String input;
  49. int boundLow;
  50. int boundHigh;
  51. const char delimiter = ',';
  52. const char finald = '.';
  53.  
  54. int stupD = 500; //Setup Delay
  55.  
  56. void setup() {
  57.   // put your setup code here, to run once:
  58.   Serial.begin(9600);
  59.  
  60.   pinMode(PotPin1, INPUT); //Sets Pin Potentiometer 1 is Tied to To an Input
  61.   pinMode(PotPin2, INPUT); //Sets Pin Potentiometer 2 is Tied to To an Input
  62.   pinMode(PotPin3, INPUT); //Sets Pin Potentiometer 3 is Tied to To an Input
  63.   pinMode(PotPin4, INPUT); //Sets Pin Potentiometer 4 is Tied to To an Input
  64.   pinMode(PotPin5, INPUT); //Sets Pin Potentiometer 5 is Tied to To an Input
  65.   pinMode(PotPin6, INPUT); //Sets Pin Potentiometer 6 is Tied to To an Input
  66.  
  67.   // Servo1.attach(J1); //Calls this is like a pinmode command but also does more from the servo library
  68.   // Servo2.attach(J2); //Calls this is like a pinmode command but also does more from the servo library
  69.   // Servo3.attach(J3); //Calls this is like a pinmode command but also does more from the servo library
  70.   // Servo4.attach(J4); //Calls this is like a pinmode command but also does more from the servo library
  71.   // Servo5.attach(J5); //Calls this is like a pinmode command but also does more from the servo library
  72.   // Servo6.attach(J6); //Calls this is like a pinmode command but also does more from the servo library
  73.  
  74.    HC12.begin(9600); //Start the HC-12 Module
  75.    delay(stupD); //I assume this delay is to give the HC-12 time to read
  76. }
  77.  
  78. void loop() {
  79.   // put your main code here, to run repeatedly:
  80.  
  81.   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.
  82.   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.
  83.   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.
  84.   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.
  85.   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.
  86.   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.
  87.  
  88. //  Serial.print("PotVal1: ");
  89. //  Serial.print(PotVal1);
  90. //  Serial.print(", ");
  91. //  Serial.print("PotVal2: ");
  92. //  Serial.print(PotVal2);
  93. //  Serial.print(", ");
  94. //  Serial.print("PotVal3: ");
  95. //  Serial.print(PotVal3);
  96. //  Serial.print(", ");
  97. //  Serial.print("PotVal4: ");
  98. //  Serial.print(PotVal4);
  99. //  Serial.print(", ");
  100. //  Serial.print("PotVal5: ");
  101. //  Serial.print(PotVal5);
  102. //  Serial.print(", ");
  103. //  Serial.print("PotVal6: ");
  104. //  Serial.print(PotVal6);
  105. //  Serial.println(".");
  106.  
  107.   // scale the numbers from the pot
  108.   angle1 = map(PotVal1, 0, 1023, 20, 150); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  109.   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
  110.   angle3 = map(PotVal3, 0, 1023, 100, 180); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  111.   angle4 = map(PotVal4, 0, 1023, 0, 140); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  112.   angle5 = map(PotVal5, 0, 1023, 10, 140); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  113.   angle6 = map(PotVal6, 0, 1023, 50, 120); //Scales Converted Values Between 0 and 1023 to Angles Setting Servo Range
  114.   // print out the angle for the servo motor
  115.   Serial.print("Angle 1: ");
  116.   Serial.print(angle1);
  117.   Serial.print(", ");
  118.   Serial.print("Angle 2: ");
  119.   Serial.print(angle2);
  120.   Serial.print(", ");
  121.   Serial.print("Angle 3: ");
  122.   Serial.print(angle3);
  123.   Serial.print(", ");
  124.   Serial.print("Angle 4: ");
  125.   Serial.print(angle4);
  126.   Serial.print(", ");
  127.   Serial.print("Angle 5: ");
  128.   Serial.print(angle5);
  129.   Serial.print(", ");
  130.   Serial.print("Angle 6: ");
  131.   Serial.print(angle6);
  132.   Serial.println(".");
  133.  
  134.   String angle1S = String(angle1); //Converts integer to a string
  135.   String angle2S = String(angle2); //Converts integer to a string
  136.   String angle3S = String(angle3); //Converts integer to a string
  137.   String angle4S = String(angle4); //Converts integer to a string
  138.   String angle5S = String(angle5); //Converts integer to a string
  139.   String angle6S = String(angle6); //Converts integer to a string
  140.  
  141.   String message = "s" + angle1S + delimiter + angle2S + delimiter + angle3S + delimiter + angle4S + delimiter + angle5S + delimiter + angle6S + finald + "e"; //Construct the Single Message String
  142.  
  143.   //This section transmits the angle readings to the HC-12 Module
  144.   HC12.print(message);
  145.   HC12.println(""); //the println line is absolutely necessary because the receive code looks for a new line character to know when the message has finished being received
  146.  
  147.   delay(100); //Delay from Reference 1 Transmit Code
  148. }
  149.  
  150. //Notes
  151. //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
  152. //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.
  153. //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.
  154. //Restrict Joint 4 from 0 to 140 degrees.  It can comfortably do this
  155. //Restrict Joint 5 from 10 to 140 degrees.  It can 0 to 150 (I tested these)
  156. //Restrict Joint 6 from 50 to 120 degrees.  50 is closed gripper.  120 is fully open.  
  157.  
  158. //A good resting pose
  159. //  Servo1.write(90); //Moves Specified Servo to Specified Angle
  160. //  Servo2.write(110); //Moves Specified Servo to Specified Angle
  161. //  Servo3.write(120); //Moves Specified Servo to Specified Angle
  162. //  Servo4.write(50); //Moves Specified Servo to Specified Angle
  163. //  Servo5.write(50); //Moves Specified Servo to Specified Angle
  164. //  Servo6.write(120); //Moves Specified Servo to Specified Angle
  165.  
  166. //Reference 1 https://rootsaid.com/long-range-remote-controller/
  167. //Reference 2 https://howtomechatronics.com/tutorials/arduino/arduino-and-hc-12-long-range-wireless-communication-module/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement