Advertisement
JonD1988

DIY Robotic Arm Sketch Rev 1

Sep 1st, 2021
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*        
  2.        DIY Arduino Robot Arm Smartphone Control  
  3.         by Dejan, www.HowToMechatronics.com  
  4. */
  5.  
  6. #include <SoftwareSerial.h>
  7. #include <Servo.h>
  8.  
  9. Servo servo01;
  10. Servo servo02;
  11. Servo servo03;
  12. Servo servo04;
  13. Servo servo05;
  14. Servo servo06;
  15.  
  16. SoftwareSerial Bluetooth(3, 4); // Arduino(RX, TX) - HC-05 Bluetooth (TX, RX)
  17.  
  18. int servo1Pos, servo2Pos, servo3Pos, servo4Pos, servo5Pos, servo6Pos; // current position
  19. int servo1PPos, servo2PPos, servo3PPos, servo4PPos, servo5PPos, servo6PPos; // previous position
  20. int servo01SP[50], servo02SP[50], servo03SP[50], servo04SP[50], servo05SP[50], servo06SP[50]; // for storing positions/steps
  21. int speedDelay = 20;
  22. int index = 0;
  23. String dataIn = "";
  24.  
  25. void setup() {
  26.   servo01.attach(5);
  27.   servo02.attach(6);
  28.   servo03.attach(7);
  29.   servo04.attach(8);
  30.   servo05.attach(9);
  31.   servo06.attach(10);
  32.   Serial3.begin(38400);
  33.   //Bluetooth.begin(38400); // Default baud rate of the Bluetooth module
  34.   Bluetooth.setTimeout(1);
  35.   delay(20);
  36.   // Robot arm initial position
  37.   servo1PPos = 90;
  38.   servo01.write(servo1PPos);
  39.   servo2PPos = 150;
  40.   servo02.write(servo2PPos);
  41.   servo3PPos = 35;
  42.   servo03.write(servo3PPos);
  43.   servo4PPos = 140;
  44.   servo04.write(servo4PPos);
  45.   servo5PPos = 85;
  46.   servo05.write(servo5PPos);
  47.   servo6PPos = 80;
  48.   servo06.write(servo6PPos);
  49. }
  50.  
  51. void loop() {
  52.   // Check for incoming data
  53.   if (Bluetooth.available() > 0) {
  54.     dataIn = Bluetooth.readString();  // Read the data as string
  55.    
  56.     // If "Waist" slider has changed value - Move Servo 1 to position
  57.     if (dataIn.startsWith("s1")) {
  58.       String dataInS = dataIn.substring(2, dataIn.length()); // Extract only the number. E.g. from "s1120" to "120"
  59.       servo1Pos = dataInS.toInt();  // Convert the string into integer
  60.       // We use for loops so we can control the speed of the servo
  61.       // If previous position is bigger then current position
  62.       if (servo1PPos > servo1Pos) {
  63.         for ( int j = servo1PPos; j >= servo1Pos; j--) {   // Run servo down
  64.           servo01.write(j);
  65.           delay(20);    // defines the speed at which the servo rotates
  66.         }
  67.       }
  68.       // If previous position is smaller then current position
  69.       if (servo1PPos < servo1Pos) {
  70.         for ( int j = servo1PPos; j <= servo1Pos; j++) {   // Run servo up
  71.           servo01.write(j);
  72.           delay(20);
  73.         }
  74.       }
  75.       servo1PPos = servo1Pos;   // set current position as previous position
  76.     }
  77.    
  78.     // Move Servo 2
  79.     if (dataIn.startsWith("s2")) {
  80.       String dataInS = dataIn.substring(2, dataIn.length());
  81.       servo2Pos = dataInS.toInt();
  82.  
  83.       if (servo2PPos > servo2Pos) {
  84.         for ( int j = servo2PPos; j >= servo2Pos; j--) {
  85.           servo02.write(j);
  86.           delay(50);
  87.         }
  88.       }
  89.       if (servo2PPos < servo2Pos) {
  90.         for ( int j = servo2PPos; j <= servo2Pos; j++) {
  91.           servo02.write(j);
  92.           delay(50);
  93.         }
  94.       }
  95.       servo2PPos = servo2Pos;
  96.     }
  97.     // Move Servo 3
  98.     if (dataIn.startsWith("s3")) {
  99.       String dataInS = dataIn.substring(2, dataIn.length());
  100.       servo3Pos = dataInS.toInt();
  101.       if (servo3PPos > servo3Pos) {
  102.         for ( int j = servo3PPos; j >= servo3Pos; j--) {
  103.           servo03.write(j);
  104.           delay(30);
  105.         }
  106.       }
  107.       if (servo3PPos < servo3Pos) {
  108.         for ( int j = servo3PPos; j <= servo3Pos; j++) {
  109.           servo03.write(j);
  110.           delay(30);
  111.         }
  112.       }
  113.       servo3PPos = servo3Pos;
  114.     }
  115.     // Move Servo 4
  116.     if (dataIn.startsWith("s4")) {
  117.       String dataInS = dataIn.substring(2, dataIn.length());
  118.       servo4Pos = dataInS.toInt();
  119.       if (servo4PPos > servo4Pos) {
  120.         for ( int j = servo4PPos; j >= servo4Pos; j--) {
  121.           servo04.write(j);
  122.           delay(30);
  123.         }
  124.       }
  125.       if (servo4PPos < servo4Pos) {
  126.         for ( int j = servo4PPos; j <= servo4Pos; j++) {
  127.           servo04.write(j);
  128.           delay(30);
  129.         }
  130.       }
  131.       servo4PPos = servo4Pos;
  132.     }
  133.     // Move Servo 5
  134.     if (dataIn.startsWith("s5")) {
  135.       String dataInS = dataIn.substring(2, dataIn.length());
  136.       servo5Pos = dataInS.toInt();
  137.       if (servo5PPos > servo5Pos) {
  138.         for ( int j = servo5PPos; j >= servo5Pos; j--) {
  139.           servo05.write(j);
  140.           delay(30);
  141.         }
  142.       }
  143.       if (servo5PPos < servo5Pos) {
  144.         for ( int j = servo5PPos; j <= servo5Pos; j++) {
  145.           servo05.write(j);
  146.           delay(30);
  147.         }
  148.       }
  149.       servo5PPos = servo5Pos;
  150.     }
  151.     // Move Servo 6
  152.     if (dataIn.startsWith("s6")) {
  153.       String dataInS = dataIn.substring(2, dataIn.length());
  154.       servo6Pos = dataInS.toInt();
  155.       if (servo6PPos > servo6Pos) {
  156.         for ( int j = servo6PPos; j >= servo6Pos; j--) {
  157.           servo06.write(j);
  158.           delay(30);
  159.         }
  160.       }
  161.       if (servo6PPos < servo6Pos) {
  162.         for ( int j = servo6PPos; j <= servo6Pos; j++) {
  163.           servo06.write(j);
  164.           delay(30);
  165.         }
  166.       }
  167.       servo6PPos = servo6Pos;
  168.     }
  169.     // If button "SAVE" is pressed
  170.     if (dataIn.startsWith("SAVE")) {
  171.       servo01SP[index] = servo1PPos;  // save position into the array
  172.       servo02SP[index] = servo2PPos;
  173.       servo03SP[index] = servo3PPos;
  174.       servo04SP[index] = servo4PPos;
  175.       servo05SP[index] = servo5PPos;
  176.       servo06SP[index] = servo6PPos;
  177.       index++;                        // Increase the array index
  178.     }
  179.     // If button "RUN" is pressed
  180.     if (dataIn.startsWith("RUN")) {
  181.       runservo();  // Automatic mode - run the saved steps
  182.     }
  183.     // If button "RESET" is pressed
  184.     if ( dataIn == "RESET") {
  185.       memset(servo01SP, 0, sizeof(servo01SP)); // Clear the array data to 0
  186.       memset(servo02SP, 0, sizeof(servo02SP));
  187.       memset(servo03SP, 0, sizeof(servo03SP));
  188.       memset(servo04SP, 0, sizeof(servo04SP));
  189.       memset(servo05SP, 0, sizeof(servo05SP));
  190.       memset(servo06SP, 0, sizeof(servo06SP));
  191.       index = 0;  // Index to 0
  192.     }
  193.   }
  194. }
  195.  
  196. // Automatic mode custom function - run the saved steps
  197. void runservo() {
  198.   while (dataIn != "RESET") {   // Run the steps over and over again until "RESET" button is pressed
  199.     for (int i = 0; i <= index - 2; i++) {  // Run through all steps(index)
  200.       if (Bluetooth.available() > 0) {      // Check for incomding data
  201.         dataIn = Bluetooth.readString();
  202.         if ( dataIn == "PAUSE") {           // If button "PAUSE" is pressed
  203.           while (dataIn != "RUN") {         // Wait until "RUN" is pressed again
  204.             if (Bluetooth.available() > 0) {
  205.               dataIn = Bluetooth.readString();
  206.               if ( dataIn == "RESET") {    
  207.                 break;
  208.               }
  209.             }
  210.           }
  211.         }
  212.         // If speed slider is changed
  213.         if (dataIn.startsWith("ss")) {
  214.           String dataInS = dataIn.substring(2, dataIn.length());
  215.           speedDelay = dataInS.toInt(); // Change servo speed (delay time)
  216.         }
  217.       }
  218.       // Servo 1
  219.       if (servo01SP[i] == servo01SP[i + 1]) {
  220.       }
  221.       if (servo01SP[i] > servo01SP[i + 1]) {
  222.         for ( int j = servo01SP[i]; j >= servo01SP[i + 1]; j--) {
  223.           servo01.write(j);
  224.           delay(speedDelay);
  225.         }
  226.       }
  227.       if (servo01SP[i] < servo01SP[i + 1]) {
  228.         for ( int j = servo01SP[i]; j <= servo01SP[i + 1]; j++) {
  229.           servo01.write(j);
  230.           delay(speedDelay);
  231.         }
  232.       }
  233.  
  234.       // Servo 2
  235.       if (servo02SP[i] == servo02SP[i + 1]) {
  236.       }
  237.       if (servo02SP[i] > servo02SP[i + 1]) {
  238.         for ( int j = servo02SP[i]; j >= servo02SP[i + 1]; j--) {
  239.           servo02.write(j);
  240.           delay(speedDelay);
  241.         }
  242.       }
  243.       if (servo02SP[i] < servo02SP[i + 1]) {
  244.         for ( int j = servo02SP[i]; j <= servo02SP[i + 1]; j++) {
  245.           servo02.write(j);
  246.           delay(speedDelay);
  247.         }
  248.       }
  249.  
  250.       // Servo 3
  251.       if (servo03SP[i] == servo03SP[i + 1]) {
  252.       }
  253.       if (servo03SP[i] > servo03SP[i + 1]) {
  254.         for ( int j = servo03SP[i]; j >= servo03SP[i + 1]; j--) {
  255.           servo03.write(j);
  256.           delay(speedDelay);
  257.         }
  258.       }
  259.       if (servo03SP[i] < servo03SP[i + 1]) {
  260.         for ( int j = servo03SP[i]; j <= servo03SP[i + 1]; j++) {
  261.           servo03.write(j);
  262.           delay(speedDelay);
  263.         }
  264.       }
  265.  
  266.       // Servo 4
  267.       if (servo04SP[i] == servo04SP[i + 1]) {
  268.       }
  269.       if (servo04SP[i] > servo04SP[i + 1]) {
  270.         for ( int j = servo04SP[i]; j >= servo04SP[i + 1]; j--) {
  271.           servo04.write(j);
  272.           delay(speedDelay);
  273.         }
  274.       }
  275.       if (servo04SP[i] < servo04SP[i + 1]) {
  276.         for ( int j = servo04SP[i]; j <= servo04SP[i + 1]; j++) {
  277.           servo04.write(j);
  278.           delay(speedDelay);
  279.         }
  280.       }
  281.  
  282.       // Servo 5
  283.       if (servo05SP[i] == servo05SP[i + 1]) {
  284.       }
  285.       if (servo05SP[i] > servo05SP[i + 1]) {
  286.         for ( int j = servo05SP[i]; j >= servo05SP[i + 1]; j--) {
  287.           servo05.write(j);
  288.           delay(speedDelay);
  289.         }
  290.       }
  291.       if (servo05SP[i] < servo05SP[i + 1]) {
  292.         for ( int j = servo05SP[i]; j <= servo05SP[i + 1]; j++) {
  293.           servo05.write(j);
  294.           delay(speedDelay);
  295.         }
  296.       }
  297.  
  298.       // Servo 6
  299.       if (servo06SP[i] == servo06SP[i + 1]) {
  300.       }
  301.       if (servo06SP[i] > servo06SP[i + 1]) {
  302.         for ( int j = servo06SP[i]; j >= servo06SP[i + 1]; j--) {
  303.           servo06.write(j);
  304.           delay(speedDelay);
  305.         }
  306.       }
  307.       if (servo06SP[i] < servo06SP[i + 1]) {
  308.         for ( int j = servo06SP[i]; j <= servo06SP[i + 1]; j++) {
  309.           servo06.write(j);
  310.           delay(speedDelay);
  311.         }
  312.       }
  313.     }
  314.   }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement