Arduino uno 5v analog input and trigger Line Actuator

Good day folks,

I would like to apologize in advance ... I'm new to coding with Arduino.

Here is what I would like to do:

Using a arduino uno, would like to read A0 input for 5v (coming from a USB device), convert the voltage to output and control a line actuator to extend when 5v is detected and retract when 5v is lost at A0 input.

What is working:

I can control the line actuator just fine with buttons to extend and retract the motor.
I can control the line actuator just fine with 5v trigger to extend and retract the motor when 5v is lost.

The Issue I'm having:

After 5v trigger has been activated, the actuator extends to its maximum position, the board continues to send trigger to power to the actuator. The same thing happens in reverse when the 5v trigger is lost, actuator is retracted but power to the actuator is still being send.

The questions:
Can I have both the 5v trigger and buttons working in parallel if desired ? (ex: if 5v trigger is not applied I should still use the button and vice versa)
Can I apply a timer for lets say 30 seconds for the 5v trigger to activate the actuator then cut power to it after the extension/retraction process is complete ?

Hardware Used:
BTS7960
Arduino Uno
DC Motor Polarity Reversing Rocker 3 Position DPDT Momentary Automatic Reset Switch.

byte mspeed=0;
int RPWM = 5;
int LPWM = 6;
int value = analogRead (A0);  // read A0


void setup() {
  // put your setup code here, to run once:
   Serial.begin(9600);
   pinMode(2, INPUT_PULLUP);
   pinMode(3, INPUT_PULLUP);
   pinMode(A0, INPUT);
   pinMode(RPWM,OUTPUT);
   pinMode(LPWM,OUTPUT);
   pinMode(13, OUTPUT); //BUILD IN LED
}

void loop() {
  if( analogRead(A0) > 512 == HIGH & analogRead(A0) < 512 ==LOW ){
    digitalWrite(13, HIGH); //write digital pin 13 high LED
    mspeed = 255;
    analogWrite(RPWM,0);
    analogWrite(LPWM, mspeed);
  }
  else if ( analogRead(A0) > 512 == LOW & analogRead(A0) < 512 ==HIGH ){  
    digitalWrite(13, LOW); //write digital pin 13 low LED
    mspeed = 255;
    analogWrite(RPWM,mspeed);
    analogWrite(LPWM, 0);
  }
  else {
    analogWrite(RPWM,0);
    analogWrite(LPWM, 0);
  }
}

#Thank you very much for your time

I assume you are talking about a linear actuator. It has no position feedback device or even limit switches, I am guessing, so you desire to run it for some time, sufficient time for it to reach an extreme.

This is a simple enough matter.

But first, these expressions are too complicate, though possibly correct:

 if( analogRead(A0) > 512 == HIGH & analogRead(A0) < 512 ==LOW ){

is just

 if (analogRead(A0) > 512) {

If it's greater than 512, it must be not less than 512. Leaving aside for the moment the rare occasion when it is exactly 512.

And you've used HIGH and LOW oddly. Comparison operators result it logical values, true and false would be more commonly seen, but as I have shown, you don't need to compare a logical value to any HIGH or true or whatever.

So it looks like you use 512 (or try) to mean no power.

You need to learn about and employ "state change detection". There is an example in the IDE, see
Examples/02.Digital/StateChangeDetection.

Simply put, you want to recognize when the trigger signal changes from 0 to 5, or vice verse, rather than acting when it is 0 or 5. Only when it transitions do you act.

When it goes from 0 to 5, turn on the motor, delay 30 seconds and turn off the motor. FWD.

When it goes from 5 to 0, turn on the motor, delay 30 seconds and turn off the motor. REV.

Or exactly the opposite.

The example code is a bit cluttered and obscure in its functioning. Read the example in the IDE, and see this article for details:

But here I have stripped it down and made the substitution to reading an analog voltage where a pushbutton would be the, um, logical choice:

const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 3;        // the pin that the LED is attached to

int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  Serial.begin(9600);
}

void loop()
{
// originally, we watched a button for a press
//  buttonState = digitalRead(buttonPin);

// but we are using an analog signal above or below 512 as a button
  buttonState = analogRead(A0) > 512;

  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      Serial.println("I see the value go above 512!");
    }
    else {
      Serial.println("I see the value go below 512!");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
    lastButtonState = buttonState;  // remember state for next comparison
  }
}

HTH

About whether you can preserve the use of the linear actuator buttons or not is another matter. What exact linear actuator are you using, and what are you doing between the actuator and the Arduino that lets the Arduino control the actuator?

If, for example, the Arduino used relays, you could probably wire them in parallel to the actuator control switch. There might be some excitement potential, however, when both you and the Arduino decide to do opposite things at the same time.

So more on that when we know with what you are dealing, and how.

a7

Hello,

You are correct, a linear actuator (model

Between the Arduino and the actuator I'm using the BTS7960 board.

In short what I'm trying to do is to trigger the actuator to extend to its max value for ~30 seconds once a USB port is live on the device monitored and retract the actuator for 30~ once the USB no longer provides power and at the end of each loop to terminate the job and cut power to the actuator.

Would it make more sense to move away from Analog input and switch over to a Optocoupler instead ?

may be worth uding a current sensor to check if the actuator is reaching its limits

There may be many good reasons to use an optoisolator.

But that can be a totally separate issue, as I understand your current state of progress.

Please draw, by hand is fine, a schematic of your project as it is now. Just show all the parts, including how the PC USB port is being watched. Also be sure to show where all power comes from and how it is routed to the parts that need it.

You didn't say what actuator you have. Do say, and provide here any technical data you can, and/or links to where you bought it or manufacturer's data sheet or operating instructions. Whatever, at this point more information is better.

You didn't say the code I wrote or "state change detection" make any sense or seem to fit the requirements for the functionality you seek.

a7

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