I have an important end-of-year project where I need to use a servo motor. The aim of this project is to lower and raise a lamp on a bicycle using an angle sensor that measures a slope to transmit to the servomotor the fact of moving it from 0 to 180°, i.e. from 1ms to 3ms, but to do this automatically with slope values that vary every 3seconds and have the servomotor follow this variation. For example, if a slope is 10°, the angle sensor picks up the measurement and transmits it to the servomotor, which will turn accordingly. If you can answer my questions, I'd be very grateful. I'm sending the code below.:
#include <Servo.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
SoftwareSerial BluetoothSerial(2, 3); // Utiliser des pins 10 et 11 pour TX et RX
Servo myServo; // Création de l'objet Servo
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
pinMode(10, OUTPUT);
// Initialisation de la communication Bluetooth
BluetoothSerial.begin(9600);
// Initialisation du servomoteur
myServo.attach(10); // Connecter le servomoteur à la broche 10
}
void loop() {
// Déclarations des variables
float l_float_Xpente = 0;
float l_float_Xtension = 0;
float l_float_sommeX = 0;
float l_float_moyX = 0;
int l_int_Xout = 0;
int iBcl = 0;
// Lecture et moyenne des valeurs
for (iBcl = 0; iBcl < 10; iBcl++) {
l_int_Xout = analogRead(A4);
l_float_Xtension = (l_int_Xout * 5.0) / 1023;
l_float_sommeX += l_float_Xtension;
delay(300);
}
l_float_moyX = l_float_sommeX / 10;
// Calcul de l'angle
l_float_Xpente = ((l_float_moyX - 1.2628) / (-0.0041)) - 3.90;
// Affichage sur le moniteur série et l'écran LCD
Serial.print("Valeur de l'angle = ");
Serial.println(l_float_Xpente);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("angle = ");
lcd.print(l_float_Xpente);
lcd.write(223); // Caractère degré
// Envoi de la valeur de l'angle via Bluetooth
BluetoothSerial.print("Valeur de l'angle = ");
BluetoothSerial.println(l_float_Xpente);
int angle = map(l_float_Xpente, -35, 35, 0, 180); // Convertit la mesure de la pente en un angle entre 0 et 180 degrés
angle = constrain(angle, 0, 180);
myServo.write(angle);
delay(100); // Délai de 100 millisecondes
}