Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.47 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. //Servo Stuff
  4. int servoRotationPin = 0;
  5. int servoHorizontalPin = 1;
  6.  
  7. int rotMin = 0; //Min position of servo
  8. int rotMax = 160; //Max position of servo
  9. int rotStart = 100; //Start position of servo
  10.  
  11. int hozMin = 94; //Min position of servo
  12. int hozMax = 184; //Max position of servo
  13. int hozStart = 142; //Start position of servo
  14.  
  15. int panInterval = 5;
  16. int tiltInterval = 50;
  17.  
  18. Servo rotServo;
  19. Servo hozServo;
  20. Servo fwdServo;
  21.  
  22. int currentRotPosition = rotStart;
  23. int currentHosPosition = hozStart;
  24.  
  25.  
  26. int leftWheelPWMPin = 3;  //PWM (Sets Speed) Range from 0-255
  27. int rightWheelPWMPin = 9;  //PWM (Sets Speed) Range from 0-255
  28. int leftWheelDirectionPin = 2;  //Direction LOW is Forward. HIGH is Backwards
  29. int rightWheelDirectionPin = 8;  //Direction LOW is Forward. HIGH is Backwards
  30.  
  31. //Generally the wheels won't turn at the same speed. To fix this we use an offset to lower the power from the stronger motor.
  32. int leftWheelSpeedOffset = 0;
  33. int rightWheelSpeedOffset = 0;
  34.  
  35. int spd = 255; //Default speed
  36. char incomingByte = 0; //Single character used for serial communication to determine the direction to go
  37.  
  38. void setup()
  39. {
  40.   //Set control pins to be outputs
  41.   pinMode(leftWheelPWMPin, OUTPUT);  
  42.   pinMode(rightWheelPWMPin, OUTPUT);
  43.   pinMode(leftWheelDirectionPin, OUTPUT);
  44.   pinMode(rightWheelDirectionPin, OUTPUT);
  45.  
  46.   //Start the serial interface
  47.   Serial.begin(115200);
  48.  
  49.   //Servo setup
  50.   rotServo.attach(servoRotationPin);
  51.   hozServo.attach(servoHorizontalPin);
  52.  
  53.  //Servo set default points
  54.   rotServo.write(rotStart);
  55.   hozServo.write(hozStart);
  56. }
  57.  
  58. void loop()
  59. {
  60.   if (Serial.available() > 0) {
  61.     // read the incoming byte:
  62.     incomingByte = Serial.read();
  63.     switch(incomingByte){
  64.     case 70: //F
  65.       forward(spd);
  66.       break;
  67.     case 66: //B
  68.       backward(spd);
  69.       break;
  70.     case 76: //L
  71.       left(spd);
  72.       break;
  73.     case 82: //R
  74.       right(spd);
  75.       break;        
  76.     case 83: //S
  77.       brake();      
  78.       break;
  79.     case 65: //A
  80.       rotateLeft();
  81.       break;
  82.     case 67: //C
  83.       rotateRight();
  84.       break;
  85.     case 71: //G
  86.       armUp();
  87.       break;
  88.     case 72: //H
  89.       armDown();
  90.       break;
  91.     }
  92.   }
  93. }
  94.  
  95. void forward(int speed){
  96.   setSpeed(speed);
  97.   digitalWrite(leftWheelDirectionPin, LOW); //Left Side Forward
  98.   digitalWrite(rightWheelDirectionPin, LOW); //Right Side Forward
  99.  
  100.  
  101. }
  102.  
  103. void backward(int speed){
  104.   setSpeed(speed);
  105.   digitalWrite(leftWheelDirectionPin, HIGH); //Left Side Backward
  106.   digitalWrite(rightWheelDirectionPin, HIGH); //Right Side Backward
  107. }
  108.  
  109. void left(int speed){
  110.   setSpeed(speed);
  111.   digitalWrite(leftWheelDirectionPin, HIGH); //Left Side Backward
  112.   digitalWrite(rightWheelDirectionPin, LOW); //Right Side Forward
  113. }
  114.  
  115. void right(int speed){
  116.   setSpeed(speed);
  117.   digitalWrite(leftWheelDirectionPin, LOW);   //Left Side Forward
  118.   digitalWrite(rightWheelDirectionPin, HIGH); //Right Side Backward
  119. }
  120.  
  121. void brake(){
  122.   analogWrite(leftWheelPWMPin, 0);   //Set Left Side Speed to off
  123.   analogWrite(rightWheelPWMPin, 0);  //Set Right Side Speed to off
  124. }
  125.  
  126. void setSpeed(int speed){
  127.   int leftWheelSpeed = 0;
  128.   int rightWheelSpeed = 0;
  129.   if(speed + leftWheelSpeedOffset > 255){ //Make sure we don't set the pwm speed over the max of 255
  130.     leftWheelSpeed = 255;
  131.   }else if(speed + leftWheelSpeedOffset < 0){ //Make sure we don't set the pwm speed under the min of 0
  132.     leftWheelSpeed = 0;
  133.   }else{ //If it is in the range then just set it
  134.     leftWheelSpeed = speed + leftWheelSpeedOffset;
  135.   }
  136.  
  137.   if(speed + rightWheelSpeedOffset > 255){ //Make sure we don't set the pwm speed over the max of 255
  138.     rightWheelSpeed = 255;
  139.   }else if(speed + rightWheelSpeedOffset < 0){ //Make sure we don't set the pwm speed under the min of 0
  140.     rightWheelSpeed = 0;
  141.   }else{ //If it is in the range then just set it
  142.     rightWheelSpeed = speed + rightWheelSpeedOffset;
  143.   }
  144.   analogWrite(leftWheelPWMPin, leftWheelSpeed);    //Left Side Speed
  145.   analogWrite(rightWheelPWMPin, rightWheelSpeed);  //Right Side Speed
  146. }
  147.  
  148.  
  149.  
  150. void rotateLeft(){
  151.   if(currentRotPosition < rotMax){
  152.     rotServo.write(currentRotPosition+=10);
  153.   }
  154. }
  155.  
  156.  
  157. void rotateRight(){
  158.   if(currentRotPosition > rotMin){
  159.     rotServo.write(currentRotPosition-=10);
  160.   }
  161. }
  162.  
  163. void armUp(){
  164.   if(currentHosPosition < hozMax){
  165.     hozServo.write(currentHosPosition+=10);
  166.   }
  167. }
  168. void armDown(){
  169.   if(currentHosPosition > hozMin){
  170.     hozServo.write(currentHosPosition-=10);
  171.   }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement