1

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);
  }
}
2
  • you will need to implement a 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 – jsotola Jan 17 at 18:45
  • start by making a list if all possible states that the door has ... for example, closed, open, closing fast, closing slow, stopped in middle .... assign a number to each state ... draw a chart and indicate what happens at each state and what causes the transition from one state to another state ... instructables.com/… – jsotola Jan 17 at 18:54
1

You can do this easily by using Arduino built-in PWM function and using delay function. But remember that using delay in your code will block your code so if your program just want to sit and wait for 3 seconds at some speed then delay is ok for you else you need to implement Millis function.

first you need to define different speeds as per your requirement remember PWM function has value from 0-255 where 0 in min speed and 255 is top speed. So here I will define speed1 for top speed and speed2 for low speed So now your program will look like this

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 downPin = 12; 
int upPin = 13;
int speed1 = 255; // Top speed you can change it between [0-255]
int speed2 = 150; // Low speed you can change it between [0-255]

void setup() {
  pinMode(RPWM, OUTPUT);
  pinMode(LPWM, 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(LPWM, speed1); // rotate motor with top speed (with PWM set to 255)
    delay(3000);               // wait for 3 sec
    analogWrite(LPWM, speed2); // rotate motor with low speed (with PWM set to 150)
    delay(2000);               // wait for 2 sec
   // analogWrite(LPWM,0);     // uncomment if you want to stop motor now 
  } 

  else if(digitalRead(downPin)==LOW){ //check if retraction button is pressed
    analogWrite(LPWM, 0);
    analogWrite(RPWM, speed1); // rotate motor with top speed (with PWM set to 255)
    delay(3000);               // wait for 3 sec
    analogWrite(RPWM, speed2); // rotate motor with low speed (with PWM set to 150)
    delay(2000);               // wait for 2 sec
    // analogWrite(RPWM,0);     // uncomment if you want to stop motor now 
  } 

  else{ //if no button is pushed, remain stationary
    analogWrite(RPWM, 0); 
    analogWrite(LPWM, 0);
  }
}
2
  • Excellent! Thanks for your help! Eventually I may want to use millis () but for now this works pretty dang well. Will post final code as addendum above. – Chris Groat Jan 18 at 0:12
  • Good to hear that chris. – dharmik Jan 18 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.