Hello Guys
I have a project to build one DC motor in a machine, and need to have 3 pre programmed position. The Shiftautomations sketch is the what is very similar to my project.
With the original sketch you are be able to definied 3 coordinates, and to store them, with button. After you can moove with push buttons in this targets.
What I need to change?
1.To have already definied coordinates in sketch instead of "store" them with button.
2.Before the motor arrive to target to slow down (to have a precise position)
I try to do myself but without sucess.
Please help to me.
Here is the sketch from Shiftautomation:
#include //http://www.pjrc.com/teensy/arduino_libraries/Encoder.zip
#include //GitHub - JChristensen/JC_Button: Arduino library to debounce button switches, detect presses, releases, and long presses
const int RELAY[] = {6,7}; //RELAY[0] and RELAY[1] to access the pins
const int BTN_EXTEND = 4;
const int BTN_RETRACT = 5;
const uint8_t MANUAL = 1; //a constant to indicate manual mode
const uint8_t AUTOMATIC = 2; //a constant to indicate automatic mode
const int BTN_MEM_PIN[] = {8,9,10};
const int BTN_SET_MEM = 11;
//Set up the linear actuator encoder
//On many of the Arduino boards pins 2 and 3 are interrupt pins
// which provide the best performance of the encoder data.
Encoder myEnc(2, 3);
long oldPosition = -999;
long targetPosition = 0;
#define ACCURACY 10 //How close to your target position is close enough. Higher accuracy may result in
// a bit of jitter as the actuator nears the position
#define DEBOUNCE_MS 20 //A debounce time of 20 milliseconds usually works well for tactile button switches.
#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
#define INVERT true //Since the pullup resistor will keep the pin high unless the
//switch is closed, this is negative logic, i.e. a high state
//means the button is NOT pressed. (Assuming a normally open switch.)
uint8_t MODE = MANUAL;
Button btnExtend(BTN_EXTEND, PULLUP, INVERT, DEBOUNCE_MS);
Button btnRetract(BTN_RETRACT, PULLUP, INVERT, DEBOUNCE_MS);
Button btnSetPos(BTN_SET_MEM, 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);
long memPosition[] = {0,0,0};
void setup() {
pinMode(RELAY[0], OUTPUT);
pinMode(RELAY[1], OUTPUT);
Serial.begin(9600);
}
void loop() {
btnExtend.read();
btnRetract.read();
btnSetPos.read();
btnPos1.read();
btnPos2.read();
btnPos3.read();
if (btnExtend.isPressed()) {
extendActuator();
MODE = MANUAL;
}
if (btnRetract.isPressed()) {
retractActuator();
MODE = MANUAL;
}
if (!btnExtend.isPressed() && !btnRetract.isPressed() && MODE == MANUAL) {
stopActuator();
MODE = MANUAL;
}
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];
}
//check the encoder to see if the position has changed
long newPosition = myEnc.read();
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("]");
if(targetPosition < newPosition) {
Serial.println("AUTO RETRACT");
retractActuator();
MODE = AUTOMATIC;
}
if(targetPosition > newPosition) {
Serial.println("AUTO EXTEND");
extendActuator();
MODE = AUTOMATIC;
}
if( (targetPosition == newPosition) || abs(targetPosition - newPosition) <= ACCURACY) {
Serial.println("AUTO STOP");
stopActuator();
MODE = MANUAL;
}
}
if(btnSetPos.isPressed()) {
if(btnPos1.isPressed())
memPosition[0] = newPosition;
if(btnPos2.isPressed())
memPosition[1] = newPosition;
if(btnPos3.isPressed())
memPosition[2] = newPosition;
}
}
void extendActuator() {
//Serial.println("extendActuator");
digitalWrite(RELAY[0], HIGH);
digitalWrite(RELAY[1], LOW);
}
void retractActuator() {
//Serial.println("retractActuator");
digitalWrite(RELAY[0], LOW);
digitalWrite(RELAY[1], HIGH);
}
void stopActuator() {
//Serial.println("stopActuator");
digitalWrite(RELAY[0], HIGH);
digitalWrite(RELAY[1], HIGH);
}