d'accord avec @lesept
ce qui serait envisageable serait de grouper un servo + un axe de joystick dans une classe SerStick
éventuellement avec votre logique de tester < 15 et > 1000 pour faire un pas dans un sens ou dans l'autre.
par exemple jetez un oeil ici avec 2 instances
le sketch
#include "SerStick.h"
SerStick coude(3, A0);
SerStick poignet(5, A1);
void setup() {
coude.begin();
poignet.begin();
}
void loop() {
coude.tick();
poignet.tick();
delay(50);
}
SerStick.h
#ifndef SERSTICK_H
#define SERSTICK_H
#include <Arduino.h>
#include <Servo.h>
class SerStick {
public:
SerStick(const uint8_t sPin, const uint8_t jPin, const int us = 1200, const int pas = 20): servoPin(sPin), joystickPin(jPin), microSec(us), deltaMicroSec(pas) {};
void begin();
void tick();
private:
Servo moteur;
uint8_t servoPin;
uint8_t joystickPin;
int microSec;
int deltaMicroSec;
};
#endif
SerStick.cpp
#include "SerStick.h"
void SerStick::begin() {
moteur.attach(servoPin);
}
void SerStick::tick() {
int jPos = analogRead(joystickPin); // pas besoin de pinMode() pour un analogRead
if (jPos < 15) microSec += deltaMicroSec; // 15 pourrait être passé au constructeur
else if (jPos > 1000) microSec -= deltaMicroSec; // 1000 pourrait être passé au constructeur
if (microSec < 0) microSec = 0; // 0 pourrait être passé au constructeur
else if (microSec > 2000) microSec = 2000; // 2000 pourriat être passé au constructeur
moteur.writeMicroseconds(microSec);
}