SERVO and Serial Communication

Hello,

I am new to the world of arduino. My project requires me to use motor for some application. So I am using SG90 servo and I am able to sweep the servo from 0 deg to 180 deg using the sweep code given in the examples. I made small change to the original code which is highlighted below:

#include <Servo.h>

Servo myservo;

int pos=0;

void setup()
{
myservo.attach(9);

Serial.begin(9600);
}

void loop()
{

for(pos=0;pos<180;pos++)
{
myservo.write(pos);
Serial.println(“Hello1”);

delay(10);
}
for(pos=180;pos>=1;pos–)
{
myservo.write(pos);
Serial.println(“Hello2”);

delay(10);
}
}

So as you can see I have added a serial communication just to see if I can receive any messages while the servo is turning. So once the servo starts to rotate I open the “Serial monitor” but I see few “Hello1” s and thats it and I dont get any more "Hello"s.

Can somebody let me know what mistake I am making as it is very crucial for my project?

Thanks

Kind Regards
Nikhil

Which pin did you attach to the servo? Which arduino are you using and how are you powering it up?

I suspect you are sending far too much data through the serial port.

Try increasing the baud rate to 115200 (on the Arduino and the Serial Monitor).

Try extending the delay() to 100msecs which will obviously make the servos move very slowly but may help with diagnosis.

Try reducing the println messages to a single character.

...R

I'm just guessing here but shouldn't this:

 for(pos=180;pos>=1;pos--)

be this:
for(pos=180;pos=0;pos--)

?

raschemmel:
I'm just guessing here but shouldn't this:

 for(pos=180;pos>=1;pos--)

be this:
for(pos=180;po=0;pos--)

?

No, I really don't think so.

  for(pos=180;pos>=1;pos--)

So after the first run through the loop and pos is incremented to 179 , since 179 is greater than 1, why wouldn't it drop out of the
loop when it gets to this ?

  for(pos=179;pos>=1;pos--)

Isn't

 pos>=1

the condition to exit the loop ? (and isn't this condition met when pos=179 ?)
What am I missing here ?

No, his sketch works fine. I have it running on my nano and I also have that exact servo model too. How is the servo being powered?

@Raschemmel
No, it exits the for loop when the condition is false.

What am I missing here ?

At least an "s" and a "="

At least an "s" and a "="

meaning ?

@Raschemmel
No, it exits the for loop when the condition is false.

Oh right, I need some more coffee...

meaning ?

for(pos=180;po=0;pos--)
Meaning you’re missing at least an “s” and a “=”, or a “!”, or a “<” or a “>”
I assumed that was obvious.

OK, let's imagine for a moment that there's a variable called "po" and that you assign it the value zero.
What do you think that will do to your for loop?

I have no idea where all the BS about insults came from.

for(pos=180;po=0;pos–)

The condition must be either pos == 0, pos >= 0, pos <= 0, or pos != 0.

No insults needed, but maybe a few face palms are.

I suggest you print out a message in setup() so it is obvious when the Arduino has reset. For example, this line will print out a line of text telling you what sketch is running:

Serial.println(__FILE__ " " __DATE__ " " __TIME__);

If it does reset while you are moving the servo, the most likely reason is that the servo is overloading the Arduino's power supply.

for(pos=180;po=0;pos–)
The condition must be either pos == 0, pos >= 0, pos <= 0, or pos != 0.

Ok I get it now.
It has to be this:

  for(pos=180;pos>=1;pos--)

so that after the LAST decrement, the zero value will prevent it from re-entering the FOR loop.

Thanks for explaining that.

  for(pos=180;po=0;pos--)

Meaning you’re missing at least an “s” and a “=”, or a “!”, or a “<” or a “>”
I assumed that was obvious.

Now I see what you mean. You can’t have an “=” in the middle term .

@AWOL,
Look at my Reply#3, and look at your Reply#4.
The “s” is there in my post (Reply#3) but missing in yours (#4).

No insults needed, but maybe a few face palms are.

(I slapped myself a couple of times …)

ForLoopIllustrated.png

@AWOL,
Look at my Reply#3, and look at your Reply#4.
The "s" is there in my post (Reply#3) but missing in yours (#4).

Yup, and check my post time and your edit time.
I'm that fast. :wink:

How can you post a comment about my edit 3 minutes BEFORE I finished my edit ? (isn't that cheating ?) :open_mouth:

Guys, I really don't think 13 posts about a FOR loop is any help to the OP.

...R

raschemmel:
How can you post a comment about my edit 3 minutes BEFORE I finished my edit ? (isn't that cheating ?) :open_mouth:

Don't you mean twelve minutes after your post?