Help, I cannot get a servo to work with MY code

Hello and thank you in advance for any help anyone can offer. I have a project that I am working on, and have working using a linear actuator. This is a working solution to a problem I have solved, but it is far and away not the best. The project unlocks a door that has a crashbar when a button is used.... my linear actuator pulls a cable that moves a piece that unlocks the door. I would like to use a servo to just push on that same part.

Anyways... The problem I am having is I cannot get the servo to move within my code. What I have tried so far and how it went:
I have downloaded and applied the "servo sweep" to my setup and it works.
I can get the movement to work fine until I add the condition of the button push. I even added some light flashes and some serial.print commands to see where it is at in my process, but everything works except the turning of the servo. I will add my code to this message, but I know there has to be someone out there that can point out what I am missing. (BTW, it does compile without issue). I have also tried to coding using subroutines and without subroutines

Thanks in advance for at least looking at my call for help!!!
Corey

type or paste code here#include <ezButton.h>
#include <Servo.h>
#define SERVO_PIN 9 // Pin for the servo
Servo myServo;

ezButton button(6);  // create ezButton object that attach to pin 7;
int pos = 0;

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
  pinMode(9, OUTPUT); 
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  button.loop(); // MUST call the loop() function first
  myServo.write(0);
  delay(100);
  if(button.isReleased())
  {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);   
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);   
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);  
  dunlock();
  delay(1500);
  dlock();
  exit;

  }}
void dunlock() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 180 degrees to 90 degrees
    myServo.write(pos);}              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
    return;
  
}

void dlock() {
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 90 degrees to 180 degrees
    // in steps of 1 degree
    myServo.write(pos);
    delay(15);}             // tell servo to go to position in variable 'pos'
    return;}

If you have gone through all that hassle, you might as well post your code according to the Forum rules. It's the only way you can get any help.

And while you are at it, post also a diagram showing all your connections

thanks, I am new and accidentally posted before adding the code. You caught this fast, so I hope someone that might know what I am doing wrong in the code responds as fast! Going to take me a minute to do a diagram but will get it up there too.

Have a great day
Corey

Your delay(15) is outside the loop, in both cases.
correction. seeing double. just in dunlock().
Also,

  • As far as I can see, you want to loop and execute the conditional whenever the button is released. What does exit do in this context?
  • you can remove the 'return' statements at the end of the two functions. The function is done, it returns. doesn't need to be told to do so.

You need to attach your servo to a pin... inside setup(); is best.

  myServo.attach(SERVO_PIN);
2 Likes

Try this code:

#include "ezButton.h"
#include <Servo.h>
#define SERVO_PIN 9 // Pin for the servo
Servo myServo;

ezButton button(6);  // create ezButton object that attach to pin 7;
int pos = 0;
//--------------------------------------------------------------------
void setup() {
  myServo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the Servo object
  Serial.begin(9600);
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
  // pinMode(SERVO_PIN, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}
//--------------------------------------------------------------------
void loop() {
  button.loop(); // MUST call the loop() function first
  myServo.write(0);
  delay(100);
  if (button.isReleased())
  {
    digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
    dunlock();
    delay(1500);
    dlock();
    //exit;
  }
}
//--------------------------------------------------------------------
void dunlock() {
  for (pos = 0; pos <= 180; pos++) { // goes from 180 degrees to 90 degrees
    myServo.write(pos);
    // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  //return;
}
//--------------------------------------------------------------------
void dlock() {
  for (pos = 180; pos >= 0; pos--) { // goes from 90 degrees to 180 degrees
    // in steps of 1 degree
    myServo.write(pos);
    delay(15);
  }             // tell servo to go to position in variable 'pos'
  // return;
}

So I got it working, thank you so much for all your help. I believe it was the exit that was causing the problem. Thanks again, and have a great day!

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