Advertisement
Guest User

MeArm Controller Example

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