I am new to Arduino and wondering if anyone can help me get it programmed correctly. I am using two buttons to control a linear actuator to open and close a barn door. I want it to go a certain speed for a specified period of time... like 3 seconds. Then I want it to slow down and go another 2 seconds. The linear actuator does not have feedback which is fine for the application because it does not have to be precise. Below is the code I copied from the manufacturers website - it works well, but I can’t figure out how to make it go for specified periods of time. Please remember I’ve never used an Arduino before and this is literally a foreign language to me. I’m just hoping someone can tell me what to do. It’s for my house, not for a school project.
*** ADDENDUM - Thanks for the help. Wish I could post a video... Here is the final code:
/* Firgelli Automations
* Limited or no support: we do not have the resources for Arduino code support
*
* Program enables momentary direction control of actuator using push button
*/
int RPWM = 10; //connect Arduino pin 10 to IBT-2 pin RPWM
int LPWM = 11; //connect Arduino pin 11 to IBT-2 pin LPWM
int RPWM2 = 5; //connect Arduino pin 5 to IBT-2 pin RPWM
int LPWM2 = 6; //connect Arduino pin 6 to IBT-2 pin LPWM
int downPin = 12;
int upPin = 13;
int speed1 = 0; // Top speed you can change it between [0-255]
int speed2 = 0; // Low speed you can change it between [0-255]
int speed3 = 0; // Low speed you can change it between [0-255]
void setup() {
pinMode(RPWM, OUTPUT);
pinMode(LPWM, OUTPUT);
pinMode(RPWM2, OUTPUT);
pinMode(LPWM2, OUTPUT);
pinMode(downPin, INPUT_PULLUP);
pinMode(upPin, INPUT_PULLUP);
}
void loop() {
if(digitalRead(upPin)==LOW){ //check if extension button is pressed
analogWrite(RPWM, 0);
analogWrite(RPWM2, 0);
analogWrite(LPWM, 255); // rotate motor with top speed (with PWM set to 255)
analogWrite(LPWM2, 236); // rotate motor with top speed (with PWM set to 255)
delay(3000); // wait for x sec
analogWrite(LPWM, 185); // rotate motor with low speed (with PWM set to 150)
analogWrite(LPWM2, 181); // rotate motor with low speed (with PWM set to 150)
delay(1600); // wait for x sec
analogWrite(LPWM, 80); // rotate motor with low speed (with PWM set to 150)
analogWrite(LPWM2, 80); // rotate motor with low speed (with PWM set to 150)
delay(1400); // wait for x sec
analogWrite(LPWM, 0); // uncomment if you want to stop motor now
analogWrite(LPWM2, 0); // uncomment if you want to stop motor now
}
else if(digitalRead(downPin)==LOW){ //check if retraction button is pressed
analogWrite(LPWM, 0);
analogWrite(LPWM2, 0);
analogWrite(RPWM, 254); // rotate motor with top speed (with PWM set to 255)
analogWrite(RPWM2, 255); // rotate motor with top speed (with PWM set to 255)
delay(3000); // wait for x sec
analogWrite(RPWM, 170); // rotate motor with low speed (with PWM set to 150)
analogWrite(RPWM2, 170); // rotate motor with low speed (with PWM set to 150)
delay(1500); // wait for x sec
analogWrite(RPWM, 80); // rotate motor with low speed (with PWM set to 150)
analogWrite(RPWM2, 80); // rotate motor with low speed (with PWM set to 150)
delay(1500); // wait for x sec
analogWrite(RPWM, 0); // uncomment if you want to stop motor now
analogWrite(RPWM2, 0); // uncomment if you want to stop motor now
}
else{ //if no button is pushed, remain stationary
analogWrite(RPWM, 0);
analogWrite(LPWM, 0);
analogWrite(RPWM2, 0);
analogWrite(LPWM2, 0);
}
}
state machine
sketch ... duckduckgo.com/?q=arduino+fsm&ia=web ... also, examine the blinkWithoutDelay example sketch to learn how to time events in a non-blocking way, so that the program can respond to button presses, and releases, while the door is moving