I am currently working with a Geekduino board from robot geek (same import setting as Arduino Diecimila), a push button from RobotGeek, and an actuonics/firgelli L16-R actuator. My goal is to have the actuator extend to a distance x and come back to 0 while the button is being held. I would want it to start after a half a second, and immediately come back when it reaches x. I would then like the program to reset when the button is released. I have added below links to the components, the actuator is in pin 9 and the button pin 7. I have also included my attempted code, however the code currently only moves the actuator after the button is pressed. Sorry for my ignorance as this is my first attempt at this kind of control.
http://www.actuonix.com/L16_R_Miniature_Linear_Servo_For_RC_p/l16-r.htm
http://www.robotgeek.com/robotgeek-geekduino
http://www.robotgeek.com/robotgeek-sensor-shield
http://www.robotgeek.com/robotGeek-pushbutton
CODE:
//Includes
#include <Servo.h>
Servo LINEARACTUATOR;
int inPin = 7; // the pin number for input (for me a push button)
int actPin = 9;
int out = 1;
int back = 2;
int current; // Current state of the button
int linearValue = 0; // (LOW is pressed b/c i'm using the pullup resistors)
long millis_held; // How long the button was held (milliseconds)
long secs_held; // How long the button was held (seconds)
long prev_secs_held; // How long the button was held in the previous check
byte previous = HIGH;
unsigned long firstTime; // how long since the button was first pressed
void setup() {
Serial.begin(9600); // Use serial for debugging
LINEARACTUATOR.attach(actPin, 1050, 2000);
pinMode(actPin, OUTPUT);
digitalWrite(inPin, HIGH); // Turn on 20k pullup resistors to simplify switch input
LINEARACTUATOR.writeMicroseconds(linearValue);
}
void loop() {
current = digitalRead(inPin);
// if the button state changes to pressed, remember the start time
if (current == LOW && previous == HIGH && (millis() - firstTime) > 200) {
firstTime = millis();
}
millis_held = (millis() - firstTime);
secs_held = millis_held / 1000;
// This if statement is a basic debouncing tool, the button must be pushed for at least
// 100 milliseconds in a row for it to be considered as a push.
if (millis_held > 50) {
if (current == LOW && secs_held == out) {
linearValue = 2000; // Each second the button is held blink the indicator led
}
LINEARACTUATOR.writeMicroseconds(linearValue);
if (current == LOW && secs_held == back){
linearValue = 0;
}
LINEARACTUATOR.writeMicroseconds(linearValue);
// check if the button was released since we last checked
if (current == HIGH && previous == LOW) {
}
}
previous = current;
prev_secs_held = secs_held;
}