Urgent Help Needed. I am using DC Motor with Encoder. The Encoder gives 64counts /rev. I am Using Arduino Mega, SN754410 Driver IC and the Pololu Motor.
I am new to Arduino. I need to program the Arduino In a such a way that it control's the position of the Motor. Say for Example to move the motor for 120 counts.
What Shall I do. I don't get the programming part.
However I have done the encoder.h one where I get the values displayed on the serial monitor.
Kunal04:
However I have done the encoder.h one where I get the values displayed on the serial monitor.
If you can get the values already, then you can now use them to control your motor(s). Start the motors, check for the desired position, when the position is reached, stop the motor.
Or more sophisticated, compute the error (difference between desired and measured position), and use that
(via a PID loop perhaps) to set the motor direction and PWM level. This is closed loop position control.
I shall get something going up running in some days time. I will ask questions then by posting up my program. However One question. Can I overwrite the arduino library in my code.?
Please put your code in its own window as seen in other posts. This can be done by placing [code] and [/code] around the code or use the </> icon. This makes it easier for others to read.
Okay I have now got the coding for the encoder Read, Now my next task is to run the motor in one direction and lets say count to 1200 counts. I am using Quadrature encoder.
Here is code, Help Needed.
int encoder0PinA = 2;
int encoder0PinB = 3;
volatile int encoder0Pos = 0;
volatile int encoder0PinALast = LOW;
volatile int n = LOW;
int valNew = 0;
int valOld = 0;
volatile int m = LOW;
void setup()
{
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);
attachInterrupt(1, CountA, CHANGE);
attachInterrupt(0, StateB, FALLING);
}
void loop()
{
encoder0PinALast = n;
valNew = encoder0Pos;
if (valNew != valOld) {
Serial.print (encoder0Pos, DEC);
//Serial.print (m);
Serial.print ("/");
valOld = valNew;
}
}
void CountA()
{
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (m == LOW) {
encoder0Pos--;
}
else {
encoder0Pos++;
}
}
}
void StateB()
{
m = digitalRead(encoder0PinB);
}