The engine does not stop where it should

Hello everyone, I wrote the following code, the barrier project, used gear motors, I use a rotation angle sensor to recognize the position of the barrier.

At first, everything works fine, that is, all barriers open and stop where they should.

After pressing the first button, the barrier closes, but does not stop in the specified position. If you release the button and press it again, the barrier will not close, that is, after releasing the button, it will work normally. Similarly, when opening the barrier. Please tell me if I made a mistake.

Your sketch never exits home_position()

I did so to end the cycle

I see.

Buttons being pressed do not produce a clean HIGH or LOW. They produce a "noisy" or "ringing" signal for a brief period, making one button press look like 100 presses. Do you have a hardware debounce circuit attached to the buttons (resistor+capacitor)? If not, I think your buttons need to be "debounced" which can be seen here:

https://docs.arduino.cc/built-in-examples/digital/Debounce/

You could translate the comments, or eliminate pointless ones like these

const int sensorPinA = A0; // Пин для подключения первого сенсора

all of which say

Pin for connecting the [nth] sensor

Don't put comments on lines of code that are 100 clear without them.

Those pins are obviously pins for connecting sensors.

If you do have useful comments, it would be polite to translate them.

Don't do it on my behalf - when I read other ppl's code I wear special goggles that make comments invisible as they are almost invariably gratuitous, out of date or just plain wrong.

The code can speak for itself.

a7

I replaced two buttons with bluetooth. The result is the same. Could there be a problem with the angle sensor? Maybe it doesn't update the data in real time, but updates when you release the button?

What does your bluetooth send during "press" and "hold"?

Use "debug" print statements to discover the values of your buttons and motors.

When you press the button, the application sends the symbol "i", when you release it sends the symbol "z"

void secondFunction() {
  jasil = digitalRead(kizil);
  jasil1 = digitalRead(kizil1);

 if (Serial.available()) {
    caracter = Serial.read();
    Serial.println(caracter);
    if (caracter == 'r')
    {
    
   
    sensorValueA = analogRead(sensorPinA);
    sensorAngleA = map(sensorValueA, 0, 1023, 0, 333);
  
    sensorValueB = analogRead(sensorPinB);
    sensorAngleB = map(sensorValueB, 0, 1023, 0, 333);
  
    sensorValueC = analogRead(sensorPinC);
    sensorAngleC = map(sensorValueC, 0, 1023, 0, 333);
   
    sensorValueD = analogRead(sensorPinD);
    sensorAngleD = map(sensorValueD, 0, 1023, 0, 333);
   
    sensorValueG = analogRead(sensorPinG);
    sensorAngleG = map(sensorValueG, 0, 1023, 0, 333);

    sensorValueH = analogRead(sensorPinH);
    sensorAngleH = map(sensorValueH, 0, 1023, 0, 333);
    if (sensorAngleA <= 210) {
      digitalWrite(STBY, HIGH);  
      digitalWrite(AIN1, LOW);  
      digitalWrite(AIN2, HIGH);
      analogWrite(PWMA, 255);    
    }

    if (sensorAngleB <= 210) {
      digitalWrite(STBY, HIGH);  
      digitalWrite(AIN1B, LOW);  
      digitalWrite(AIN2B, HIGH);
      analogWrite(PWMB, 255);    
    }

    if (sensorAngleC <= 218) {
      digitalWrite(STBY1, HIGH);  
      digitalWrite(AIN1C, LOW);  
      digitalWrite(AIN2C, HIGH);
      analogWrite(PWMC, 255);   
    }

    if (sensorAngleD <= 229) {
      digitalWrite(STBY1, HIGH); 
      digitalWrite(AIN1D_PIN, LOW); 
      digitalWrite(AIN2D_PIN, HIGH);
      analogWrite(PWMD, 255);    
    }

    if (sensorAngleH <= 232) {
      digitalWrite(STBY2, HIGH);
      digitalWrite(AIN1H_PIN, LOW);  
      digitalWrite(AIN2H_PIN, HIGH);
      analogWrite(PWMH, 255);  
    }

  }
  else if (caracter == 'i')
  {

    sensorValueA = analogRead(sensorPinA);
    sensorAngleA = map(sensorValueA, 0, 1023, 0, 333);

    sensorValueB = analogRead(sensorPinB);
    sensorAngleB = map(sensorValueB, 0, 1023, 0, 333);

    sensorValueC = analogRead(sensorPinC);
    sensorAngleC = map(sensorValueC, 0, 1023, 0, 333);

    sensorValueD = analogRead(sensorPinD);
    sensorAngleD = map(sensorValueD, 0, 1023, 0, 333);
 
    sensorValueG = analogRead(sensorPinG);
    sensorAngleG = map(sensorValueG, 0, 1023, 0, 333);

    sensorValueH = analogRead(sensorPinH);
    sensorAngleH = map(sensorValueH, 0, 1023, 0, 333);
    if (sensorAngleA >= 155) {
      digitalWrite(STBY, HIGH); 
      digitalWrite(AIN1, HIGH);   
      digitalWrite(AIN2, LOW);
      analogWrite(PWMA, 255);    
    }

    if (sensorAngleB >= 140) {
      digitalWrite(STBY, HIGH); 
      digitalWrite(AIN1B, HIGH);  
      digitalWrite(AIN2B, LOW);
      analogWrite(PWMB, 255);   
    }

    if (sensorAngleC >= 151) {
      digitalWrite(STBY1, HIGH);  
      digitalWrite(AIN1C, HIGH);  
      digitalWrite(AIN2C, LOW);
      analogWrite(PWMC, 255);    
    }

    if (sensorAngleD >= 161) {
      digitalWrite(STBY1, HIGH);  
      digitalWrite(AIN1D_PIN, HIGH);  
      digitalWrite(AIN2D_PIN, LOW);
      analogWrite(PWMD, 255);    
    }

    if (sensorAngleH >= 167) {
      digitalWrite(STBY2, HIGH);  
      digitalWrite(AIN1H_PIN, HIGH);  
      digitalWrite(AIN2H_PIN, LOW);
      analogWrite(PWMH, 255);   

    }
  }
  else if (caracter == 'z'){
    digitalWrite(STBY, LOW);
    digitalWrite(STBY1, LOW);
    digitalWrite(STBY2, LOW);
    digitalWrite(AIN1, LOW);
    digitalWrite(AIN2, LOW);
    digitalWrite(AIN1B, LOW);
    digitalWrite(AIN2B, LOW);
    digitalWrite(AIN1C, LOW);
    digitalWrite(AIN2C, LOW);
    digitalWrite(AIN1D_PIN, LOW);
    digitalWrite(AIN2D_PIN, LOW);
    digitalWrite(AIN1H_PIN, LOW);
    digitalWrite(AIN2H_PIN, LOW);
    analogWrite(PWMA, 0);
    analogWrite(PWMB, 0);
    analogWrite(PWMC, 0);
    analogWrite(PWMD, 0);
    analogWrite(PWMH, 0);
  }

}}
const int sensorPinA = A0; // I'm good enough, I'm smart enough, 
                          //   and doggone it, people like me.

Very funny, thank you for your help

I looked through the Serial angle sensors are working, but the motor does not stop when you do not release the button

Say again. This

When you press the button, the application sends the symbol "i", when you release it sends the symbol "z"

makes it seem like that's what should happen.

Press, 'i', motor runs. Release, 'z', motor stops.

When you do not release the button, you do not get 'z', the moto does not stop.

a7

When you see the first "i", ignore the other "i" until you see a "z"...