Joystick Controlled Laser Help ( UNO 2560 R3 STM32 A072)

I am relativly new to Arduino, and for my schools planetarium, I wanted to make a laser mounted on two servos that could point a laser based on joystick inputs, to keep the laser steady, and make more circular movements.

I honestly have zero idea after reading articles how to link a joystick to a servo control. Every attempt I kept getting errors, and I just don't know what to do.

Here is a diagram of the wiring of the board, servos, joystick, and laser.

Here is my "sweeping" code.

#include <Servo.h>
Servo servo1; // y axis
int pos1 = 0;
int pos2 = 0;
Servo servo2; // x axis

void setup() 
{
  for(pos1 = 0; pos1 <180; pos1 += 1)
  {
  servo1.attach(9);
  servo1.write(pos1);
  delay(10);
  }
  for(pos1 = 180; pos1>=1; pos1-=1)
  {
    servo1.write(pos1);
    delay(10);
  }
      for(pos2 = 0; pos2 <180; pos2 += 1)
  {
  servo2.attach(10);
  servo2.write(pos2);
  delay(10);
  }
  for(pos2 = 180; pos2>=1; pos2-=1)
  {
    servo2.write(pos2);
    delay(10);
  }
 
}

void loop()
{  
}

Any help is appreciated.

Thanks,
Jack

The code for the movement should go into loop(), setup() is called only once after a reset.

In loop() you analogRead() the X and Y values from the joystick, and write them to your X and Y servos. Use map() to convert the joystick values to corresponding servo values.

Thanks for the help, but I atually found out it was the USB cable did not give enough juice to power the board with 2 servos.

Thanks though.

Hi,

I honestly have zero idea after reading articles how to link a joystick to a servo control. Every attempt I kept getting errors, and I just don't know what to do.

How does your project work now you have the power sorted out, servos require at least an amp each to work properly, so you will need to give them their own 5V supply.

Looking at your sketch it looks like you are just checking the initialisation of the servos, the servo will only work once then stop, until you reset the UNO.
Your joystick control should be in the void loop() part of your sketch.
The servo library will have examples to help your programming.

Tom..... :slight_smile:

How does your project work now you have the power sorted out, servos require at least an amp each to work properly, so you will need to give them their own 5V supply.

Looking at your sketch it looks like you are just checking the initialisation of the servos, the servo will only work once then stop, until you reset the UNO.
Your joystick control should be in the void loop() part of your sketch.
The servo library will have examples to help your programming.

Tom..... :slight_smile:

I looked around again and followed the tutorials yet again. It wasn't my code at all that was the problem. Yes, that is my "sweep" code, and it was not AS jittery. But they joystick code I wrote had a delay and didn't work 75% of the time, so I plugged in my 2 amp power supply, and bam. Worked right off the bat.

I'm not at my computer right now, but I basically just set variables to position that I mapped with the joystick inputs. First time using joysticks, and one of my first time using servos. But hours of hair tearing and I found out it wasn't my code at all, just a power issue, after I saw the arduino said activity light flicker and power light nearly turn off completely. I went back, and uploaded my first version of code, and it worked. Then I tweaked around with speed, and then it was done.

Thanks for the help.