Skip to content

Instantly share code, notes, and snippets.

@e-Gizmo
Created May 28, 2019 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e-Gizmo/a2d5045bac7cdc939f141f187025a347 to your computer and use it in GitHub Desktop.
Save e-Gizmo/a2d5045bac7cdc939f141f187025a347 to your computer and use it in GitHub Desktop.
This a Sample Sketch for Controlling PanTilt Sg-90 servos with Thumb Joystick, programmed with gizDuino UNO-SE.
#include <Servo.h>
// Servo pins
#define servoApin 5
#define servoBpin 6
// Thumb Joystick pins
#define thumbYpin A5
#define thumbXpin A4
#define thumbBpin A3
//Positions
#define max_X 160
#define max_Y 160
#define min_X 0
#define min_Y 0
#define homeX 90
#define homeY 90
// Servo Definitions
Servo servoA, servoB;
int currentX = homeX, currentY = homeY;
void setup(){
Serial.begin(9600);
//Pin Initializations
servoA.attach(servoApin);
servoB.attach(servoBpin);
//Set to home
servoA.write(homeX);
servoB.write(homeY);
}
void loop(){
//read from joystick
int valueX = map(analogRead(thumbXpin), 0, 1024, -10, 10);
int valueY = map(analogRead(thumbYpin), 0, 1000, -10, 10);
Serial.print(analogRead(thumbYpin));
Serial.print(F(" \t "));
Serial.println(analogRead(thumbXpin));
currentX = currentX + valueX;
currentY = currentY + valueY;
//constrain values
currentX = constrain(currentX, min_X, max_X);
currentY = constrain(currentY, min_Y, max_Y);
//Write to servo
servoA.write(currentX);
servoB.write(currentY);
//go back to home pos
if((digitalRead(thumbBpin)) == LOW){
currentY = homeY;
currentX = homeX;
}
delay(15);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment