Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo panServo; Servo tiltServo; Servo fireServo; // create servo object to control a servo
- int tiltSpeed = 3; int panSpeed = 3;
- int tiltPin = 10; int panPin = 9; int firePin = 8;
- int tiltAngle; int panAngle; int fireAngle; //119~150
- int command;
- void setup() {
- Serial.begin(9600);
- panServo.attach(panPin);
- tiltServo.attach(tiltPin);
- fireServo.attach(firePin);
- tiltAngle=5; panAngle=5; fireAngle = 133;
- fireServo.write(fireAngle);
- }
- void loop() {
- command = Serial.read();
- if (command == 0){
- moveUp();
- }
- if (command == 1){
- moveDown();
- }
- if (command == 2){
- moveLeft();
- }
- if (command == 3){
- moveRight();
- }
- if (command == 4){
- shootGun();
- }
- tiltServo.write(tiltAngle);
- panServo.write(panAngle);
- }
- void moveUp(){
- if (tiltAngle < 180)
- tiltAngle+=tiltSpeed;
- }
- void moveDown(){
- if (tiltAngle > 0)
- tiltAngle-=tiltSpeed;
- }
- void moveLeft(){
- if (panAngle < 180)
- panAngle+=panSpeed;
- }
- void moveRight(){
- if (panAngle > 0)
- panAngle-=panSpeed;
- }
- void shootGun(){
- fireServo.write(150);
- delay(500);
- fireServo.write(133);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement