Homing the DC motor

Hello Everyone

In my machine project I use one DC motor controlled axe. The code wich I use is working stable, and good except one thing. Sometimes I need to homing the axe position. The code which I use is from shiftautomation and change a little for my needings.

Please help to me to add in a sketch a homing with one push button. Its also need to zero the counter, after the homing. The homing can be done with some normal mechanical switch. Somebody say it a simple job, but I am not so skilled in arduino.

Here is the sketch:

#include <Button.h> 
#include  <Encoder.h>

const int PWM0 =  6;
const int PWM1 =  5;
const int BTN_MEM_PIN[] = {A0, A1, A2, A3};
const int READY =  13;
int currentPos = 0;
int newPosition = 0;
int runningSpeed = 0;
long distanceTogo = 0;


Encoder myEnc(2, 3);
long oldPosition  = -999;
long targetPosition = 0;
#define ACCURACY 10

#define DEBOUNCE_MS 20
#define PULLUP true
#define INVERT true


#define motorSpeed 255
#define motorSpeed1 40
#define motorSpeed2 30


Button btnPos1(BTN_MEM_PIN[0], PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos2(BTN_MEM_PIN[1], PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos3(BTN_MEM_PIN[2], PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos4(BTN_MEM_PIN[3], PULLUP, INVERT, DEBOUNCE_MS);



long memPosition[] = {0, 0, 0, 0};


void setup() {
  pinMode(PWM0, OUTPUT);
  pinMode(PWM1, OUTPUT);
  analogWrite(PWM0, 0);
  analogWrite(PWM1, 0);
  pinMode(READY, OUTPUT);



  Serial.begin(9600);
}

void loop() {

  memPosition[0] = 0;
  memPosition[1] = 1000;
  memPosition[2] = 2000;
  memPosition[3] = 3000;


  btnPos1.read();
  btnPos2.read();
  btnPos3.read();
  btnPos4.read();



  if (btnPos1.wasReleased()) {
    Serial.println("btnPos1");
    targetPosition = memPosition[0] ;

  }
  if (btnPos2.wasReleased()) {
    Serial.println("btnPos2");
    targetPosition = memPosition[1] ;
  }
  if (btnPos3.wasReleased()) {
    Serial.println("btnPos3");
    targetPosition = memPosition[2] ;
  }
  if (btnPos4.wasReleased()) {
    Serial.println("btnPos4");
    targetPosition = memPosition[3] ;
  }

  long newPosition = myEnc.read();
  distanceTogo = (abs(targetPosition - newPosition));

  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }

  if ( newPosition != targetPosition) {

    Serial.print("Target/Actual:"); Serial.print(targetPosition); Serial.print(" / "); Serial.print(newPosition); Serial.print(" ["); Serial.print(abs(targetPosition - newPosition)); Serial.println("]");
    Serial.println(distanceTogo);

    if (targetPosition < newPosition) {
      retractActuator();

    }
    if (targetPosition > newPosition) {
      extendActuator();
    }
    if ( (targetPosition == newPosition) || abs(targetPosition - newPosition) <= ACCURACY) {
      stopActuator();
    }

    if (distanceTogo <= 300 ) {
      runningSpeed = motorSpeed2;
    }

    if (301 <= distanceTogo && distanceTogo <= 500)  {
      runningSpeed = motorSpeed1;
    }
    if (distanceTogo >= 501) {
      runningSpeed = motorSpeed;
    }
  }


}


void retractActuator() {
  analogWrite(PWM0, 0);
  analogWrite(PWM1, runningSpeed);
  digitalWrite(READY, LOW);

}

void extendActuator() {
  analogWrite(PWM0, runningSpeed);
  analogWrite(PWM1, 0);
  digitalWrite(READY, LOW);
}



void stopActuator() {

  analogWrite(PWM0, 0);
  analogWrite(PWM1, 0);
  digitalWrite(READY, HIGH);



}

What is a "DC motor controlled axe"?

Steve

Hello

I think it is not so important in this project, but for you I will explain.

It is one positioning axes for drilling a holes. When I hit a button the axes will move to position counted by encoder. In this sketch I have 4 buttons, so in 4 positions be able to move.

I need a homing to be shure that the 0 position is always a same.

Sorry for my bad English

Most people use a limit switch to detect "home".

Yes I have a limit switch. The only left thing is to implement the homing in the sketch. Did you help to me?

The only left thing is to implement the homing in the sketch.

Not really. You also have to properly connect the limit switch to the Arduino, using a pullup or pulldown resistor.

To connect properly the limit switch is not a problem to me. I understand and use a pull up and down in other projects too. More interesting is the sketch. Delta said "while loop"? How can I start? Where to put? Before or after existing loop? Of course I dont want to somebody fix instead of me, but some help in start would be great.

Brinarpi:
It is one positioning axes for drilling a holes

The singular of "axes" is "axis". An axe (also hatchet) is a tool for chopping down trees. :slight_smile:

...R

Brinarpi:
To connect properly the limit switch is not a problem to me. I understand and use a pull up and down in other projects too. More interesting is the sketch. Delta said "while loop"? How can I start? Where to put? Before or after existing loop? Of course I dont want to somebody fix instead of me, but some help in start would be great.

When I look through the code in your Original Post I cannot relate it to your question.

You have several buttons in your program - do you want one of those to cause the DC motor to home or do you want to add an extra button for that purpose?

It would also help if you can describe how the existing program is used so that we can have a good mental image of your project. A diagram may be a big help, or maybe even a short YouTube video.

...R

Hello Robin

Its long time, that we dont speak. You are the only one, who has a good ideas to go further with projects.
Once again, sorry for my english, I'll try my best all the time, and I learn every day something.

In a meantime I solve this problem by myself.
Here is the sketch wich is work, and almost finish.

#include <Button.h>
#include  <Encoder.h>

const int PWM0 =  6;
const int PWM1 =  5;

const int BTN_EXTEND =  10;
const int BTN_RETRACT = 11;
const int HOMING = 12;

const uint8_t MANUAL = 1;
const uint8_t AUTOMATIC = 2;


const int BTN_MEM_PIN[] = {A0, A1, A2, A3};
const int READY =  13;
int currentPos = 0;
int newPosition = 0;
int runningSpeed = 0;
long distanceTogo = 0;


Encoder myEnc(2, 3);
long oldPosition  = -999;
long targetPosition = 0;
#define ACCURACY 10

#define DEBOUNCE_MS 20
#define PULLUP true
#define INVERT true


#define motorSpeed 255
#define motorSpeed1 40
#define motorSpeed2 30
#define motorSpeedhoming 100

uint8_t MODE = MANUAL;

Button btnExtend(BTN_EXTEND, PULLUP, INVERT, DEBOUNCE_MS);
Button btnRetract(BTN_RETRACT, PULLUP, INVERT, DEBOUNCE_MS);
Button btnHoming(HOMING, PULLUP, INVERT, DEBOUNCE_MS);

Button btnPos1(BTN_MEM_PIN[0], PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos2(BTN_MEM_PIN[1], PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos3(BTN_MEM_PIN[2], PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos4(BTN_MEM_PIN[3], PULLUP, INVERT, DEBOUNCE_MS);



long memPosition[] = {0, 0, 0, 0};


void setup() {
  pinMode(PWM0, OUTPUT);
  pinMode(PWM1, OUTPUT);
  analogWrite(PWM0, 0);
  analogWrite(PWM1, 0);
  pinMode(READY, OUTPUT);





  Serial.begin(9600);
}

void loop() {

  memPosition[0] = 150;
  memPosition[1] = 3000;
  memPosition[2] = 2000;
  memPosition[3] = 100;

  btnExtend.read(); //XXXX
  btnRetract.read(); //XXXX
  btnHoming.read();

  btnPos1.read();
  btnPos2.read();
  btnPos3.read();
  btnPos4.read();

  if (btnExtend.wasReleased()) {
    extendActuator();
    runningSpeed = motorSpeedhoming;
    MODE = MANUAL;
  }

  if (btnRetract.wasReleased()) {
    retractActuator();
    runningSpeed = motorSpeedhoming;
    MODE = MANUAL;
  }



  if (btnHoming.isPressed()) {
    myEnc.write(0);
    targetPosition = memPosition[3] ;
    MODE = AUTOMATIC ;
  }



  if (btnPos1.wasReleased()) {
    Serial.println("btnPos1");
    MODE = AUTOMATIC;
    targetPosition = memPosition[0] ;

  }
  if (btnPos2.wasReleased()) {
    Serial.println("btnPos2");
    MODE = AUTOMATIC;
    targetPosition = memPosition[1] ;
  }
  if (btnPos3.wasReleased()) {
    Serial.println("btnPos3");
    MODE = AUTOMATIC;
    targetPosition = memPosition[2] ;
  }
  if (btnPos4.wasReleased()) {
    Serial.println("btnPos4");
    MODE = AUTOMATIC;
    targetPosition = memPosition[3] ;
  }

  long newPosition = myEnc.read();
  distanceTogo = (abs(targetPosition - newPosition));

  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }

  if ( MODE == AUTOMATIC && newPosition != targetPosition) {

    Serial.print("Target/Actual:"); Serial.print(targetPosition); Serial.print(" / "); Serial.print(newPosition); Serial.print(" ["); Serial.print(abs(targetPosition - newPosition)); Serial.println("]");
    Serial.println(distanceTogo);

    if (targetPosition < newPosition) {
      retractActuator();
      MODE = AUTOMATIC;

    }
    if (targetPosition > newPosition) {
      extendActuator();
      MODE = AUTOMATIC;
    }
    if ( (targetPosition == newPosition) || abs(targetPosition - newPosition) <= ACCURACY) {
      stopActuator();
      MODE = AUTOMATIC;
    }

    if (distanceTogo <= 300 ) {
      runningSpeed = motorSpeed2;
    }

    if (301 <= distanceTogo && distanceTogo <= 500)  {
      runningSpeed = motorSpeed1;
    }
    if (distanceTogo >= 501) {
      runningSpeed = motorSpeed;
    }
  }


}


void retractActuator() {
  analogWrite(PWM0, 0);
  analogWrite(PWM1, runningSpeed);
  digitalWrite(READY, LOW);

}

void extendActuator() {
  analogWrite(PWM0, runningSpeed);
  analogWrite(PWM1, 0);
  digitalWrite(READY, LOW);
}



void stopActuator() {

  analogWrite(PWM0, 0);
  analogWrite(PWM1, 0);
  digitalWrite(READY, HIGH);



}