Creating a Maze with Servos and an Analog Stick

Hey all! Creating a maze moved by 2 servos (1 for each axis) controlled by an analog stick, trying to slow down the movements of the servos so that the maze is easier to control, however when I run my program the maze jut tilts down slowly and stops. Any ideas how I could fix this?

[code]
#include <Servo.h>

#define servoPinX 9
#define servoPinY 11
#define axisX A0
#define axisY A1

Servo myServoX; 
Servo myServoY; 

int homePosX = 90; // starting position for X-axis servo
int homePosY = 90; // starting position for Y-axis servo
int posX = 0;
int posY = 0;

void setup() {
  Serial.begin(9600);
  myServoX.attach(servoPinX); 
  myServoY.attach(servoPinY); 
  posX = homePosX; // update servo with home position at startup
  posY = homePosY;
  myServoX.write(posX);
  myServoY.write(posY);
}

void loop() {
  int xRawVal = analogRead(axisX); 
  int yRawVal = analogRead(axisY); 
  int xVal = map(xRawVal, 0, 1023, 0, 180); 
  int yVal = map(yRawVal, 0, 1023, 0, 180); 

  Serial.print(xVal);
  Serial.print(" - ");
  Serial.print(yVal);

  if (xVal > 0) {
    posX++;
    myServoX.write(posX);
    delay(100);
  }
  if (xVal < 0) {
    posX--;
    myServoX.write(posX);
    delay(100);
  }
  if (yVal > 0) {
    posY++;
    myServoY.write(posY);
    delay(100);
  }
  if (yVal < 0) {
    posY--;
    myServoY.write(posY);
    delay(100);
  }
}

[/code]

What does the printout of xVal and yVal show? Is that as You expect?
What moves did You do with the joystick?

How do You think that xVal or yVal would get negative values? Where in the code?

Have a look at this:

  int xVal = map(xRawVal, 0, 1023, -90, 90);
  int yVal = map(yRawVal, 0, 1023, -90, 90);

Adding in the negative values to the mapping made sense, I added them and the maze still falls one way on startup, it does move when I give inputs with the analog stick but it always starts moving back down once I let go. What I find strange is the x-axis value is never 0 when printed to the serial monitor, it always is 2.

Does the printout reflect the change to -90, 90?

Do some mind gymnastics and see if You can add some more useful printouts.
It's late and I need to prepare (sleep) before tomorrow.

Hi,
What does the monitor show as the servo uncontrollably tilts?

Can you post a circuit diagram of your project please?
How have you got the servos powered?

Thanks.. Tom.. :slight_smile:

Hello,

I did some research online and was able to find a working program, am currently working on understanding it and making changes to better suit my needs. I have attached the circuit, as well as the new code if you are curious. Both servos are powered by the arduino only, however they are very small servos (SG90)

[code]
#include <Servo.h> 
const int ServoXPin = 9;  // X Servo digital pin
const int ServoYPin = 11; // Y Servo digital pin
 
const int JXpin = A0; //Joystick X pwm pin
const int JYpin = A1; //Joystick Y pwm pin
const int JSpin = 7; //Joystick Select button digital pin
 
const int Xdiff = -10; // home position for X servo in relation to 90
const int Ydiff = 5; // home position for Y servo in relation to 90
 
Servo XServo;
Servo YServo;
 
int Xval;
int Yval;
int JSval;
int Xang; // x servo angle
int Yang; // y servo angle
 
void setup() {
  // put your setup code here, to run once:
 
  pinMode(ServoXPin, OUTPUT);
  pinMode(ServoYPin, OUTPUT);
  pinMode(JXpin, INPUT);
  pinMode(JYpin, INPUT);
  pinMode(JSpin, INPUT);
 
  XServo.attach(ServoXPin);
  YServo.attach(ServoYPin);
 
  Xang = 90 + Xdiff; // setting a start place for X angle
  Yang = 90 + Ydiff; // setting a start place for Y angle
  XServo.write(Xang);
  YServo.write(Yang);
 
  digitalWrite(JSpin, HIGH); //Software solution for pullup resistor
 
}
 
void loop() {
  //   put your main code here, to run repeatedly:
 
  Xval = analogRead(JXpin);
  Yval = analogRead(JYpin);
 
  if (Xval < 500) {
    Xang = Xang - 1;
    if (Xang <= 45 + Xdiff) {
      Xang = 45 + Xdiff;
    }
    XServo.write(Xang);
  }
 
  if (Xval > 600) {
    Xang = Xang + 1;
    if (Xang >= 135 + Xdiff) {
      Xang = 135 + Xdiff;
    }
    XServo.write(Xang);
  }
 
  if (Yval < 500) {
    Yang = Yang - 1;
    if (Yang >= (135 + Ydiff)) {
      Yang = (135 + Ydiff);
    }
    YServo.write(Yang);
  }
 
  if (Yval > 600) {
    Yang = Yang + 1;
    if (Yang <= (45 + Ydiff)) {
      Yang = (45 + Ydiff);
    }
    YServo.write(Yang);
  }
 
  JSval = digitalRead(JSpin);
 
 
  if (JSval == 0) {
    if (Xang < 90 + Xdiff) {
      for (Xang; Xang < (91 + Xdiff); Xang ++) {
        XServo.write(Xang);
        delay(50);
      }
    }
    if (Xang > 90 + Xdiff) {
      for (Xang; Xang > (89 + Xdiff); Xang --) {
        XServo.write(Xang);
        delay(50);
      }
    }
    if (Yang < 90 + Ydiff) {
      for (Yang; Yang < (91 + Ydiff); Yang ++) {
        YServo.write(Yang);
        delay(50);
      }
    }
    if (Yang > 90 + Ydiff) {
      for (Yang; Yang > (89 + Ydiff); Yang --) {
        YServo.write(Yang);
        delay(50);
      }
    }
  }
 
  delay(50);
}

[/code]

Screen Shot 2021-01-22 at 10.42.14 PM.png

Screen Shot 2021-01-22 at 10.42.14 PM.png

Hi,
Good that you have got something working.

When you mount the maze on the servo assembly you will definitely need a separate supply for the servos.

Tom... :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.