iasatan

Serial

Mar 20th, 2016
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 0 0
  1. #include <Servo.h>
  2. #include<robotarm.h>
  3.  
  4. //Define variables
  5.  
  6. //Servos
  7. #define servoClawPin              8
  8. #define handPin                   9
  9. #define elbowPin                  10
  10. #define shoulderPanPin            11
  11. #define shoulderTiltPin           12
  12.  
  13. #define buttonPin                 5
  14.  
  15.  
  16.  
  17.  
  18. Servo clawServo;  
  19. Servo handServo;      
  20. Servo elbowServo;
  21. Servo shoulderPanServo;
  22. Servo shoulderTiltServo;
  23.  
  24.  
  25.  
  26. byte clawPosition;
  27. byte clawPrevPos=144;
  28. byte handPosition;
  29. byte handPrevPos=90;
  30. byte elbowPosition;
  31. byte elbowPrevPos=90;
  32. byte shoulderPanPosition;
  33. byte shoulderPanPrevPos=90;
  34. byte shoulderTiltPosition;
  35. byte shoulderTiltPrevPos=90;
  36.  
  37. byte hundredByte;
  38. byte tenByte;
  39. byte oneByte;
  40.  
  41. char hundredChar='0';
  42. char tenChar='0';
  43. char oneChar='0';
  44.  
  45. byte serialPos;
  46. char serialServo;
  47.  
  48.  
  49. void setup()
  50. {
  51.  
  52.   pinMode(servoClawPin, OUTPUT);
  53.   pinMode(handPin, OUTPUT);
  54.   pinMode(elbowPin, OUTPUT);
  55.   pinMode(shoulderPanPin, OUTPUT);
  56.   pinMode(shoulderTiltPin, OUTPUT);
  57.  
  58.   Serial.begin(9600);
  59.  
  60.   clawServo.attach(8);
  61.   handServo.attach(9);
  62.   elbowServo.attach(10);
  63.   shoulderPanServo.attach(11);
  64.   shoulderTiltServo.attach(12);
  65.  
  66.   pinMode(buttonPin, INPUT);
  67.   elbowPosition = constrain(elbowPosition, 0, 145);
  68. }
  69.  
  70.  
  71.  
  72. void loop()
  73. {
  74.   delay(200);
  75.   if(Serial.available())
  76.   {
  77.     serialServo=Serial.read();
  78.     Serial.println(serialServo);
  79.   }
  80.   if(Serial.available())
  81.   {
  82.     hundredChar=Serial.read();
  83.     Serial.println(hundredChar-'0');
  84.   }
  85.   if(Serial.available())
  86.   {
  87.     tenChar=Serial.read();
  88.     Serial.println(tenChar-'0');
  89.   }
  90.   if (Serial.available())
  91.   {
  92.     oneChar=Serial.read();
  93.     Serial.println(oneChar-'0');
  94.   }
  95.   hundredByte=hundredChar-'0';
  96.   tenByte=tenChar-'0';
  97.   oneByte=oneChar-'0';
  98.   hundredByte=hundredByte*100;
  99.   tenByte=tenByte*10;
  100.   serialPos=hundredByte+tenByte+oneByte;
  101.   Serial.println(serialPos);
  102.   if(serialServo=='c')//claw
  103.               {
  104.                 Serial.println("Claw");
  105.                 serialPos = constrain(serialPos, 116, 180);
  106.                 clawServo.write(serialPos);
  107.                 clawPrevPos=serialPos;
  108.                 serialServo=' ';
  109.               }
  110.               else if(serialServo=='h')//hand
  111.               {
  112.                 Serial.println("hand");
  113.                 migrate(handServo,serialPos, 30, handPrevPos);
  114.                 //handServo.write(serialPos);
  115.                 handPrevPos=serialPos;
  116.                 serialServo=' ';
  117.               }
  118.               else if(serialServo=='e')//elbow
  119.               {
  120.                 Serial.println("elbow");
  121.                 elbowPosition = constrain(serialPos, 0, 145);
  122.                 migrate(elbowServo,elbowPosition, 30,elbowPrevPos);
  123.                 //elbowServo.write(elbowPosition);
  124.                 elbowPrevPos=elbowPosition;
  125.                 serialServo=' ';
  126.               }
  127.               else if(serialServo=='p')//shoulderPan
  128.               {
  129.                 Serial.println("shoulderPan");
  130.                 migrate(shoulderPanServo,serialPos, 30, shoulderPanPrevPos);
  131.                 //shoulderPanServo.write(serialPos);
  132.                 shoulderPanPrevPos=serialPos;
  133.                 serialServo=' ';
  134.               }
  135.               else if(serialServo=='t')//shoulderTilt
  136.               {
  137.                 Serial.println("shoulderTilt");
  138.                 migrate(shoulderTiltServo,serialPos, 30,shoulderTiltPrevPos);
  139.                 //shoulderTiltServo.write(serialPos);
  140.                 shoulderTiltPrevPos=serialPos;
  141.                 serialServo=' ';
  142.               }
  143.               delay(500);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment