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);
}