Your sketch is in the loop. I dont want a loop, but I need to act on the button press.
I'll try to simplify as possible and come on next:
#include <Button.h>
#include <Encoder.h>
const int RELAY0 = 6;
const int RELAY1 = 7;
const int BTN_EXTEND = 4;
const int BTN_RETRACT = 5;
const int BTN_MEM_PIN[] = {8,9,10};
Encoder myEnc(2, 3);
long oldPosition = -999;
long targetPosition = 0;
#define ACCURACY 8
#define DEBOUNCE_MS 20
#define PULLUP true
#define INVERT true
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(RELAY0, OUTPUT);
pinMode(RELAY1, OUTPUT);
analogWrite(RELAY0, 0);
analogWrite(RELAY1, 0);
Serial.begin(9600);
}
void loop() {
memPosition[0] = 100;
memPosition[1] = -100;
memPosition[2] = 0;
btnPos1.read();
btnPos2.read();
btnPos3.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] ;
}
long newPosition = myEnc.read();
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("]");
if(targetPosition < newPosition) {
Serial.println("AUTO RETRACT");
retractActuator();
}
if(targetPosition > newPosition) {
Serial.println("AUTO EXTEND");
extendActuator();
}
if( (targetPosition == newPosition) || abs(targetPosition - newPosition) <= ACCURACY) {
Serial.println("AUTO STOP");
stopActuator();
}
}
}
void extendActuator() {
analogWrite(RELAY0, 50);
analogWrite(RELAY1, 0);
}
void retractActuator() {
analogWrite(RELAY0, 0);
digitalWrite(RELAY1, HIGH);
}
void stopActuator() {
analogWrite(RELAY0, 0);
analogWrite(RELAY1, 0);
delay(15);
}
1.Please help to me what can I do to able to change in "void retractActuator" a digital write to analog becouse if I type in analog it does not wotk that way. Only like in the sketch is work.
A