Coding problem with a servo motor

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
}

You didn't ask any questions. What do you need help with?

Please edit your post to include code tags so the code is readable. Include information on what type of Arduino you have and what the other components of your project are.

See this post for more info on how to get help here:

Seems like homework to me...."can you do my work for me??"

If you are traveling on "level" terrain which then turns down a hill, you want the light to point down the hill, parallel to the hill, fixed to the vehicle, not adjusted to "level."

The calculations for the "level" on A4 (range 0 to 1023) makes strange results... vis...
level

Files for WOKWI...

sketch.ino
#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
byte servoPin = 10;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.clear();
  pinMode(servoPin, OUTPUT);

  // Initialisation de la communication Bluetooth
  // BluetoothSerial.begin(9600);

  // Initialisation du servomoteur
  myServo.attach(servoPin); // 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);
  Serial.print("Valeur de l'angle = ");
  Serial.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
}

diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 24, "left": -0.5, "attrs": {} },
    { "type": "wokwi-lcd1602", "id": "lcd1", "top": -169.37, "left": -32, "attrs": {} },
    { "type": "wokwi-servo", "id": "servo1", "top": -69.2, "left": 201.6, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot1", "top": 17.9, "left": 182.2, "attrs": {} }
  ],
  "connections": [
    [ "lcd1:E", "nano:9", "green", [ "v9.6", "h-28.8" ] ],
    [ "nano:4", "lcd1:D4", "green", [ "v-14.4", "h28.3" ] ],
    [ "nano:5", "lcd1:D5", "green", [ "v-38.4", "h19.1" ] ],
    [ "nano:8", "lcd1:RS", "green", [ "v-57.6", "h-29.1" ] ],
    [ "nano:6", "lcd1:D6", "green", [ "v-48", "h38.2" ] ],
    [ "nano:7", "lcd1:D7", "green", [ "v-57.6", "h57.3" ] ],
    [ "servo1:PWM", "nano:10", "green", [ "h0" ] ],
    [ "nano:5V", "servo1:V+", "red", [ "v0" ] ],
    [ "nano:GND.1", "servo1:GND", "black", [ "v0" ] ],
    [ "nano:GND.1", "pot1:GND", "black", [ "v19.2", "h9.6" ] ],
    [ "pot1:SIG", "nano:A4", "green", [ "v28.8", "h-106" ] ],
    [ "pot1:VCC", "nano:5V", "red", [ "v38.4", "h-87.2" ] ]
  ],
  "dependencies": {}
}

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