Advertisement
iasatan

Servo with bracket

Feb 24th, 2016
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. //load libraries
  2. #include <Wire.h>
  3. #include <LCD.h>
  4. #include <LiquidCrystal_I2C.h>
  5. #include <Servo.h>
  6.  
  7. //Define variables
  8.  
  9. #define I2C_ADDR 0x27 //Define I2C Address where the PCF8574A is
  10. #define BACKLIGHT_PIN 3
  11. #define En_pin 2
  12. #define Rw_pin 1
  13. #define Rs_pin 0
  14. #define D4_pin 4
  15. #define D5_pin 5
  16. #define D6_pin 6
  17. #define D7_pin 7
  18. #define lcdColumn 16
  19. #define lcdRow 2
  20. #define joystickPanPin A0
  21. #define joystickClawPin A1
  22. #define servoPanPin 9
  23. #define servoClawPin 10
  24.  
  25. //Initialise the LCD
  26. LiquidCrystal_I2C lcd(I2C_ADDR, En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
  27. Servo panServo;
  28. Servo clawServo;
  29.  
  30. int i;
  31. int servoPanPosition = 90;
  32. int servoClawPosition = 140;
  33. int joystickPanSpeed = 0;
  34. int joystickClawSpeed = 0;
  35.  
  36.  
  37. void setup()
  38. {
  39. pinMode(servoPanPin, OUTPUT);
  40. pinMode(servoClawPin, OUTPUT);
  41. panServo.attach(servoPanPin);
  42. clawServo.attach(servoClawPin);
  43. lcd.begin (lcdColumn,lcdRow);
  44.  
  45. //Switch on the backlight
  46. lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  47. lcd.setBacklight(HIGH);
  48. }
  49.  
  50.  
  51. void loop()
  52. {
  53. joystickPanSpeed = (analogRead(joystickPanPin) - 518) / 50;
  54. // -512 to provide equal +/- numbers
  55. joystickClawSpeed = (analogRead(joystickClawPin) - 511) / -50;
  56. // negative 50 to reverse direction
  57.  
  58. servoPanPosition = constrain((servoPanPosition + joystickPanSpeed), 53, 180);
  59. servoClawPosition = constrain((servoClawPosition + joystickClawSpeed), 116, 180);
  60. // constarin function to not exceed servo limits
  61. lcd.setCursor(0,0);
  62. lcd.print("Claw pos: ");
  63. lcd.print(servoClawPosition);
  64. lcd.print(" ");
  65. lcd.setCursor(0,1);
  66. lcd.print("Pan pos: ");
  67. lcd.print(servoPanPosition);
  68. lcd.print(" ");
  69.  
  70. panServo.write(servoPanPosition);
  71. clawServo.write(servoClawPosition);
  72.  
  73. delay(100);
  74. // adjustable for overall speed
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement