ljhalawani
Published

Two mode Servo Control (Direct and Precise)

Make precise servo control by incrementation, or directly reflect analog joystick position.

IntermediateShowcase (no instructions)500
Two mode Servo Control (Direct and Precise)

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Analog joystick (Generic)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Connection diagram

How to hook everything up

Code

The Code

C/C++
This code is made on Arduino IDE and uses Servo.h library included with the Arduino IDE package.
//libraries
#include <Servo.h>

//GLOBAL  
//servo pin
const int servoPin = 2;
//creating servo motor object
Servo sMot1; 
//saved servo postition
float sSPos;

//analog joistick pins
const int xPin = A6;
const int yPin = A7;
const int clickPin = 3;
//values store for joystick
int xVal, yVal, clickVal;

//built in LED pin
const int ledPin = 13;

//read delay (ms)
int dt = 0;

//current control mode
int currentState = 1;

//GLOBAL FUNCTIONS
//set servo in position
void sPos(int dg) {
  sMot1.write(dg);
  //delay(1000);
}
//MODE CHANGER
//Switches to precise or direct mode whenever the analog joystic button is clicked
void changeMode(){
if (currentState == 1 && clickVal == 0) {
  currentState = 0;
  delay(500);
} else if (currentState == 0 && clickVal == 0) {
  currentState = 1;
  delay(500);
}
}

void setup() {
  
//configure serial port transmission rate
Serial.begin(9600);
//set the servo library object to the right pin
sMot1.attach(servoPin);
//sets the servo position in the middle
sSPos = 85; 

//configure joystick pins
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(clickPin, INPUT);
digitalWrite(clickPin, HIGH);
}


void loop() {
  xVal = analogRead(xPin);
  yVal = analogRead(yPin);
  clickVal = digitalRead(clickPin);
  //delay(dt);
  //DEBUG
  //Serial.println("xVal: "+String(xVal)+" | yVal: "+String(yVal)+" | clickPin state: "+String(clickVal));
  //Serial.println("current operation mode: "+String(currentState));
  
  changeMode();
  if (currentState == 0){
      //operate servo direct mode
      //DEBUG
      //Serial.println("DIRECT MODE: Servo position: "+String(sSPos));
      sSPos = (170./1023.)*yVal;
      sPos(sSPos);
  } else {
      //operate servo precise mode
      //DEBUG
      //Serial.println("PRECISE MODE: Servo position: "+String(sSPos));
      //Serial.println("Saved sero position: "+String(sSPos));
      delay(10);
      if (sSPos < 170 && yVal > 524) { 
          sSPos = sSPos + 1; 
      } else if (sSPos > 0 && yVal < 518) {
          sSPos = sSPos - 1;
  }

  sPos(sSPos); 
  }




  //click to blink built in LED, to indicate when the mode was switched
  if(clickVal == 0){
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
}

Credits

ljhalawani
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.