So I have to generate PWM signal for servo using timer in phase-correct mode. Plus, operating with potentiometer, turn the servo left-right.
I made a realisation of the second part of the task, but I have no idea what to add to my code to fullfil first part of the task.
Here is my code:
#include <Servo.h>
Servo myservo; // create Servo object
int potpin = A0; // analog pin A0 for potentiometer
int val; // value from A0 pin
void setup() {
myservo.attach(5); // servo attached to the pin 5
}
void loop() {
val = analogRead(potpin); // get value from A0 potentiometer (0-1023)
int newval = map(val, 0, 1023, 0, 180); // mapping to (0-180)
myservo.write(newval); // turn the servo according to the mapped value
delay(15); // small delay for servo to spin
}
So i have servo connected to pin 5 and working on 50 Hz and Fosc = 16 Mhz and finally i got wave frequency ~30Hz. Code above is written inside the setup function, but how do I translate the signal to the servo (I guess inside the loop function)?
I cannot use any other timer because teacher's servo is connected to pin5 (PD5) that is controlled by register OCR0B according to the pinout. All I need is to understand how do I send this OCR0B = map(analogRead(A0),0,1023,500,2480); to the servo MG90S (I guess of course). There is nothing in the servo or t/c datasheets.
but how do I translate the signal to the servo (I guess inside the loop function)?
how do I send this OCR0B = map(analogRead(A0),0,1023,500,2480); to the servo MG90S (I guess of course).
Your wiring diagram shows you connecting the pin 5 output to the servo. What happens when you put OCR0B = map(analogRead(A0),0,1023,500,2480); into loop()?
I'd ask my teacher whether I really shall invent a very new framework timing by abusing timer 0 for something else. Something is very strange with your project description.