Arduino Coding

No results found
MONITORING THE CURRENT OF A LINEAR ACTUATOR

This example code uses a MegaMoto Plus and an Arduino Uno to monitor the current of a linear actuator.

/*  Code to monitor the current amp draw of the actuator, and to cut power if it
  rises above a certain amount.

  Written by Progressive Automations
  August 19th, 2015

  Hardware:
  - RobotPower MegaMoto control boards
  - Arduino Uno
  - 2 pushbuttons
 */

const int EnablePin = 8;
const int PWMPinA = 11;
const int PWMPinB = 3; // pins for Megamoto

const int buttonLeft = 4;
const int buttonRight = 5;//buttons to move the motor

const int CPin1 = A5;  // motor feedback

int leftlatch = LOW;
int rightlatch = LOW;//motor latches (used for code logic)

int hitLimits = 0;//start at 0
int hitLimitsmax = 10;//values to know if travel limits were reached

long lastfeedbacktime = 0; // must be long, else it overflows
int firstfeedbacktimedelay = 750; //first delay to ignore current spike
int feedbacktimedelay = 50; //delay between feedback cycles, how often you want the motor to be checked
long currentTimefeedback = 0; // must be long, else it overflows

int debounceTime = 300; //amount to debounce buttons, lower values makes the buttons more sensitive
long lastButtonpress = 0; // timer for debouncing
long currentTimedebounce = 0;

int CRaw = 0;      // input value for current readings
int maxAmps = 0; // trip limit 

bool dontExtend = false;
bool firstRun = true;
bool fullyRetracted = false;//program logic

void setup()
{
  Serial.begin(9600);
  pinMode(EnablePin, OUTPUT);
  pinMode(PWMPinA, OUTPUT);
  pinMode(PWMPinB, OUTPUT);//Set motor outputs
  pinMode(buttonLeft, INPUT);
  pinMode(buttonRight, INPUT);//buttons
  
  digitalWrite(buttonLeft, HIGH);
  digitalWrite(buttonRight, HIGH);//enable internal pullups
  pinMode(CPin1, INPUT);//set feedback input
  
  currentTimedebounce = millis();
  currentTimefeedback = 0;//Set initial times

  maxAmps = 15;// SET MAX CURRENT HERE

}//end setup

void loop()
{
  latchButtons();//check buttons, see if we need to move

  moveMotor();//check latches, move motor in or out

}//end main loop

void latchButtons()
{
  if (digitalRead(buttonLeft)==LOW)//left is forwards
  {
    currentTimedebounce = millis() - lastButtonpress;// check time since last press
    if (currentTimedebounce > debounceTime && dontExtend == false)//once you've tripped dontExtend, ignore all forwards presses
    {
      leftlatch = !leftlatch;// if motor is moving, stop, if stopped, start moving
      firstRun = true;// set firstRun flag to ignore current spike
      fullyRetracted = false; // once you move forwards, you are not fully retracted
      lastButtonpress = millis();//store time of last button press
      return;
    }//end if
  }//end btnLEFT

  if (digitalRead(buttonRight)==LOW)//right is backwards
  {
    currentTimedebounce = millis() - lastButtonpress;// check time since last press

    if (currentTimedebounce > debounceTime)
    {
      rightlatch = !rightlatch;// if motor is moving, stop, if stopped, start moving
      firstRun = true;// set firstRun flag to ignore current spike
      lastButtonpress = millis();//store time of last button press
      return;    }//end if
  }//end btnRIGHT
}//end latchButtons

void moveMotor()
{
  if (leftlatch == HIGH) motorForward(255); //speed = 0-255
  if (leftlatch == LOW) motorStop();
  if (rightlatch == HIGH) motorBack(255); //speed = 0-255
  if (rightlatch == LOW) motorStop();

}//end moveMotor

void motorForward(int speeed)
{
  while (dontExtend == false && leftlatch == HIGH)
  {
    digitalWrite(EnablePin, HIGH);
    analogWrite(PWMPinA, speeed);
    analogWrite(PWMPinB, 0);//move motor
    if (firstRun == true) delay(firstfeedbacktimedelay); // bigger delay to ignore current spike
    else delay(feedbacktimedelay); //small delay to get to speed

    getFeedback();
    firstRun = false;
    
    latchButtons();//check buttons again
  }//end while

}//end motorForward

void motorBack (int speeed)
{
  while (rightlatch == HIGH)
  {
    digitalWrite(EnablePin, HIGH);
    analogWrite(PWMPinA, 0);
    analogWrite(PWMPinB, speeed);//move motor
    if (firstRun == true) delay(firstfeedbacktimedelay);// bigger delay to ignore current spike
    else delay(feedbacktimedelay); //small delay to get to speed
    getFeedback();

    firstRun = false;
    
    latchButtons();//check buttons again

  }//end while

  dontExtend = false;//allow motor to extend again, after it has been retracted

}//end motorBack

void motorStop()
{
  analogWrite(PWMPinA, 0);
  analogWrite(PWMPinB, 0);

  digitalWrite(EnablePin, LOW);
  firstRun = true;//once the motor has stopped, reenable firstRun to account for startup current spikes

}//end stopMotor

void getFeedback()
{
  CRaw = analogRead(CPin1); // Read current

  if (CRaw == 0 && hitLimits < hitLimitsmax) hitLimits = hitLimits + 1;
  else hitLimits = 0; // check to see if the motor is at the limits and the current has stopped 

  if (hitLimits == hitLimitsmax && rightlatch == HIGH)
  {
    rightlatch = LOW; // stop motor
    fullyRetracted = true;
  }//end if

  else if (hitLimits == hitLimitsmax && leftlatch == HIGH)
  {
    leftlatch = LOW;//stop motor
    hitLimits = 0;
  }//end if

  if (CRaw > maxAmps)
  {
    dontExtend = true;
    leftlatch = LOW; //stop if feedback is over maximum
  }//end if

  lastfeedbacktime = millis();//store previous time for receiving feedback
}//end getFeedback
CONTROLLING MULTIPLE ACTUATORS WITH THE MULTIMOTO ARDUINO SHIELD

This example code shows how to control up to 4 of our linear actuators with the LC-82 MultiMoto Arduino Shield and the LC-066. Due to the current limitations on each channel of the MultiMoto, this code is only meant for use with our PA-14, PA-14P, and PA-11 actuator models.

/*    Example code to control up to 4 actuators,using the Robot Power MultiMoto driver.
   Hardware:
    - Robot Power MultiMoto
    - Arduino Uno

    Wiring:
  - Connect actuators to the M1, M2, M3, M4 connections on the MultiMoto board.
  - Connect the negative (black) to the right connection, positive (red) to the left.
  - Connect a 12 volt source (minimum 1A per motor if unloaded, 8A per motor if fully loaded)to the BAT terminals. Ensure that positive and negative are placed in the correct spots.

   Code modified by Progressive Automations from the example code provided by Robot Power
     <a href="http://www.robotpower.com/downloads/" rel="nofollow"> http://www.robotpower.com/downloads/</a>

    Robot Power MultiMoto v1.0 demo
    This software is released into the Public Domain
*/

// include the SPI library:

#include <SPI.h>
// L9958 slave select pins for SPI
#define SS_M4 14
#define SS_M3 13
#define SS_M2 12
#define SS_M1 11
// L9958 DIRection pins
#define DIR_M1 2
#define DIR_M2 3
#define DIR_M3 4
#define DIR_M4 7
// L9958 PWM pins
#define PWM_M1 9
#define PWM_M2 10    // Timer1
#define PWM_M3 5
#define PWM_M4 6     // Timer0


// L9958 Enable for all 4 motors
#define ENABLE_MOTORS 8

int pwm1, pwm2, pwm3, pwm4;
boolean dir1, dir2, dir3, dir4;

void setup() {
  unsigned int configWord;

  // put your setup code here, to run once:
  pinMode(SS_M1, OUTPUT); digitalWrite(SS_M1, LOW);  // HIGH = not selected
  pinMode(SS_M2, OUTPUT); digitalWrite(SS_M2, LOW);
  pinMode(SS_M3, OUTPUT); digitalWrite(SS_M3, LOW);
  pinMode(SS_M4, OUTPUT); digitalWrite(SS_M4, LOW);

  // L9958 DIRection pins
  pinMode(DIR_M1, OUTPUT);
  pinMode(DIR_M2, OUTPUT);
  pinMode(DIR_M3, OUTPUT);
  pinMode(DIR_M4, OUTPUT);

  // L9958 PWM pins
  pinMode(PWM_M1, OUTPUT);  digitalWrite(PWM_M1, LOW);
  pinMode(PWM_M2, OUTPUT);  digitalWrite(PWM_M2, LOW);    // Timer1
  pinMode(PWM_M3, OUTPUT);  digitalWrite(PWM_M3, LOW);
  pinMode(PWM_M4, OUTPUT);  digitalWrite(PWM_M4, LOW);    // Timer0

  // L9958 Enable for all 4 motors
  pinMode(ENABLE_MOTORS, OUTPUT); 
 digitalWrite(ENABLE_MOTORS, HIGH);  // HIGH = disabled

/ /******* Set up L9958 chips *********
  ' L9958 Config Register
  ' Bit
  '0 - RES
  '1 - DR - reset
  '2 - CL_1 - curr limit
  '3 - CL_2 - curr_limit
  '4 - RES
  '5 - RES
  '6 - RES
  '7 - RES
  '8 - VSR - voltage slew rate (1 enables slew limit, 0 disables)
  '9 - ISR - current slew rate (1 enables slew limit, 0 disables)
  '10 - ISR_DIS - current slew disable
  '11 - OL_ON - open load enable
  '12 - RES
  '13 - RES
  '14 - 0 - always zero
  '15 - 0 - always zero
  */  // set to max current limit and disable ISR slew limiting
  configWord = 0b0000010000001100;

  SPI.begin();
  SPI.setBitOrder(LSBFIRST);
  SPI.setDataMode(SPI_MODE1);  // clock pol = low, phase = high

  // Motor 1
  digitalWrite(SS_M1, LOW);
  SPI.transfer(lowByte(configWord));
  SPI.transfer(highByte(configWord));
  digitalWrite(SS_M1, HIGH);
  // Motor 2
  digitalWrite(SS_M2, LOW);
  SPI.transfer(lowByte(configWord));
  SPI.transfer(highByte(configWord));
  digitalWrite(SS_M2, HIGH);
  // Motor 3
  digitalWrite(SS_M3, LOW);
  SPI.transfer(lowByte(configWord));
  SPI.transfer(highByte(configWord));
  digitalWrite(SS_M3, HIGH);
  // Motor 4
  digitalWrite(SS_M4, LOW);
  SPI.transfer(lowByte(configWord));
  SPI.transfer(highByte(configWord));
  digitalWrite(SS_M4, HIGH);

  //Set initial actuator settings to pull at 0 speed for safety
  dir1 = 0; dir2 = 0; dir3 = 0; dir4 = 0; // Set direction
  pwm1 = 0; pwm2 = 0; pwm3 = 0; pwm4 = 0; // Set speed (0-255)

digitalWrite(ENABLE_MOTORS, LOW);// LOW = enabled
} // End setup

void loop() {
    dir1 = 1;
    pwm1 = 255; //set direction and speed 
    digitalWrite(DIR_M1, dir1);
    analogWrite(PWM_M1, pwm1); // write to pins

    dir2 = 0;
    pwm2 = 128;
    digitalWrite(DIR_M2, dir2);
    analogWrite(PWM_M2, pwm2);

    dir3 = 1;
    pwm3 = 255;
    digitalWrite(DIR_M3, dir3);
    analogWrite(PWM_M3, pwm3);

    dir4 = 0;
    pwm4 = 128;
    digitalWrite(DIR_M4, dir4);
    analogWrite(PWM_M4, pwm4);

    delay(5000); // wait once all four motors are set

    dir1 = 0;
    pwm1 = 128;
    digitalWrite(DIR_M1, dir1);
    analogWrite(PWM_M1, pwm1);

    dir2 = 1;
    pwm2 = 255;
    digitalWrite(DIR_M2, dir2);
    analogWrite(PWM_M2, pwm2);

    dir3 = 0;
    pwm3 = 128;
    digitalWrite(DIR_M3, dir3);
    analogWrite(PWM_M3, pwm3);

    dir4 = 1;
    pwm4 = 255;
    digitalWrite(DIR_M4, dir4);
    analogWrite(PWM_M4, pwm4);

   delay(5000); 

}//end void loop
WASP MOTOR CONTROLLER FOR LINEAR ACTUATOR CONTROL

This example code is for combining our LC-85 Wasp with our LC-066 to control the motion of a linear actuator.

/*Sample code for the Robot Power Wasp. 

 This ESC is controlled using RC signals, with pulses
  ranging from 1000 - 2000 microseconds.

  The main loop of this program holds the actuator still for 1 second, extends for 2 seconds,
  stops for 1 second, retracts for 2 seconds, and repeats.

  Modified by Progressive Automations, using the original example code "Sweep" from the 
  Arduino example libraries. 
  
  Hardware:
  - 1 Wasp Controller
  - Arduino Uno
  
  Wiring:
  Control side:
  - Connect the red/black to +5v and GND
  - Connect the yellow wire to your signal pin on the Arduino (in this example, pin 9)
  Power Side:
  - Connect the +/- of the motors power supply to the +/- connections on the Wasp
  - Connect the +/- of the actuator to the remaining two connections

  This example code is in the public domain.
*/ 

#include <servo.h>  
Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
 
    myservo.writeMicroseconds(1500); // stop signal
    delay(1000);  //1 second
    
    myservo.writeMicroseconds(2000); // full speed forwards signal
    delay(2000); //2 seconds  
    
    myservo.writeMicroseconds(1500); // stop signal
    delay(1000);  // 1 second
    
    myservo.writeMicroseconds(1000); // full speed reverse signal
    delay(2000); //2 seconds   

}
USING RELAYS TO CONTROL LINEAR ACTUATORS

This example code utilizes our relays and our LC-066 to control a linear actuator. You can read our full blog post for more detail.

const int forwards = 7;
const int backwards = 6;//assign relay INx pin to arduino pin

void setup() {
 
pinMode(forwards, OUTPUT);//set relay as an output
pinMode(backwards, OUTPUT);//set relay as an output

}

void loop() {
  
 digitalWrite(forwards, LOW);
 digitalWrite(backwards, HIGH);//Activate the relay one direction, they must be different to move the motor
 delay(2000); // wait 2 seconds

 digitalWrite(forwards, HIGH);
 digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
 delay(2000);// wait 2 seconds
 
 digitalWrite(forwards, HIGH);
 digitalWrite(backwards, LOW);//Activate the relay the other direction, they must be different to move the motor
 delay(2000);// wait 2 seconds

 digitalWrite(forwards, HIGH);
 digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
 delay(2000);// wait 2 seconds

}
CONTROLLING THE TIMING OF A SINGLE LINEAR ACTUATORS MOTION

This example code uses our LC-80, LC-066, any linear actuator and a power source. You can get more detail on the code and what it does in our blog post.

//Use the jumpers on the board to select which pins will be used
int EnablePin1 = 13;
int PWMPinA1 = 11;
int PWMPinB1 = 3;


int extendtime = 10 * 1000;  // 10 seconds, times 1000 to convert to milliseconds
int retracttime = 10 * 1000; // 10 seconds, times 1000 to convert to milliseconds
int timetorun = 300 * 1000; // 300 seconds, times 1000 to convert to milliseconds


int duty;
int elapsedTime;
boolean keepMoving;


void setup() {
  Serial.begin(9600);
  pinMode(EnablePin1, OUTPUT);//Enable the board
  pinMode(PWMPinA1, OUTPUT);
  pinMode(PWMPinB1, OUTPUT);//Set motor outputs
  elapsedTime = 0; // Set time to 0
  keepMoving = true; //The system will move
  
}//end setup
void loop() {
  if (keepMoving) 
  {
    digitalWrite(EnablePin1, HIGH); // enable the motor
    pushActuator();
    delay(extendtime);
    stopActuator();
    delay(10);//small delay before retracting
    
    pullActuator();
    delay(retracttime);
    stopActuator();
    
    elapsedTime = millis();//how long has it been?
    if (elapsedTime > timetorun) {//if it's been 300 seconds, stop

      Serial.print("Elapsed time is over max run time. Max run time: ");
      Serial.println(timetorun);
      keepMoving = false;  
    }
  }//end if
}//end main loop


void stopActuator() {
  analogWrite(PWMPinA1, 0);
  analogWrite(PWMPinB1, 0); // speed 0-255
}


void pushActuator() {
  analogWrite(PWMPinA1, 255);
  analogWrite(PWMPinB1, 0); // speed 0-255
}


void pullActuator() {
  analogWrite(PWMPinA1, 0);
  analogWrite(PWMPinB1, 255);//speed 0-255
}
CONTINUOUSLY EXTEND & RETRACT A LINEAR ACTUATOR WITH BRUSHLESS DC MOTOR

This program can be use to continuously extend and retract the stroke of a linear actuator.

SETUP LOOP CODE
void setup() {
  Serial.begin(9600);  // initialize serial communication at 9600 bits per second

  pinMode(out_lim, INPUT_PULLUP); // configures pin 45 as input pin
  pinMode(in_lim, INPUT_PULLUP); // configures pin 53 as input pin
  pinMode(run_f, OUTPUT); // configures pin 25 as output pin
  pinMode(run_r, OUTPUT); // configures pin 30 as output pin
  
  retract(); // retracts the stroke on startup
  delay(500);
}
void extend() // this function enables the motor to run
{
  digitalWrite(run_f, LOW);
  digitalWrite(run_r, HIGH);
}

void retract() // this function reverses the direction of motor
{
  digitalWrite(run_f, LOW);
  digitalWrite(run_r, LOW);
 }

void run_stop() // this function disables the motor
{
  digitalWrite(run_f, HIGH);
  digitalWrite(run_r, HIGH);
}
void loop() {
  int out_lim_state = digitalRead(out_lim);     // reads the limit switches and saves its value
  int in_lim_state = digitalRead(in_lim);

  Serial.print("outer limit switch value "), Serial.println(out_lim_state); // 0 -> limit switch is pressed
  Serial.print("inner limit switch value "), Serial.println(in_lim_state);  // 1 -> limit switch is not pressed
  
  if (out_lim_state == 0 && in_lim_state == 1) // if outer limit switch is pressed and inner is not (extended all the way)
  {
    retract(); // retract the stroke
  }
 
else if (out_lim_state == 1 && in_lim_state == 0) // if inner limit switch is pressed and outer is not (reracted all the way)
  {
    extend(); // extend the stroke
  }

  else // otherwise do nothing
  {
    
  }
delay(5);        // delay in between reads for stability
}
PA-12 MICRO SERVO ACTUATOR SAMPLE CODES
Click the link for a collection of sample codes for our PA-12 Micro Servo Actuators.