hockey1alex
Published © GPL3+

Laser turret version 2 with joystick

Dual servo control with a laser using a joystick

IntermediateFull instructions provided1,154
Laser turret version 2 with joystick

Things used in this project

Hardware components

SG90 Micro-servo motor
SG90 Micro-servo motor
×2
Jumper wires (generic)
Jumper wires (generic)
×1
Analog joystick (Generic)
×1
Laser Diode, 655 nm
Laser Diode, 655 nm
×1
Breadboard (generic)
Breadboard (generic)
×1
Arduino UNO
Arduino UNO
×1

Story

Read more

Schematics

Circuit diagram

copy the diagram

Code

code

Java
go ahead and copy the code.
#include<Servo.h>
 Servo s1,s2;  
 
void setup() {
  s1.attach(10); 
  s2.attach(11);
  pinMode(12,OUTPUT); 
  pinMode(2,INPUT);   
  digitalWrite(2,HIGH);
  Serial.begin(9600);  
 
  
}
int p1=90,p2=90;   
void loop() 
{
  
  int t1=0,t2=0;  
  int a=analogRead(A0); 
  int b=analogRead(A1);

  
  if(a<=450 or a>=550)
  {
    t1=map(a,0,1023,10,-10);
    p1=change(p1,t1);  
   
  }
  if(b<=450 or b>=550)
  {
    t2=map(b,0,1023,-10,10);
    p2=change(p2,t2);  
  }
   s1.write(p1); 
   s2.write(p2);
  if(digitalRead(2)==LOW)
  digitalWrite(12,HIGH);
  else
  digitalWrite(12,LOW);
  
  
  Serial.print("s1:");
  Serial.print(a);
  Serial.print("\t");
  Serial.print("s2:");
  Serial.print(b);
  Serial.print("\t");
  Serial.print("t1:");
  Serial.print(t1);
  Serial.print("\t");
  Serial.print("t2:");
  Serial.print(t2);
  Serial.print("\t");
  Serial.print("pos1: ");
  Serial.print(p1);
  Serial.print("pos2: ");
  Serial.println(p2);
  delay(70);          
}

int change(int pos,int t)
{
    pos=pos+t;                   
    if (pos>180)               
    pos=180;
    if(pos<0)                  
    pos=0;
    return(pos);              
}

Credits

hockey1alex
1 project • 1 follower
Contact

Comments

Please log in or sign up to comment.