Motor Link : Motor Link
My code :
#include <EEPROM.h>
#include <IRremote.h>
//MOTOR CONSTANTS
#define encoder0PinA 2
#define motorPinA 3
#define motorPinB 4
#define STOP 0
#define FORWARD 1
#define REVERSE 2
#define RANGE_END 2000
#define RANGE_START 1500
#define RANGE_TOLERANCE 0
#define STEP_SIZE 50
//IR CONSTANTS
#define IR_CLOSE_CODE 28815
#define IR_OPEN_CODE 45135
#define IR_STEP_REVERSE_CODE 61455
unsigned int val = 0;
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
volatile int encoderDirection = STOP;
volatile int motorDirection = STOP;
volatile int encoderState, lastEncoderState;
volatile unsigned int counter = 0;
volatile boolean IsStepReverseActive = false;
volatile unsigned int targetCounter = RANGE_START;
void setup() {
irrecv.enableIRIn();
pinMode(encoder0PinA, INPUT);
pinMode(motorPinA, OUTPUT);
pinMode(motorPinB, OUTPUT);
attachInterrupt(0, doEncoderA, CHANGE);
encoderState = digitalRead(encoder0PinA);
lastEncoderState = encoderState;
Serial.begin (9600);
Serial.print("Counter value from EEPROM : ");
counter = EEPROMReadInt(0);
Serial.println(counter, DEC);
delay(100);
motorForward();
}
void loop() {
if (irrecv.decode(&results)) {
val = results.value;
if (val == IR_CLOSE_CODE) {
Serial.println("IR CLOSE"); motorReverse();
} else if (val == IR_OPEN_CODE) {
Serial.println("IR OPEN"); motorForward();
} else if (val == IR_STEP_REVERSE_CODE) {
Serial.println("IR STEP REVERSE");
if ((counter - STEP_SIZE) >= RANGE_START)
{
IsStepReverseActive = true;
targetCounter = counter - STEP_SIZE;
motorReverse();
}
}
irrecv.resume(); // Receive the next value
}
}
void doEncoderA() {
encoderState = digitalRead(encoder0PinA);
if (lastEncoderState != encoderState) {
lastEncoderState = encoderState;
//*****************************************************
if (encoderDirection == FORWARD) {
counter++;
}
else if (encoderDirection == REVERSE)
{
counter--;
}
//*****************************************************
if (motorDirection == FORWARD && counter >= RANGE_END)
{
motorStop();
}
else if (motorDirection == REVERSE && counter <= RANGE_START)
{
motorStop();
}
else if(IsStepReverseActive && counter <= targetCounter)
{
motorStop();
}
else
{
EEPROMWriteInt(0, counter);
}
Serial.println(counter, DEC);
}
}
void motorForward() {
if (counter < RANGE_END)
{
digitalWrite(motorPinA, HIGH);
digitalWrite(motorPinB, LOW);
motorDirection = FORWARD;
setEncoderDirection(FORWARD);
}
}
void motorReverse() {
if (counter > RANGE_START)
{
digitalWrite(motorPinA, LOW);
digitalWrite(motorPinB, HIGH);
motorDirection = REVERSE;
setEncoderDirection(REVERSE);
}
}
void motorStop() {
if (motorDirection != STOP) {
digitalWrite(motorPinA, LOW);
digitalWrite(motorPinB, LOW);
motorDirection = STOP;
IsStepReverseActive = false;
EEPROMWriteInt(0, counter);
}
}
void EEPROMWriteInt(int p_address, int p_value)
{
byte lowByte = ((p_value >> 0) & 0xFF);
byte highByte = ((p_value >> 8) & 0xFF);
EEPROM.write(p_address, lowByte);
EEPROM.write(p_address + 1, highByte);
}
unsigned int EEPROMReadInt(int p_address)
{
byte lowByte = EEPROM.read(p_address);
byte highByte = EEPROM.read(p_address + 1);
return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
}
void setEncoderDirection(int d) {
if (d == FORWARD) {
encoderDirection = FORWARD;
} else if (d == REVERSE) {
encoderDirection = REVERSE;
}
}
Info:
500RPM (N20 15000RPM, reduction ratio 30: 1)
210 pulses per revolution = Motor Tail Hall encoder (7CPR) * 30 (reduction ratio)
....
I want to move between 1500 - 2000 values, it does but position is not stable .. It always going more and more closer to one side slowly.