Servo/Linear Actuator Code troubleshooting

I am fairly new to the Arduino world and need help with an issue with a code that I have written to control a servo and a linear actuator.

Background/function:
The servo and the linear actuator are independently controlled via two momentary switches.
Both the servo and the linear actuator are coded to go to a set parameter (angle or distance) with a push of the button and then return with a subsequent push of the button.

Servo is a Parallax Servo
Linear Actuator is a Actuonix P-16-P series connected to an Actuonix LAC board.

If I upload the codes independently (one to just control the servo or one to just control the linear actuator) the code works perfectly.

When I try to fuse the two codes together only the servo is functioning and I cannot get any response from the linear actuator.

I have ran verification on the code and do not get any errors.

Any help with this would be greatly appreciated.

P.S. I apologize if this is in the wrong category or if my code does not display properly.

[table][tr][tt][size=0.8em]Code: see how to post code [/size][/tt][hr][/tr][tr][tt][size=0.8em]#include[nobbc] <[/nobbc]Servo[nobbc].h>[/nobbc]
#include[nobbc] <Bounce2.h>[/nobbc]

[nobbc]//defines[/nobbc]

[nobbc][/nobbc]Servo[nobbc] myservo; [/nobbc][nobbc]// create servo object to control expansion servo[/nobbc]
[nobbc][/nobbc]Servo[nobbc] LINEARACTUATOR; [/nobbc][nobbc]// create servo objects to control the linear actuator[/nobbc]

const int[nobbc] LINEAR_ACTUATOR_PIN = 5; [/nobbc][nobbc]//Linear Actuator Digital Pin[/nobbc]
const int[nobbc] BUTTON_PIN = 4; [/nobbc][nobbc]// the number of the pushbutton pin[/nobbc]

[nobbc]// variables will change:[/nobbc]
int[nobbc] buttonState = 0; [/nobbc][nobbc]// variable for reading the pushbutton status for the servo[/nobbc]

int[nobbc] linearValue = 1000; [/nobbc][nobbc]// current positional value being sent to the linear actuator. [/nobbc]

[nobbc]Bounce debouncer = Bounce(); [/nobbc][nobbc]// debouncing object for linear actuator push button[/nobbc]

#define[nobbc] servoPin 3 [/nobbc][nobbc]//~ PWM for servo[/nobbc]
#define[nobbc] ButtonSPin 2 [/nobbc][nobbc]//input button for expansion servo[/nobbc]

int[nobbc] ButtonSState = 0;[/nobbc]

int[nobbc] buttonSPushed = 0;[/nobbc]

int[nobbc] angle =0; [/nobbc][nobbc]// initial angle for servo[/nobbc]
int[nobbc] angleStep =30;[/nobbc]
const int[nobbc] minAngle = 0;[/nobbc]
const int[nobbc] maxAngle = 90;[/nobbc]

void setupnobbc {[/nobbc]
[nobbc] myservo.[/nobbc]writenobbc;[/nobbc]

[nobbc] myservo.[/nobbc]attachnobbc; [/nobbc][nobbc]// attaches the servo on pin 3 to the servo object[/nobbc]

pinModenobbc; [/nobbc]

[nobbc]//initialize servo/linear actuator objects[/nobbc]
[nobbc] LINEARACTUATOR.[/nobbc]attach[nobbc](LINEAR_ACTUATOR_PIN, 1000, 1500); [/nobbc][nobbc]// attaches/activates the linear actuator as a servo object [/nobbc]

[nobbc]// initialize the pushbutton pin as an input with internal pullup:[/nobbc]
pinMode[nobbc](BUTTON_PIN, [/nobbc]INPUT_PULLUP[nobbc]); [/nobbc]

[nobbc]// After setting up the button, setup the Bounce instance:[/nobbc]
[nobbc] debouncer.[/nobbc]attachnobbc;[/nobbc]
[nobbc] debouncer.interval(5); [/nobbc][nobbc]// interval in ms[/nobbc]

[nobbc]//use the writeMicroseconds to set the linear actuators to their default positions[/nobbc]
[nobbc] LINEARACTUATOR.[/nobbc]writeMicrosecondsnobbc; [/nobbc]

}

void loopnobbc {[/nobbc]
[nobbc]//servo control[/nobbc]
if(digitalReadnobbc == [/nobbc]LOW[nobbc]){[/nobbc]
buttonSPushed = 1;
}
ifnobbc{[/nobbc]
[nobbc]// change the angle for next time through the loop:[/nobbc]
angle = angle + angleStep;

[color=#434F54][nobbc]// reverse the direction of the moving at the ends of the angle:[/nobbc][/color]
[color=#5E6D03]if[/color][nobbc] (angle <= minAngle || angle >= maxAngle) {[/nobbc]
  angleStep = -angleStep;
   buttonSPushed = 0;
}

[nobbc] myservo.[/nobbc]writenobbc; [/nobbc][nobbc]// move the servo to desired angle[/nobbc]
delaynobbc; [/nobbc][nobbc]// waits for the servo to get there[/nobbc]
}

[nobbc]//LA Control[/nobbc]
[nobbc]// Update the Bounce instance :[/nobbc]
debouncer.update();

[nobbc]// Get the updated value :[/nobbc]
int[nobbc] buttonValue = debouncer.[/nobbc]readnobbc;[/nobbc]

if[nobbc] ( debouncer.fell() ) [/nobbc][nobbc]// if the button was pressed[/nobbc]
{
if[nobbc] (linearValue == 1000) [/nobbc][nobbc]//if the box is set to close[/nobbc]
{
[nobbc] linearValue = 1500; [/nobbc][nobbc]//set box to open[/nobbc]
}
else if[nobbc] (linearValue == 1500) [/nobbc][nobbc]//if the box was set to open[/nobbc]
{
[nobbc] linearValue = 1000; [/nobbc][nobbc]//set box to close[/nobbc]
}

}
}[/size][/tt][/tr][/table]

Trying to fix the code...

#include <Servo.h>
#include <Bounce2.h>

//defines

Servo myservo;  // create servo object to control expansion servo

const int LINEAR_ACTUATOR_PIN = 5;        //Linear Actuator Digital Pin
const int BUTTON_PIN = 4;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

Servo LINEARACTUATOR;  // create servo objects to control the linear actuator

int linearValue = 1000;   // current positional value being sent to the linear actuator. 

Bounce debouncer = Bounce(); // debouncing object

#define servoPin 3 //~ PWM for servo
#define ButtonSPin 2 //input button for expansion servo

int ButtonSState = 0;

int buttonSPushed = 0;

int angle =0;    // initial angle  for servo
int angleStep =30;
const int minAngle = 0;
const int maxAngle = 90;

void setup() {
  myservo.write(0);
  
  myservo.attach(servoPin);  // attaches the servo on pin 3 to the servo object
 
  pinMode(ButtonSPin,INPUT_PULLUP);  

//initialize servo/linear actuator objects
  LINEARACTUATOR.attach(LINEAR_ACTUATOR_PIN, 1000, 1500);      // attaches/activates the linear actuator as a servo object 

  // initialize the pushbutton pin as an input with internal pullup:
  pinMode(BUTTON_PIN, INPUT_PULLUP);     

  // After setting up the button, setup the Bounce instance:
  debouncer.attach(BUTTON_PIN);
  debouncer.interval(5); // interval in ms
  
  //use the writeMicroseconds to set the linear actuators to their default positions
  LINEARACTUATOR.writeMicroseconds(linearValue); 

}

void loop() {
//servo control
  if(digitalRead(ButtonSPin) == LOW){
    buttonSPushed = 1;
  }
   if( buttonSPushed ){
  // change the angle for next time through the loop:
  angle = angle + angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle <= minAngle || angle >= maxAngle) {
      angleStep = -angleStep;
       buttonSPushed = 0;
    }
    myservo.write(angle); // move the servo to desired angle
    delay(100); // waits for the servo to get there
   }

//LA Control
// Update the Bounce instance :
  debouncer.update();

  // Get the updated value :
  int buttonValue = debouncer.read();

  if ( debouncer.fell() ) // if the button was pressed
  {    
    if (linearValue == 1000) //if the box is set to close
    {
      linearValue = 1500; //set box to open
    }
    else if (linearValue == 1500) //if the box was set to open
    {
      linearValue = 1000; //set box to close
    }

   }

Your code changes linearvalue when the button is pressed but it doesn't use it on the actuator.

Isn't that the point of this line?

//use the writeMicroseconds to set the linear actuators to their default positions
  LINEARACTUATOR.writeMicroseconds(linearValue); 

I'm not sure I understand your statement.

See below the linear actuator code that does work by itself. I merely copied it into the servo code that also worked by itself.

//Includes
#include <Servo.h> //Servo Library
#include <Bounce2.h> //Debouncing Library

//Defines
const int LINEAR_ACTUATOR_PIN = 5;        //Linear Actuator Digital Pin
const int BUTTON_PIN = 4;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

Servo LINEARACTUATOR;  // create servo objects to control the linear actuator

int linearValue = 1000;   // current positional value being sent to the linear actuator. 

Bounce debouncer = Bounce(); // debouncing object


void setup() 
{ 
  //initialize servo/linear actuator objects
  LINEARACTUATOR.attach(LINEAR_ACTUATOR_PIN, 1000, 1500);      // attaches/activates the linear actuator as a servo object 

  // initialize the pushbutton pin as an input with internal pullup:
  pinMode(BUTTON_PIN, INPUT_PULLUP);     

  // After setting up the button, setup the Bounce instance:
  debouncer.attach(BUTTON_PIN);
  debouncer.interval(5); // interval in ms
  
  //use the writeMicroseconds to set the linear actuators to their default positions
  LINEARACTUATOR.writeMicroseconds(linearValue); 

} 

void loop() 
{ 
    // Update the Bounce instance :
  debouncer.update();

  // Get the updated value :
  int buttonValue = debouncer.read();

  if ( debouncer.fell() ) // if the button was pressed
  {    
    if (linearValue == 1000) //if the box is set to close
    {
      linearValue = 1500; //set box to open
    }
    else if (linearValue == 1500) //if the box was set to open
    {
      linearValue = 1000; //set box to close
    }
  } 
  
  LINEARACTUATOR.writeMicroseconds(linearValue); //use the writeMicroseconds to set the actuator to the new position
} 
}

It looks like you didn't post all your code in post #2. I don't see the end of the loop function.

There it is! Thank you sir. I knew it had to be something little like that. Probably me staring at a computer too long.

This may be more than you need, but who knows:

Simultaneous Control of Multiple Servos

Thank you for taking the time to reply. Wildbill was correct. I missed the last line in my code when I copied and pasted from my notepad to Arduino IDE.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.