Arduino and Breadboard, Servos breaking themselves

54 Views Asked by At

I'm using an Arduino Uno and breadboard for the first time and building my first actual robot. I have 2 servos controlling the x and y axis and attached a laser pointer to the end of it. My problem is not setting up the Arduino or anything, it's the getting the servos to move to a certain spot (wherever the mouse is) and then stopping until the mouse moves. I am coding this in Python and will gladly put in the code I am trying to use. (And the Arduino code) If you see anything wrong then please correct me. Thank you. Also the code isn't mine and I just found a cool video that had links to the code but when it get's to the point (wherever the mouse is) it won't stop and instead continue twisting up the wires that are connecting everything and supplying power.

#include<Servo.h>

Servo servoVer; //Vertical Servo
Servo servoHor; //Horizontal Servo

int x;
int y;

int prevX;
int prevY;

void setup()
{
  Serial.begin(9600);
  servoVer.attach(10); //Attach Vertical Servo to Pin 5
  servoHor.attach(11); //Attach Horizontal Servo to Pin 6
  servoVer.write(90);
  servoHor.write(90);
}

void Pos()
{
  if (prevX != x || prevY != y)
{
    prevX.update;
    prevY.update;

  }
}

void loop()
{
  if(Serial.available() > 0)
  {
    if(Serial.read() == 'X')
    {
      x = Serial.parseInt();
      if(Serial.read() == 'Y')
      {
        y = Serial.parseInt();
      Pos();
      }
    }
    while(Serial.available() > 0)
    {
      Serial.read();
    }
  }
}

Arduino code ^^

import pygame as game
import time
import serial
import sys
game.init()
servo_comms = serial.Serial('COM5', 9600) 
game.mouse.set_visible(True)
display = game.display.set_mode((180,180))
FPS = 60
watch = game.time.Clock()
black = (0,0,0)

def coordinate_graph(x,y):
   display.fill((255,225,225))
    game.draw.rect(display, (0,225,0), [x + 1, y ,5,2])
    game.draw.rect(display, (0,225,0), [x - 2, y ,5,2])
    game.draw.rect(display, (0,225,0), [x, y + 1 ,2,5])
    game.draw.rect(display, (0,225,0), [x ,y - 2 ,2,5])
    watch.tick(FPS)

def starter():
    while True:
        (x, y) = game.mouse.get_pos()
        x = int(x)
        y = int(y)
        data = ("X{0:.0f}Y{1:.0f}Z".format(x, y))
        servo_comms.write(data.encode())
        for event in game.event.get():
            if event.type == game.quit:
                sys.exit(0)
        font = game.font.SysFont('Ariel', 24)
        img = font.render(data.encode(), True, black)
        display.blit(img, (20, 20))
        game.display.update()
        coords(x, y)

starter()

Python code ^^

If you have any ideas on how to make the motors stop when they get to the coordinates on the 180 x 180 page, please help me. Thank you.

0

There are 0 best solutions below