waterlubber

Antenna Rotator

Nov 17th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #define pitchPin 9 //Pin for pitch servo/stepper (up/down)
  2. #define yawPin 10 //Pin for yaw servo/stepper
  3. #define rotateDelay 10 //milliseconds per degree
  4.  
  5.  
  6. #include <Servo.h>
  7. Servo pitchServo;
  8. Servo yawServo;
  9. int pitch = 0;
  10. int yaw = 0;
  11.  
  12. void setup() {
  13.   Serial.begin(9600); //Comment
  14.   pitchServo.attach(pitchPin); //Set up servos
  15.   yawServo.attach(yawPin);
  16.   pitchServo.write(0); //Set up servo positions
  17.   yawServo.write(0);
  18.   Serial.println(F("Initialized, starting program. \n Please use degrees, \n from 0 to 360 for yaw and from 0 to 180 for pitch.")); //F() stores the string in flash memory, to save RAM
  19.  
  20. }
  21.  
  22. void loop() {
  23.   Serial.println("Enter Pitch: ");
  24.   while (!Serial.available()) {} //Wait until pitch is recieved
  25.   pitch = Serial.parseInt();
  26.   pitch = constrain(pitch, 0, 180);
  27.   Serial.print("Pitch: ");
  28.   Serial.println(pitch);
  29.   Serial.println("Enter Yaw: ");
  30.   while (!Serial.available()) {} //Wait until yaw is recieved
  31.   yaw = Serial.parseInt();
  32.   yaw = constrain(yaw, 0, 360);
  33.   Serial.print("Yaw: ");
  34.   Serial.println(yaw);
  35.   //Rotate the servos
  36.   rotate(pitch, yaw);
  37.   Serial.println(F("Rotation Successful"));
  38.  
  39. }
  40.  
  41. void rotate(int pitch, int yaw) { //This is a function so switching to stepper motors is easier lol
  42.   Serial.println(F("Rotating pitch..."));
  43.   pitchServo.write(pitch);
  44.   delay(pitch * rotateDelay + 100); //Let servo rotate
  45.   Serial.println(F("Rotating yaw..."));
  46.   yawServo.write(yaw);
  47.   delay(yaw * rotateDelay + 100);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment