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);
}
}
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.
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);
}