Guest User

MeArm Controller Example

a guest
Sep 22nd, 2015
4,406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.11 KB | None | 0 0
  1. /* MeArm Controller ~ Robotic Arm
  2. Created by Muhamad Andi Prasetyo
  3. Jum'at, 11 - 09 - 2015
  4. www.boarduino.blogspot.com
  5. */
  6.  
  7.  
  8. #include <SoftwareSerial.h>
  9. #include <Servo.h>
  10. Servo base;         //Create an object servo base
  11. Servo shoulder;     //Create an object servo shoulder
  12. Servo elbow;        //Create an object servo elbow
  13. Servo gripper;      //Create an object servo gripper
  14.  
  15. char ServoID;                 //Variable for Servo ID
  16. int hasilButton;              //Variable button pressed results
  17. int nilaiBase;                //Variable servo base value
  18. int nilaiShoulder;            //Variable servo Shoulder value
  19. int nilaiElbow;               //Variable servo Elbow value
  20. int nilaiGripper;             //Variable servo Gripper value
  21. const int increment = 5;      //Increase degree (B+, S+, E+, G+)
  22. const int decrement = 5;      //Decrease degree (B-, S-, E-, G-)
  23. String readString;            //Read string values
  24. SoftwareSerial BT(12,13);     //Bluetooth RX TX on pins 12 and 13
  25.  
  26. void setup() {
  27.   Serial.begin(9600);     //Set the Serial Baudrate 9600
  28.   BT.begin(38400);        //Set Baudrate Bluetooth accordance with your bluetooth module
  29.   pinMode(13,OUTPUT);     //Set pin 13 as output
  30.   base.attach(3);         //Set the servo base of the pin 3
  31.   shoulder.attach(5);     //Set the servo shoulder of the pin 5
  32.   elbow.attach(6);        //Set the servo elbow of the pin 6
  33.   gripper.attach(9);      //Set the servo gripper of the pin 9
  34.   base.write(90);         //Start positioning servo base to 90 degrees
  35.   shoulder.write(90);     //Start positioning servo shoulder to 90 degrees
  36.   elbow.write(90);        //Start positioning servo elbow to 90 degrees
  37.   gripper.write(90);      //Start positioning servo gripper to 90 degrees
  38. }
  39.  
  40. void loop() {
  41.   if (BT.available()) {                   //If bluetooth available
  42.     hasilButton = BT.read();              //Set hasilButton = Bluetooth Read
  43.     ServoID = BT.read();                  //Set ServoID = Bluetooth Read
  44.     base.write(nilaiBase);                //Set the degree of servo according nilaiBase
  45.     shoulder.write(nilaiShoulder);        //Set the degree of servo according nilaiShoulder
  46.     elbow.write(nilaiElbow);              //Set the degree of servo according nilaiElbow
  47.     gripper.write(nilaiGripper);          //Set the degree of servo according nilaiGripped
  48.  
  49.     if(hasilButton == 'X'){               //If the button = X (X can customize the application MeArm Controller)
  50.       digitalWrite(13, LOW);              //Turn off the led on pin 13
  51.     }
  52.     if(hasilButton == 'Z'){               //If the button = Z (Z can customize the application MeArm Controller)
  53.       digitalWrite(13, HIGH);             //Turn on the led on pin 13
  54.     }
  55.     if(hasilButton == 'a'){               //If the button = a (a can customize the application MeArm Controller)
  56.       nilaiBase-=decrement;               //Servo base value - value decrement
  57.     }
  58.     else if(hasilButton == 'A'){          //If the button = A (A can customize the application MeArm Controller)
  59.       nilaiBase+=increment;               //Servo base value + value increment
  60.     }
  61.     if(hasilButton == 's'){               //If the button = s (s can customize the application MeArm Controller)
  62.       nilaiShoulder-=decrement;           //Servo base shoulder - value decrement
  63.     }
  64.     else if(hasilButton == 'S'){          //If the button = S (S can customize the application MeArm Controller)
  65.       nilaiShoulder+=increment;           //Servo shoulder value + value increment
  66.     }
  67.     if(hasilButton == 'e'){               //If the button = e (e can customize the application MeArm Controller)
  68.       nilaiElbow-=decrement;              //Servo base elbow - value decrement
  69.     }
  70.     else if(hasilButton == 'E'){          //If the button = E (E can customize the application MeArm Controller)
  71.       nilaiElbow+=increment;              //Servo shoulder value + value increment
  72.     }
  73.     if(hasilButton == 'g'){               //If the button = g (g can customize the application MeArm Controller)
  74.       nilaiGripper-=decrement;            //Servo base gripper - value decrement
  75.     }
  76.     else if(hasilButton == 'G'){          //If the button = G (G can customize the application MeArm Controller)
  77.       nilaiGripper+=increment;            //Servo gripper value + value increment
  78.     }
  79.      
  80.     if(ServoID=='1'){                     //If ServoID = 1
  81.       baseServo_slider();                 //Call subroutine baseServo_slider
  82.     }
  83.    
  84.     if(ServoID=='2'){                     //If ServoID = 2
  85.       shoulderServo_slider();             //Call subroutine shoulderServo_slider
  86.     }
  87.    
  88.     if(ServoID=='3'){                     //If ServoID = 3
  89.       elbowServo_slider();                //Call subroutine elbowServo_slider
  90.     }
  91.     if(ServoID=='4'){                     //If ServoID = 4
  92.       gripperServo_slider();              //Call subroutine gripperServo_slider
  93.     }
  94.   }  
  95. }
  96.  
  97. void baseServo_slider(){
  98.         delayMicroseconds(300);  
  99.         while (BT.available()) {            
  100.           char c = BT.read();
  101.           readString += c;
  102.         }
  103.         if (readString.length() >0) {
  104.           Serial.print("Base : ");
  105.           Serial.println(readString.toInt());
  106.           nilaiBase = (readString.toInt());         //nilaiBase = readString from android
  107.           base.write(readString.toInt());           //Set the degree of servo base according to the value ReadString
  108.           readString="";
  109.         }
  110. }
  111.  
  112. void shoulderServo_slider(){
  113.         delayMicroseconds(300);
  114.         while (BT.available()) {
  115.           char c = BT.read();  
  116.           readString += c;        
  117.         }
  118.         if (readString.length() >0) {
  119.           Serial.print("Shoulder : ");
  120.           Serial.println(readString.toInt());
  121.           nilaiShoulder = (readString.toInt());    //nilaiShoulder = readString from android
  122.           shoulder.write(readString.toInt());      //Set the degree of servo shoulder according to the value ReadString
  123.           readString="";
  124.         }
  125. }
  126.  
  127. void elbowServo_slider(){
  128.         delayMicroseconds(300);
  129.         while (BT.available()) {
  130.           char c = BT.read();  
  131.           readString += c;        
  132.         }
  133.         if (readString.length() >0) {
  134.           Serial.print("Elbow : ");
  135.           Serial.println(readString.toInt());  
  136.           nilaiElbow = (readString.toInt());     //nilaiElbow = readString from android
  137.           elbow.write(readString.toInt());       //Set the degree of servo elbow according to the value ReadString
  138.           readString="";
  139.         }
  140. }
  141.  
  142. void gripperServo_slider(){
  143.         delayMicroseconds(300);
  144.         while (BT.available()) {
  145.           char c = BT.read();  
  146.           readString += c;        
  147.         }
  148.         if (readString.length() >0) {
  149.           Serial.print("Gripper : ");
  150.           Serial.println(readString.toInt());
  151.           nilaiGripper = (readString.toInt());     //nilaiGripper = readString from android  
  152.           gripper.write(readString.toInt());       //Set the degree of servo gripper according to the value ReadString
  153.           readString="";
  154.         }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment