Issue with Servos

Hi,
I am fairly new into this, so i decided to try a simple project.
I tried to write a code to control 2 servo motors by writing values into monitor, and it worked for first time, but now when i set it up, the motors just start twitching and they don’t respond to my commands…
Wiring has been done propery, so it can’t be the issue

This is the code i used;

#include <Servo.h>
Servo servo1;
int val1;
Servo servo2;
int val2;

void setup(){
Serial.begin(9600);
servo1.attach (6);
servo2.attach (9);
}

void loop (){
while (Serial.available()>0){
val1 = Serial.parseInt();
val2 = Serial.parseInt();
if (Serial.available()){
servo1.write(val1);
servo2.write(val2);
delay(15);
}
}
}

The code compiles nicely so i cant see where is the problem…

(Board is mega 2560 if it means anything)

The code compiles nicely so i cant see where is the problem...

Just because code compiles it dosn't mean it will do what you want, it just means it will obey the commands you have written.

So the line:-
while (Serial.available()>0){
Will read at least 5 bytes from the serial buffer when the buffer contains one or more bytes. It will repeat this process over and over until there is nothing left in the buffer.

Then it will only take action on those bytes if there is still data left in the serial buffer, which there will not be because you emptied the buffer on the first part of the code. If you ever got into the second while loop it would not get out of it because there is nothing in the while loop that does anything to reduce the number of bytes in the buffer.

Is this what you actually wanted to do?

Lell:
Wiring has been done propery, so it can't be the issue

I am glad that you have confidence in your wiring. Help us gain that same confidence.
Make a hand drawn wiring diagram. Pay attention to how the servos are powered.
Take a picture of that diagram.
Post that picture here.

vinceherman:
I am glad that you have confidence in your wiring. Help us gain that same confidence.
Make a hand drawn wiring diagram. Pay attention to how the servos are powered.
Take a picture of that diagram.
Post that picture here.

Motors are wired into the breadboard powershield, and signal inputs go straight into pins 6 & 9.... There are no other components in the system, and if i swap around the wires, everything is completely lifeless, so it can't be that

Grumpy_Mike:
Just because code compiles it dosn't mean it will do what you want, it just means it will obey the commands you have written.

So the line:-
while (Serial.available()>0){
Will read at least 5 bytes from the serial buffer when the buffer contains one or more bytes. It will repeat this process over and over until there is nothing left in the buffer.

Then it will only take action on those bytes if there is still data left in the serial buffer, which there will not be because you emptied the buffer on the first part of the code. If you ever got into the second while loop it would not get out of it because there is nothing in the while loop that does anything to reduce the number of bytes in the buffer.

Is this what you actually wanted to do?

Maybe, possibly?
I just wanted for servos to take position sent in from monitor, so i worked from similar project that used LEDs instead and modified it to accept servos instead. In the original code, this line was present, so i put it in

Lell:
Motors are wired into the breadboard powershield, and signal inputs go straight into pins 6 & 9.... There are no other components in the system, and if i swap around the wires, everything is completely lifeless, so it can't be that

But since power problems represent the vast majority of problems with servos, it will still be a big step forward if you provide a wiring diagram.
Pencil and paper. Take a picture. Post the picture here.

Servo power connected via breadboard causes a lot of problems. Breadboards are designed for very low currents. LEDs use low currents. Servos don't.

So what sort of servos are these? And how are they powered? If it's via breadboard from the Arduino 5V pin then I'd say your wiring has very definitely NOT been done properly.

Steve

slipstick:
Servo power connected via breadboard causes a lot of problems. Breadboards are designed for very low currents. LEDs use low currents. Servos don't.

So what sort of servos are these? And how are they powered? If it's via breadboard from the Arduino 5V pin then I'd say your wiring has very definitely NOT been done properly.

Steve

They are tiny, very basic servos which did work from board's power before
But now i have a power "shield" (it clips directly on a breadboard and is independent from the board itself), which uses external battery source and is capable of powering larger DC motors, so power itself shouldn't be problem

So let's see where we are. You have unnamed servos powered via a breadboard using an unnamed "power shield" with some unstated specification and run by some "external battery source" you've told us nothing about. At some point you have swapped some unstated wires round then back.

You refuse to draw a circuit diagram but you're sure that power and wiring can't be problems. Best of luck with that then.

Steve

Lell:
Motors are wired into the breadboard powershield, and signal inputs go straight into pins 6 & 9.... There are no other components in the system, and if i swap around the wires, everything is completely lifeless, so it can't be that

Maybe, possibly?
I just wanted for servos to take position sent in from monitor, so i worked from similar project that used LEDs instead and modified it to accept servos instead. In the original code, this line was present, so i put it in

OK so everything is fine, you have done everything correctly, you have not possibly made any mistakes, so what is your problem?

Oh it doesn't work any more? Then it is YOU that have done something wrong is it not? The first step in fault finding is to ditch the ego and admit it to yourself.

The only way we can help is if we know exactly what you have. We can't look over your shoulder and say "that wire is missing or wrong". YOU have to tell us what you have done so we can try to spot what YOU have done wrong. If you are not prepared to cooperate here then I recommend you search for an Arduino forum that is run by psychics.