PQ12-R Linear Actuator : Arduino Help!

Hi!

I would appreciate some advice for a problem I’m encountering.

I purchased a small linear actuator ( PQ12-R by Actuonix) from Robotshop:

For some reason, I am unable to control it in the fashion I would like. I am able to position it at its max and min, and anywhere in between, but I am unable to make the actuator move from its fully retracted position to its fully extended position, and then back to its fully retracted position.

Here is my current arduino code:

//INCLUDES
#include <Servo.h>

//DEFINES
#define LINEARACTUATORPIN 9

Servo LINEARACTUATOR;

int linearValue;

//SETUP
void setup() {

LINEARACTUATOR.attach(LINEARACTUATORPIN, 2000, 1000);

}

void loop() {
//
for (linearValue = 2000; linearValue = 1000; linearValue -= 1) {
LINEARACTUATOR.writeMicroseconds(linearValue);
delay(30);
}
for (linearValue = 1000; linearValue = 2000; linearValue += 1) {
LINEARACTUATOR.writeMicroseconds(linearValue);
delay(30);
}
}

When I run it it only accomplishes the first for loop, and ends their, in its fully extended position. It does not accomplish the second for loop, which was intended to return it back into its retracted position.

Is there something I’m not seeing here?

If help is possible, It would be greatly appreciated.

Hardware concerned:
PQ12-R by Actuonix
Software concerned:
Arduino

Thank you so much in advance!

Your for loops are wrong:

In a for loop, the first parameter (linearValue = 2000) sets the starting point. In this case it sets linearValue to 2000. The third parameter (linearValue -= 1) tells it what to do to the variable after every loop. The second parameter (linearValue = 1000) tells it what condition to check for to go through the loop again. If the value of that parameter is non-zero it goes through the loop again. Read it like this: "loop as long as linearValue = 1000 is not zero). However, the “=” operator in C and C++ is an ASSIGNMENT. Just as in your first parameter, it is setting the value of linearValue, not comparing it. The result of an assignment (linearValue = 1000) is the value assigned: in this case 1000. 1000 is non-zero, so instead of comparing the linearValue to 1000 and returning a 0 or non-zero value if it is equal or not, it is returning the 1000. The third parameter will change it to 999 each time, but then it gets reset to 1000 again, over and over. this loop will never end. The second for loop works similar.
What you need to do is replace the “linearValue = 1000” in the first for loop with “linearValue >= 1000” to compare it: “as long as linearValue is greater than or equal to 1000”. Similarly, the second for loop should read “linearValue <= 2000”

Thank you very much, this resolved the issue.

Great!
You are welcome.