I have a coding question in Chapter 5, servo motor [solved]

Hi everyone,

I am new to Arduino, and i am following the kit book guidance, and come to Chapter 5, Servo Motor.

I follow the code is okay, but when I try to understand and compare the codes with previous chapter i have below quesion, Could anyone please give guidance to me please.

=======================================================
Original suggested code:
void setup()
{
myservo.attach(9); //9 = pin9
Serial.begin(9600);
}

void loop()
{
........
myservo.write(angle);
delay(15);
}

=======================================================
I don't understand why don't use previous chapter code: "analogWrite"????
Then I try to change as below, the motor moves but with shivering, why???????????

void setup()
{
pinMode(9,OUTPUT);
Serial.begin(9600);
}

void loop()
{
......
analogWrite(9,angle);
delay(15);
}

thx!

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.

totohk:
I don't understand why don't use previous chapter code: "analogWrite"????

analogWrite() can only be used on pins that support hardware PWM. The Servo library allows you to use non-PWM pins so that you can control more servos. I believe the reason they start with analogWrite() is to give you a more basic understanding of how servos work.

totohk:
Then I try to change as below, the motor moves but with shivering, why?

Post your full sketch, using code tags.

Thank you.
I have briefly read the "How to use this forum post." as you mentioned, I will try to follow the rules here. This forum is quite different from my experiences, but I will try to follow, avoid bring inconvenience here.

Thank you. :slight_smile:

pert:
...
Post your full sketch, using code tags.

My codes are as below:

#include <Servo.h>
Servo myservo;

//==================================
int const inputpin = A0;
int inputvalue;
int angle;
//=====================================================================================
void setup() 
{
  pinMode(9,OUTPUT);
  //*********************why i can use this as output??with strange result??
 //myservo.attach(9);
  Serial.begin(9600);
}
//=====================================================================================
void loop() 
{
inputvalue = analogRead(inputpin);
Serial.print("Input pot value: ");
Serial.print(inputvalue);
angle = map(inputvalue,6,1017,10,170);
Serial.print(", angle: ");
Serial.println(angle);

analogWrite(9,angle);
//*********************why i can use this as output??with strange result??
//myservo.write(angle);
delay(15);
}

(deleted)

spycatcher2k:
analogWrite outputs a PWM signal - pulse width modulation
Servos work on a PPM signal - pulse position modulation

They are similar, but different.

No, servos also work on PWM, it's just that the frequency and duty-cycle reailution of analogWrite PWM is unsuitable for driving standard RC servos.
(PPM is only used in the transmitter to receiver multiplexed.stream)