2

So this is a bit weird, and I don't understand anything. Basically, I'm just playing with my servo. I wrote a small program, which can be found at this link: https://pastebin.pl/view/8e9c9573 (Note: It's three files, I left some comments there, and the idk function is just my last resort, trying to separate myself from loop, I really don't know :/ ).

So I run controller.Setup() in the setup function, and then in loop I just move it to 180 and detach it, then I run into an infinite loop. Note: I know it's not very good code, but that's not what concerns me right now.

The problem is, when I add the call to detach (line 22), the previous move doesn't run! I think I'm going crazy, cause as soon as I remove the line and recompile, it actually moves.

I tried to remove the lock(Controller::m_Attached), and it still doesn't work. Please let me know if I didn't notice something, but I really don't understand.

Notes:

EDIT: Someone told me to post the code in the question, so here I go:

//Main.ino
#include <Servo.h>
#include "Controller.h"

Controller controller(3);


void idk();

void setup() {
    Serial.begin(9600);
    controller.Setup();
}

void loop() {
    idk();
    while(true);
}

void idk() {
    controller.Move(180);
    controller.Unlock();
}



// Controller.h
#pragma once
#include <Servo.h>

class Controller
{
public:
    Controller(int servoPin) 
        : m_ServoPin(servoPin), m_Attached(false) {}

    void Setup();
    void Move(int angle);

    void Lock();
    void Unlock();

private:
    Servo m_Servo;
    bool m_Attached;
    const int m_ServoPin = 3;
};


// Controller.ino
#include "Controller.h"

void Controller::Setup()
{
    m_Servo.attach(m_ServoPin);
    m_Attached = true;
    Serial.print("[CONTROLLER] Attached Servo\n");
}

void Controller::Move(int angle)
{
    if (m_Attached)
        m_Servo.write(angle);
}

void Controller::Lock() {
    m_Servo.attach(m_ServoPin);
    m_Attached = true;
}

void Controller::Unlock() {
    m_Servo.detach();
    m_Attached = false;
}
stfn
  • 23
  • 4
  • the detach() function probably stops the data pin from outputting the servo control signal ... you are disabling the control signal a few microseconds after you enable it, so it is never generated ... furthermore, a servo requires the signal to be present at all times – jsotola Oct 14 '20 at 17:02
  • @jsotola Thank you sooo much. When I add a delay in between the function calls it works. You should make it an answer so that I can accept it – stfn Oct 14 '20 at 17:13

1 Answers1

2

The detach() function probably stops the data pin from outputting the servo control signal.

Your code disables the control signal a few microseconds after it is enabled.

The cotrol signal is never generated.

Furthermore, a servo requires the signal to be present at all times for it to keep its position.

When your programming includes real-world events, you have to think about the time frame of the events, and about the time frame of the running program.

Think in the terms that the program can loop 100,000 times before the servo has finished moving.

jsotola
  • 1,249
  • 2
  • 10
  • 18