Bringing this back from the dead. Using IDE 1.0.6.
Finally got around to working on this more. Fortunately I came back and checked the thread and found the post above. So I've massaged that code a little and here's what I have:
//
// AutoLoc Linear Actuator Control Software
//
// AutoLoc linear actuators use a DC motor and simple polarity reversing to extend
// and retract. They also feature a hall effect sensor that sends pulses as it moves.
//
// http://www.carid.com/images/autoloc/vertical-doors/pdf/la24-installation-instructions.pdf
//
// This code taken from:
// http://forum.arduino.cc/index.php?topic=236470.0
// which originally took the code from here:
// http://learn.robotgeek.com/demo-code/123-arduino-linear-actuator-tutorial-preset-position-button-control.html
//
//
const int relay1 = 7; // extend relay output pin connection
const int relay2 = 4; // retract relay output pin connection
const int reedSwitch = 2; // interupt pin connection - See attachInterrupt() docs for your board
int counter = 0;
int counterState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
long lastDebounce0 = 0;
long debounceDelay = 10; // Ignore bounces under 10ms
int CurrentPosition = 0;
int goalPosition = 0;
boolean Extending = false;
boolean Retracting = false;
int incomingByte; // a variable to read incoming serial data into
void actuatorExtend(){
digitalWrite(relay1, HIGH); // Always turn one off and then the other on
digitalWrite(relay2, LOW); // so that there are never two relays on (BIG!)
}
void actuatorRetract(){
digitalWrite(relay2, HIGH); // Always turn one off and then the other on
digitalWrite(relay1, LOW); // so that there are never two relays on (BIG!)
}
void actuatorStop() {
digitalWrite(relay2, HIGH);
digitalWrite(relay1, HIGH);
}
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(reedSwitch, INPUT); // reedSwitch is an input
attachInterrupt(1, trigger0, RISING); // See attachInterrupt() doc for YOUR BOARD
// Leanardo needs "1" for pin 2
// Never see the output below on the Serial Monitor
Serial.println("Repetition counter");
Serial.print("Start");
Serial.print("\t");
Serial.println("End");
// Set both output pins
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
// Initialize both relays to off (make sure whatever relay setup you use defaults
// to OFF since there may be some delay between power-up and this code executing!
digitalWrite(relay2, HIGH);
digitalWrite(relay1, HIGH);
}
void loop() {
if (Extending == true && CurrentPosition > goalPosition ) {
//we have reached our goal, shut the relays off
actuatorStop();
Extending = false;
Serial.println("IDLE");
}
if (Retracting == true && CurrentPosition < goalPosition) {
//we have reached our goal, shut the relay off
actuatorStop();
Retracting = false;
Serial.println("DELI");
}
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
if (incomingByte == 'A') {
goalPosition = 5;
if (goalPosition > CurrentPosition) {
Retracting = false;
Extending = true;
actuatorExtend ();
Serial.println("AExtending");
}
else if (goalPosition < CurrentPosition) {
Retracting = true;
Extending = false;
actuatorRetract();
Serial.println("ARetracting");
}
}
if (incomingByte == 'B') {
goalPosition = 2;
if (CurrentPosition < goalPosition ) {
Retracting = false;
Extending = true;
actuatorExtend ();
Serial.println("BExtending");
}
else if (goalPosition < CurrentPosition) {
Retracting = true;
Extending = false;
actuatorRetract();
Serial.println("BRetracting");
}
}
if (incomingByte == 'S') {
actuatorStop();
Serial.println("Stop");
}
}
}
// Once we start a relay, we should start getting pulses from the sensor as the
// actuator moves. When we get a RISING edge and interrupt will be generated and
// we will jump to the following function.
void trigger0() {
if ( (millis() - lastDebounce0) > debounceDelay && Extending == true) {
counter++;
Serial.print(counter);
Serial.println(" : ");
CurrentPosition = counter;
lastDebounce0 = millis();
}
if ( (millis() - lastDebounce0) > debounceDelay && Retracting == true) {
counter--;
Serial.println(counter);
Serial.print(" : ");
CurrentPosition = counter;
lastDebounce0 = millis();
}
}
This is really just to move it between two spots, 5 pulses from power up and 3 pulses from power up. I grabbed some arbitrary numbers.
It seems to sort of work, but quickly (like after anywhere from 2 to 5 movements) something goes haywire. It's like it gets stuck in an interrupt. Hitting the Reset button on the Leonardo doesn't even fix it. I have to power cycle the board and then it will work a little bit again. Now, the first version of this code was for a linear actuator that was potentiometer controlled (so it didn't need interrupts). Then the poster above modified it to use interrupts and debounce his reed switch (and he ran it on a Mega, so I had to change some interrupt stuff for the Leonardo, but maybe I missed something?). I haven't bothered to try to understand the debounce code fully, but I'm also not sure I need it with a Hall Effect sensor. Thoughts on that part?
Also, anything obvious that would cause this thing to get stuck? It usually will extend and retract a couple times and ultimately an attempt to go one way or the other causes it to just start extending with no limit (and I can't stop it with an 'S' command, I have to remove power).
Anything obvious here?
--Donnie