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:
- I'm using
arduino-cli, here's my not-very-perfect Makefile: https://pastebin.pl/view/daa852ff - I use the Servo library: https://www.arduino.cc/reference/en/libraries/servo/
- I'm using an Arduino Uno
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;
}