Failure to run Setup() loop

I’m using an Uno to run a simple pan/tilt script, but while it reads the joystick just fine, the servo motors aren’t running at all, even with a simple timed rotation. (I have continuous servos so I’m not worried about hitting mechanical limits.) The only solution I can seem to find is that the setup loop never runs, so the servos are never initialized. I’m unable to initialize the servos within the main loop itself.

Please take a look at my script below and see if I’m missing anything!

// Used with kind permission from http://learn.explorelabs.com/
// Creative Commons 4.0 Share Alike (CC by SA 4.0) license
#include <Servo.h>

Servo tilt;
Servo pan; // Create servo object
int joyX = A0; // Analog pin connected to x-axis servo
int joyY = A1; // Analog pin connected to y-axis servo
int x, y; // Variables to read values

void setup() {
Serial.println(“setup start”);
tilt.attach(9); // Attach tilt servo on pin 9 to the servo object
Serial.println(“servo 1”);
pan.attach(10); // Attach pan servo on pin 10 to the servo object
tilt.write(20);
pan.write(20);
Serial.println(“setup done”);
delay(200);
}

void loop() {
Serial.begin(9600);
tilt.attach(9);
pan.attach(10);
x = joyX; // Read value of x-axis (between 0 and 1023)
y = joyY; // Read value of y-axis (between 0 and 1023)
x = map(analogRead(joyX), 0, 1023, 0, 100); // Scale it to use
Serial.print(x);
Serial.print(" x value ");
delay(250);

y = map(analogRead(joyY), 0, 1023, 0, 100);
Serial.print(y);
Serial.println(" y value");
delay(250);
tilt.write(x); // Set servo position according to scaled value
pan.write(y);
delay(150); // Wait for servos to get to new position
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:

[code]

[color=blue]// your code is here[/color]

[/code]

Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

jhoog:
The only solution I can seem to find is that the setup loop never runs, so the servos are never initialized.

First, add the missing Serial.begin() statement to the start of your setup(), then run the sketch with the Serial Monitor open. That will prove to you that setup() is indeed running.

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

You only need to attach in the setup once, not continuously in the loop.

Thanks.. Tom.. :)

As pert wrote “First, add the missing Serial.begin() statement to the start of your setup(), then run the sketch with the Serial Monitor open. That will prove to you that setup() is indeed running.”

Then you should remove Serial.begin() from loop(). It is unnecessary there and uses valuable space.

x = joyX; // Read value of x-axis (between 0 and 1023)
y = joyY; // Read value of y-axis (between 0 and 1023)

Do these lines of code do what the comments say ?

Shouldn't you be actually reading the inputs ?

In case its not obvious, servo.attach() is to be called once for each servo object at setup time, not repeatedly in loop()...

Occasionally you might want to use detach() method and then its valid to re-attach at a later point.

Its wise to use a faster baudrate than 9600 for debug messages, otherwise your debugging will likely slow down and alter your program behaviour... 115200 is supported, use it!