Advertisement
babyyoda_

ServoRecordPlay

Jun 8th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <Servo.h> //Servo header file
  3.  
  4. //Declare object for 5 Servo Motors  
  5. Servo Servo_0;
  6. Servo Servo_1;
  7. Servo Servo_2;
  8. Servo Servo_3;
  9. Servo Gripper;
  10.  
  11. //Global Variable Declaration
  12. int S0_pos, S1_pos, S2_pos, S3_pos, G_pos;
  13. int P_S0_pos, P_S1_pos, P_S2_pos, P_S3_pos, P_G_pos;
  14. int C_S0_pos, C_S1_pos, C_S2_pos, C_S3_pos, C_G_pos;
  15. int POT_0,POT_1,POT_2,POT_3,POT_4;
  16.  
  17. int saved_data[700]; //Array for saving recorded data
  18.  
  19. int array_index=0;
  20. char incoming = 0;
  21.  
  22. int action_pos;
  23. int action_servo;
  24.  
  25. void setup() {
  26. Serial.begin(9600); //Serial Monitor for Debugging
  27.  
  28. //Declare the pins to which the Servo Motors are connected to
  29. Servo_0.attach(3);
  30. Servo_1.attach(5);
  31. Servo_2.attach(6);
  32. Servo_3.attach(9);
  33. Gripper.attach(10);
  34.  
  35. //Write the servo motors to initial position
  36. Servo_0.write(70);
  37. Servo_1.write(100);
  38. Servo_2.write(110);
  39. Servo_3.write(10);
  40. Gripper.write(10);
  41.  
  42. Serial.println("Press 'R' to Record and 'P' to play"); //Instruct the user
  43. }
  44.  
  45. void Read_POT() //Function to read the Analog value form POT and map it to Servo value
  46. {
  47.    POT_0 = analogRead(A0); POT_1 = analogRead(A1); POT_2 = analogRead(A2); POT_3 = analogRead(A3); POT_4 = analogRead(A4); //Read the Analog values form all five POT
  48.    S0_pos = map(POT_0,0,1024,10,170); //Map it for 1st Servo (Base motor)
  49.    S1_pos = map(POT_1,0,1024,10,170); //Map it for 2nd Servo (Hip motor)
  50.    S2_pos = map(POT_2,0,1024,10,170); //Map it for 3rd Servo (Shoulder motor)
  51.    S3_pos = map(POT_3,0,1024,10,170); //Map it for 4th Servo (Neck motor)
  52.    G_pos  = map(POT_4,0,1024,10,170);  //Map it for 5th Servo (Gripper motor)
  53. }
  54.  
  55. void Record() //Function to Record the movements of the Robotic Arm
  56. {
  57. Read_POT(); //Read the POT values  for 1st time
  58.  
  59. //Save it in a variable to compare it later
  60.    P_S0_pos = S0_pos;
  61.    P_S1_pos = S1_pos;
  62.    P_S2_pos = S2_pos;
  63.    P_S3_pos = S3_pos;
  64.    P_G_pos  = G_pos;
  65.    
  66. Read_POT(); //Read the POT value for 2nd time
  67.  
  68.    if (P_S0_pos == S0_pos) //If 1st and 2nd value are same
  69.    {
  70.     Servo_0.write(S0_pos); //Control the servo
  71.    
  72.     if (C_S0_pos != S0_pos) //If the POT has been turned
  73.     {
  74.       saved_data[array_index] = S0_pos + 0; //Save the new position to the array. Zero is added for zeroth motor (for understading purpose)
  75.       array_index++; //Increase the array index
  76.     }
  77.    
  78.     C_S0_pos = S0_pos; //Saved the previous value to check if the POT has been turned
  79.    }
  80.  
  81. //Similarly repeat for all 5 servo Motors
  82.    if (P_S1_pos == S1_pos)
  83.    {
  84.     Servo_1.write(S1_pos);
  85.    
  86.     if (C_S1_pos != S1_pos)
  87.     {
  88.       saved_data[array_index] = S1_pos + 1000; //1000 is added for 1st servo motor as differentiator
  89.       array_index++;
  90.     }
  91.    
  92.     C_S1_pos = S1_pos;
  93.    }
  94.  
  95.    if (P_S2_pos == S2_pos)
  96.    {
  97.     Servo_2.write(S2_pos);
  98.    
  99.     if (C_S2_pos != S2_pos)
  100.     {
  101.       saved_data[array_index] = S2_pos + 2000; //2000 is added for 2nd servo motor as differentiator
  102.       array_index++;
  103.     }
  104.    
  105.     C_S2_pos = S2_pos;
  106.    }
  107.  
  108.    if (P_S3_pos == S3_pos)
  109.    {
  110.     Servo_3.write(S3_pos);
  111.    
  112.     if (C_S3_pos != S3_pos)
  113.     {
  114.       saved_data[array_index] = S3_pos + 3000; //3000 is added for 3rd servo motor as differentiater
  115.       array_index++;
  116.     }
  117.    
  118.     C_S3_pos = S3_pos;  
  119.    }
  120.  
  121.    if (P_G_pos == G_pos)
  122.    {
  123.     Gripper.write(G_pos);
  124.    
  125.     if (C_G_pos != G_pos)
  126.     {
  127.       saved_data[array_index] = G_pos + 4000; //4000 is added for 4th servo motor as differentiator
  128.       array_index++;
  129.     }
  130.    
  131.     C_G_pos = G_pos;
  132.    }
  133.    
  134.   //Print the value for debugging
  135.   Serial.print(S0_pos);  Serial.print("  "); Serial.print(S1_pos); Serial.print("  "); Serial.print(S2_pos); Serial.print("  "); Serial.print(S3_pos); Serial.print("  "); Serial.println(G_pos);
  136.   Serial.print ("Index = "); Serial.println (array_index);
  137.   delay(100);
  138. }
  139.  
  140. void Play() //Functon to play the recorded movements on the Robotic ARM
  141. {
  142.   for (int Play_action=0; Play_action<array_index; Play_action++) //Navigate through every saved element in the array
  143.   {
  144.     action_servo = saved_data[Play_action] / 1000; //The fist character of the array element is split for knowing the servo number
  145.     action_pos = saved_data[Play_action] % 1000; //The last three characters of the array element is split to know the servo postion
  146.  
  147.     switch(action_servo){ //Check which servo motor should be controlled
  148.       case 0: //If zeroth motor
  149.         Servo_0.write(action_pos);
  150.       break;
  151.  
  152.       case 1://If 1st motor
  153.         Servo_1.write(action_pos);
  154.       break;
  155.  
  156.       case 2://If 2nd motor
  157.         Servo_2.write(action_pos);
  158.       break;
  159.  
  160.       case 3://If 3rd motor
  161.         Servo_3.write(action_pos);
  162.       break;
  163.  
  164.       case 4://If 4th motor
  165.         Gripper.write(action_pos);
  166.       break;
  167.     }
  168.  
  169.     delay(50);
  170.    
  171.   }
  172. }
  173.  
  174. void loop() {
  175.  
  176. if (Serial.available() > 1) //If something is received from serial monitor
  177. {
  178. incoming = Serial.read();
  179. if (incoming == 'R')
  180. Serial.println("Robotic Arm Recording Started......");
  181. if (incoming == 'P')
  182. Serial.println("Playing Recorded sequence");
  183. }
  184.  
  185. if (incoming == 'R') //If user has selected Record mode
  186. Record();
  187.  
  188. if (incoming == 'P') //If user has selected Play Mode
  189. Play();
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement