Incorrect Loop for Positioning Linear Actuator

I have the following program that is suppose to move a linear actuator to various positions based on the momentary push of one of two buttons, one for extend and on for retract.
When I download the sketch seems to ignore the whole 'updown' area of the sketch and the actuator keeps moving in and out. If I push the button the 'swcnt' changes but it has no impact

Here's what the Serial monitor shows and the sketch


#define IN_PIN   4 // the Arduino pin connected to the IN1 pin L298N
#define OUT_PIN  5 // the Arduino pin connected to the IN2 pin L298N
#define POTENTIOMETER_PIN   A0 // the Arduino pin connected to the potentiometer of the actuator
int swcnt = 0;
#define button1Pin A1     // the number of the pushbutton1 pin
#define button2Pin A2     // the number of the pushbutton2 pin
int button1State = 0;         // variable for reading the pushbutton status
int button2State = 0;         // variable for reading the pushbutton status
int TargetPosition[7] = {72, 97, 122, 147, 172, 197, 222};
#define TOLERANCE  3 

void setup() {
  Serial.begin(9600);
  pinMode(IN_PIN, OUTPUT);
  pinMode(OUT_PIN, OUTPUT);
  pinMode (button1Pin, INPUT_PULLUP);
  pinMode (button2Pin, INPUT_PULLUP);
}
void updown() {
  Serial.println(swcnt);
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
  if (button1State == LOW && button2State == HIGH) {
    swcnt++;
    if (swcnt > 6 ) {
      (swcnt = 6  );
      Serial.println(swcnt);
    }}
  if (button1State == HIGH && button2State == LOW) {
    swcnt--;
    Serial.println("re ");
    if (swcnt < 0) {
      (swcnt = 0 );
      Serial.println(swcnt);
    }}}
    
void loop() {
  updown();
  delay(3500);
int stroke_pos = analogRead(POTENTIOMETER_PIN);
  //int stroke_pos = potentiometer_value;
  Serial.print("The stroke's position = ");
  Serial.print(stroke_pos);
  Serial.println(" ");
 Serial.print("The targets position = ");
  Serial.print(TargetPosition[swcnt]);
  Serial.println(" ");

  if (stroke_pos < (TargetPosition[swcnt] - TOLERANCE))
    ACTUATOR_extend();
  else if (stroke_pos > (TargetPosition[swcnt] + TOLERANCE))
    ACTUATOR_retract();
  else
    ACTUATOR_stop();
    }
    
  void ACTUATOR_extend() {
  digitalWrite(IN_PIN, LOW);
  digitalWrite(OUT_PIN, HIGH);
  }
  
  void ACTUATOR_retract() {
  digitalWrite(IN_PIN, HIGH);
  digitalWrite(OUT_PIN, LOW);
  }

  void ACTUATOR_stop() {
  digitalWrite(IN_PIN, HIGH);
  digitalWrite(OUT_PIN, HIGH);
  }

How are the switches wired?

Your program reads the switches for less than one microsecond, then, because of the following line, does nothing for 3.5 seconds. So you can't expect the program to be responsive.

delay(3500);

1 Like

Looks like it works to me:

The targets position = 72 
swcnt:0
The stroke's position = 72 
The targets position = 72 
swcnt:0
The stroke's position = 107 
The targets position = 72 
swcnt:0
The stroke's position = 102 
The targets position = 72 
swcnt:0
The stroke's position = 117 
The targets position = 72 
swcnt:0
The stroke's position = 190 
The targets position = 97 
swcnt:1
The stroke's position = 235 
The targets position = 122 
swcnt:2
The stroke's position = 307 
The targets position = 147 
swcnt:3
The stroke's position = 340 
The targets position = 172 
swcnt:4
The stroke's position = 379 
The targets position = 197 
swcnt:5
The stroke's position = 404 
The targets position = 197 
swcnt:5
The stroke's position = 451 
The targets position = 197 
swcnt:5
The stroke's position = 449 
The targets position = 197 
swcnt:5
The stroke's position = 428 
The targets position = 197 
swcnt:5
The stroke's position = 447 
The targets position = 197 
swcnt:5
The stroke's position = 469 
The targets position = 197 
swcnt:5
The stroke's position = 446 
The targets position = 197 
swcnt:5

Of course, because of the delay(3500), you have to be holding the button down at the exact time the delay(3500) expires and the loop() loops.

The switch i wired one leg to A1 and the other to ground. If the delay is not right, should it be moved. If I remove the whole button function and preset the swcnt it works perfect - none of the extend/retract that you see in the serial monitor

It should be removed completely

Change the sketch so that it detects when one or other of the buttons becomes pressed rather than when it is pressed

The StateChangeDetection example in the IDE will show you how to use the detection pf a state change to increment a counter. Use that principle in your sketch

Got it working.. Thanks for pointing me in the right direction

Sounds good !

Glad to have been of help

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.