My task is to make this dc motor work like a servo i.e. we give angle to it and it rotates accordingly the command feed to it like a typical servo does.
As soon as you have specific questions to whatever detail you will get helpful answers
The question could even be
"If have googled for ...." and found a lot of links.
Though as I don't know anything about it yet I'm unable to decide which link is worth reading and which not
Can somebody with more experience give some suggestions what sources have a high probability to be helpful
or
can do a quick cross-reading of these links
your link list
and post her/his estimation which links might be helpful?
Have you been able to control the motor?
Have you been able to read the encoder?
In separate small bits of code to make sure you can read and control them, BEFORE trying anymore coding.
You need to develop your code in stages and then you will be able to understand how it works.
Getting a full on all bells and whistles code from someone else will teach you very little and how to debug it.
Can you please tell us your electronics, programming, arduino, hardware experience?
Most RC-Servos work very different than what you described.
A RC-Servo is permanently supplied with power over a plus and Ground-wire
and
a the RC-Servo receives a specialised PWM-signal over a single wire and move the servo-horn according to the pulse-length to a position.
All the control is done inside the RC-Servo
What you are planning to build as DC-Servo-Motor where the motor-controller and encoder are outside of the Motor-housing and all controlling is done by an external microcontroller.
Yes I have tried controlling the motor like a servo using the below code. Also I looked for may such videos but in my case the type of motor didn't matched but yes I tried to understand the idea from it and atleast wrote the code on my own.
My code:-
int a=2;
int b=3;
int k;
int v;
int pp=9;
int d=10;
int pos=0
int angle;
void setup() {
Serial.begin(9600);
pinMode(a,INPUT);
pinMode(b,INPUT);
pinMode(pp,OUTPUT);
pinMode(d,OUTPUT);
}
void loop()
{
k=digitalRead(a);
v=digitalRead(b);
if(k>v)
{
pos++;
Serial.println("clk=");
angle=pos;
Serial.print(pos);
}
else if(v>k)
{
pos--;
Serial.println("anticlk=");
angle=pos;
Serial.print(pos);
}
else
{
pos=0;
Serial.print("no rotation=");
angle=pos;
Serial.print(pos);
}
angle=map(angle,0,8192,0,360);
}
Please help me with this to improve the flow of the code. The problem with our code is that the iteration doesn't even goes inside the first if condition itself. We have tried debugging it but end up with no solution. Please suggest some ideas for controlling such dc motors like a typical standard servo motor.
Then - accidently - You must have missed to mark that lines of code that control the motor.
The lines of code that you have posted has only two lines of code that might be related to controlling the motor.
pinMode(pp, OUTPUT);
pinMode(d, OUTPUT);
But I am unsure because I do not understand what the meaning of "pp" and "d" is.
"pp" and "d" is too short to be understandable.
If you want real help from the arduino-forum you should do these things:
pressing ctrl-T inside the arduino IDE for automatic formatting your code
giving all variables and all self-written functions self-explaining names
writing a code that does nothing more than making the motor run clockwise / counterclockwise and then post this code
The reason might be that you don't drive the motor at all. If both inputs stay on LOW
or
if both inputs stay on HIGH
the conditions
Can you please post a copy of your circuit a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
you shoud run ths simulation to see what your code-logic does so far.
Seeing printed what your code is doing should give you new ideas
what you have to change in your code.
If you hoped for that your strategy can be post a little code to make other users write ready to use code.
It is very unlikely to happen. Yiu have to show own effort and ask specific questions.