Stepper Motor does not receive instructions correctly from app inventor and Arduino

Hola! soy nueva con motores y tenia una duda ya que estoy haciendo una aplicación y las luces me funcionan bien cuando envió los strings desde esta pero cuando oprimo el botón de la aplicación de las luces, el stepper motor se acciona solo sin que yo le haya dado la señal, me podrían ayudar por favor.

Hello! I am new with motors and I had a question since I am making an application and the lights work fine for me when I send the strings from this but when I press the button of the application of the lights, the stepper motor activates itself without my having given it the signal, could you help me please.

#include <Stepper.h>


#include <RTClib.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>

#define SS_PIN    53
#define RST_PIN   5
#define SERVO_PIN A1

MFRC522 rfid(SS_PIN, RST_PIN);
Servo servo;

byte authorizedUID1[4] = {0xB7, 0x3B, 0x08, 0x52};
byte authorizedUID2[4] = {0x6B, 0x9A, 0x7C, 0x1B};

int angle = 0; // the current angle of servo motor


int rojo=13;
int led_azul=9;
int rele_luz=3;
int rele_ventilacion=5;
#include <Wire.h>
RTC_DS3231 rtc;

int hora_enc;
int min_enc;
int hora_apa;
int min_apa;


int step = 2038;
int motSpeed = 10;
//Stepper myStepper(step, 11,9,10,8);
Stepper myStepper(step,  8, 10, 9, 11);
int dt = 500;
void setup() {

  myStepper.setSpeed(motSpeed);
  pinMode(rele_luz,OUTPUT);
  
  Serial.begin(9600);//puerto rtc
  rtc.begin();
  Wire.begin();
  Serial1.begin(9600);//puerto bl

   if (! rtc.begin()) {
 Serial.println("Couldn't find RTC");

 
  }
   //rtc.adjust(DateTime(F(DATE), F(TIME)));//
    //se ajusta solo 1 vez ya que si no se actualizaria
    //cada vez que corrieramos el programa 

  SPI.begin(); // init SPI bus
  rfid.PCD_Init(); // init MFRC522
  servo.attach(SERVO_PIN);
  servo.write(angle); // rotate servo motor to 0°

  Serial.println("Tap RFID/NFC Tag on reader");
}

void loop() {


  DateTime now = rtc.now();
    delay(1000);
  if(Serial1.available()){
      String datos = Serial1.readString();
      Serial.println(datos);
      String peticion = Serial1.readString();
      Serial.println(peticion);

  if(datos.startsWith("<")){

        datos.remove(0,1);
        
        hora_enc = (datos.toInt()) ;
        datos.remove (0,((datos.indexOf(","))+ 1));
       
        min_enc = (datos.toInt()) ;
        datos.remove (0,((datos.indexOf(","))+ 1));
        
        hora_apa = (datos.toInt()) ;
        datos.remove (0,((datos.indexOf(","))+ 1));
        
        min_apa = (datos.toInt()) ;
        datos.remove (0,((datos.indexOf(">"))+ 1));    
  }

  if(datos.indexOf("encender_luz")){

    digitalWrite(rele_luz,HIGH);
     digitalWrite(8,LOW);
   digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
  }

  if(datos.indexOf("apagar_luz")){

    digitalWrite(8,LOW);
   digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
   
  }


if(datos.indexOf("b")){
    myStepper.step(step);
    
    
 

  }

  if(datos.indexOf("n")){
    myStepper.step(-step);
    
 

  }







  }

instead of using just 'if's in your code, try using 'if', 'elseif'/'else in you code:
ie this:

    if (datos.indexOf("encender_luz")) {

      digitalWrite(rele_luz, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
    }

    else if (datos.indexOf("apagar_luz")) {

      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);

    }


    else if (datos.indexOf("b")) {
      myStepper.step(step);




    }

    else if (datos.indexOf("n")) {
      myStepper.step(-step);



    }

instead of:

    if (datos.indexOf("encender_luz")) {

      digitalWrite(rele_luz, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
    }

    if (datos.indexOf("apagar_luz")) {

      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);

    }


    if (datos.indexOf("b")) {
      myStepper.step(step);




    }

    if (datos.indexOf("n")) {
      myStepper.step(-step);



    }

hope that helps...

Now the stepper motor doesn't turn on, I don't know what I could be doing wrong.

Not sure then,

Re-wrote your code base on this good tutorial on Serial Input Basics,

Please give it a go.

(compiles, NOT tested!)

#include <RTClib.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <Stepper.h>
#include <Wire.h>

#define SS_PIN    53
#define RST_PIN   5
#define SERVO_PIN A1

MFRC522 rfid(SS_PIN, RST_PIN);
RTC_DS3231 rtc;
Servo servo;

byte authorizedUID1[4] = {0xB7, 0x3B, 0x08, 0x52};
byte authorizedUID2[4] = {0x6B, 0x9A, 0x7C, 0x1B};

int angle = 0; // the current angle of servo motor

int rojo = 13;
int led_azul = 9;
int rele_luz = 3;
int rele_ventilacion = 5;

int hora_enc;
int min_enc;
int hora_apa;
int min_apa;

int step = 2038;
int motSpeed = 10;
int dt = 500;
//Stepper myStepper(step, 11,9,10,8);
Stepper myStepper(step,  8, 10, 9, 11);

const byte numChars = 128;
char receivedChars[numChars];
boolean newData = false;

void setup() {

  myStepper.setSpeed(motSpeed);
  pinMode(rele_luz, OUTPUT);

  Serial.begin(9600);//puerto rtc
  rtc.begin();
  Wire.begin();
  Serial1.begin(9600);//puerto bl

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");


  }
  //rtc.adjust(DateTime(F(DATE), F(TIME)));//
  //se ajusta solo 1 vez ya que si no se actualizaria
  //cada vez que corrieramos el programa

  SPI.begin(); // init SPI bus
  rfid.PCD_Init(); // init MFRC522
  servo.attach(SERVO_PIN);
  servo.write(angle); // rotate servo motor to 0°

  Serial.println("Tap RFID/NFC Tag on reader");
}

void loop() {


  DateTime now = rtc.now();

  recvWithStartEndMarkers();
  DecodeData();

  delay(1000);
}

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial1.available() > 0 && newData == false) {
    rc = Serial1.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

void DecodeData() {

  if (newData == true) {

    String datos = receivedChars;

    Serial.println(datos);

    datos.remove(0, 1);

    hora_enc = (datos.toInt()) ;
    datos.remove (0, ((datos.indexOf(",")) + 1));

    min_enc = (datos.toInt()) ;
    datos.remove (0, ((datos.indexOf(",")) + 1));

    hora_apa = (datos.toInt()) ;
    datos.remove (0, ((datos.indexOf(",")) + 1));

    min_apa = (datos.toInt()) ;
    datos.remove (0, ((datos.indexOf(">")) + 1));

    if (datos.indexOf("encender_luz")) {
      digitalWrite(rele_luz, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
    }
    else if (datos.indexOf("apagar_luz")) {
      digitalWrite(rele_luz, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
    }
    else if (datos.indexOf("b")) {
      myStepper.step(step);
    }
    else if (datos.indexOf("n")) {
      myStepper.step(-step);
    }

    newData = false;
  }
}

thanks friend! the problem was that the code was all messed up but thank you very much for taking the time