Please help with the program, I am doing something wrong.

Please help with the program, I am doing something wrong.

Dear, I want to ask you for help please since the routine of my program is executed with errors in your actions.

The problem is that the motors stop prematurely or sometimes they miss the scheduled time and stop at another time.

They are two motors of a PanTilt.

I think it is the software, because when the failure occurs, I see that the serial port that stops sending the words that I put to monitor each step in the void loop.

In addition to stopping the motor, the relay is still active then in addition to the motor stopping, the relay is still active.

I would appreciate the most knowledgeable if you take a look at my program to see what error I have in the code.

#include "AccelStepper.h"
#include <Wire.h>
#include <RTClib.h>


// AccelStepper configuracion

AccelStepper stepperY(AccelStepper::FULL4WIRE, 4, 5, 6, 7);
AccelStepper stepperX(AccelStepper::FULL4WIRE, 8, 9, 10, 11);

// Defino pines a usar para switches del home position

#define home_switchY 3 // Pin 3 conectado to Home Switch (MicroSwitch) Y

#define home_switchX 2 // Pin 2 conectado to Home Switch (MicroSwitch) X

int rele = 12;    // Pin a usar para rele
RTC_DS3231 RTC;

bool inicio = true;
bool fin = true;


// Variables de viaje paso a paso

long initial_homingY = -1;

long initial_homingX = -1;


void setup() {

  Serial.begin(9600);  // inicia Serial monitor con velocidad de 9600 Baudios

  Wire.begin();
  RTC.begin();

  //RTC.adjust (DateTime (__DATE__, __TIME__ ));
  Serial.begin (9600);
  pinMode (rele, OUTPUT);


  pinMode(home_switchY, INPUT_PULLUP);
  pinMode(home_switchX, INPUT_PULLUP);
  pinMode (rele, OUTPUT); // Configurar rele como salida o OUTPUT
  delay(5);

  //Mostramos la hora del sistema
  DateTime fecha = RTC.now () ;

  Serial.print (fecha.day(), DEC) ;
  Serial.print ("/");
  Serial.print (fecha.month(), DEC) ;
  Serial.print ("/");
  Serial.print (fecha.year(), DEC) ;
  Serial.print (" ");
  Serial.print (fecha.hour(), DEC) ;
  Serial.print (":");
  Serial.print (fecha.minute(), DEC) ;
  Serial.print (":");
  Serial.print (fecha.second(), DEC) ;
  Serial.println();

  delay (1000);



  // Establecemos la velocidad máxima y la aceleración de cada Steppers en el inicio para el home


  stepperX.setMaxSpeed(100.0);
  stepperX.setAcceleration(100.0);

  stepperY.setMaxSpeed(100.0);
  stepperY.setAcceleration(100.0);


  // Establecemos posicion home


  homeX ();

  homeY ();

  // Establecemos posicion StandBy

  Serial.println("Estableciendo posicion Stand By . . .");

  stepperX.runToNewPosition(1200);
  stepperY.runToNewPosition(700);
  Serial.println("Posicion Stand By OK");

  apagado ();



}


void loop() {

  DateTime fecha = RTC.now () ;


  ////////// CICLO 1  ///////

  if ( fecha.hour () ==  15 && fecha.minute() == 50 ) {
    inicio = true; //reactiva alarma
    fin = true; //reactiva alarma
  }

  if ( fecha.hour () == 15 && fecha.minute () >= 50 ) {
    if (inicio == true ) {
      Serial.println ("Inicia rutina 1");
      digitalWrite (rele, HIGH);
      rutina1 ();

    }
  }


  if ( fecha.hour () == 15 && fecha.minute () == 59 ) {
    if (fin == true ) {
      digitalWrite (rele, LOW);
      Serial.println ("Rutina 1 terminada");
      apagado ();

      fin = false;    //desactiva alarma
      inicio = false; //desactiva alarma

    }
  }

  delay (1000);

  ////////// CICLO 2  ///////

  if ( fecha.hour () ==  16 && fecha.minute() == 10 ) {
    inicio = true; //reactiva alarma
    fin = true; //reactiva alarma
  }

  if ( fecha.hour () == 16 && fecha.minute () >= 10 ) {
    if (inicio == true ) {
      Serial.println ("Inicia rutina 2");
      digitalWrite (rele, HIGH);
      rutina1 ();
    }
  }


  if ( fecha.hour () == 16 && fecha.minute () == 20 ) {
    if (fin == true ) {
      digitalWrite (rele, LOW);
      Serial.println ("Rutina 2 terminada");
      apagado ();

      fin = false;   //desactiva alarma
      inicio = false;

    }
  }

  delay (1000);

  ////////// CICLO 3  ///////

  if ( fecha.hour () ==  16 && fecha.minute() == 30 ) {
    inicio = true; //reactiva alarma
    fin = true; //reactiva alarma
  }

  if ( fecha.hour () == 16 && fecha.minute () >= 30 ) {
    if (inicio == true ) {
      Serial.println ("Inicia rutina 3");
      digitalWrite (rele, HIGH);
      rutina1 ();

    }
  }


  if ( fecha.hour () == 16 && fecha.minute () == 40 ) {
    if (fin == true ) {
      digitalWrite (rele, LOW);
      Serial.println ("Rutina 3 terminada");
      apagado ();

      fin = false;   //desactiva alarma
      inicio = false;

    }
  }

  delay (1000);


}



////////////////////////////////////// FUNCIONES by CMR ////////////////////////////////////

//FUNCION HOME MOTOR X


void homeX()  {

  Serial.print("Posicionando Motor X al Home . . . . . . . . . . . ");


  while (digitalRead(home_switchX)) {
    stepperX.moveTo(initial_homingX);
    initial_homingX--;
    stepperX.run();
    delay(5);
  }

  stepperX.setCurrentPosition(0);
  stepperX.setMaxSpeed(100.0);
  stepperX.setAcceleration(100.0);
  initial_homingX = 1;

  while (!digitalRead(home_switchX)) {
    stepperX.moveTo(initial_homingX);
    stepperX.run();
    initial_homingX++;
    delay(5);
  }

  stepperX.setCurrentPosition(0);
  Serial.println("Homing Motor X Completado");
  Serial.println("");
  stepperX.setMaxSpeed(200.0);
  stepperX.setAcceleration(200.0);

}

//FUNCION HOME MOTOR Y

void homeY () {

  Serial.print("Posicionando Motor Y al Home . . . . . . . . . . . ");



  while (digitalRead(home_switchY)) {
    stepperY.moveTo(initial_homingY);
    initial_homingY--;
    stepperY.run();
    delay(5);
  }

  stepperY.setCurrentPosition(0);
  stepperY.setMaxSpeed(100.0);
  stepperY.setAcceleration(100.0);
  initial_homingY = 1;


  while (!digitalRead(home_switchY)) {
    stepperY.moveTo(initial_homingY);
    stepperY.run();
    initial_homingY++;
    delay(5);
  }

  stepperY.setCurrentPosition(0);
  Serial.println("Homing Motor Y Completado");
  Serial.println("");
  stepperY.setMaxSpeed(200.0);
  stepperY.setAcceleration(200.0);



}



// FUNCION RUTINA A

void rutina1 () {
  Serial.print("loop 1 . . . ");
  stepperX.runToNewPosition(1500);
  stepperY.runToNewPosition(800);
  delay(500);

  Serial.print("loop 2 . . . ");
  stepperX.runToNewPosition(800);
  stepperY.runToNewPosition(1000);
  delay(500);

  Serial.print("loop 3 . . . ");
  stepperX.runToNewPosition(1500);
  stepperY.runToNewPosition(1200);
  delay(500);

  Serial.print("loop 4 . . . ");
  stepperX.runToNewPosition(800);
  stepperY.runToNewPosition(1400);
  delay(500);

  Serial.print("loop 5 . . . ");
  stepperX.runToNewPosition(1500);
  stepperY.runToNewPosition(1600);
  delay(500);

  Serial.println("loop 6 . . . ");
  stepperX.runToNewPosition(800);
  stepperY.runToNewPosition(1800);
  delay(500);
}



void apagado () {
  digitalWrite (4, LOW);
  digitalWrite (5, LOW);
  digitalWrite (6, LOW);
  digitalWrite (7, LOW);
  digitalWrite (8, LOW);
  digitalWrite (9, LOW);
  digitalWrite (10, LOW);
  digitalWrite (11, LOW);

  Serial.println ("Motores en reposo");
}

PaulS:
We would appreciate it if you would use Tools + Auto Format BEFORE posting code. Your random indenting makes it look like the typing was done by a monkey with a serious hangover.

Some sample output, to narrow down the area where the problem occurs would be useful.

So would a schematic.

Since I’m a programmer, I always suspect the hardware.

updated, code in order.
I am working on the scheme.

  delay (1000);

EVERY pass through loop() executes this code. Why?

  delay (1000);

And this code. Why?

  delay (1000);

And this code. Why?

Aside from the proliferation of delay() calls, the useless comments, the overuse of white space, and the fact that there is too much code in loop(), I don’t see a problem with the code.

I suspect that you have a hardware problem. I suspect that it is with how the steppers are powered.

What, exactly, is the pan-tilt mechanism doing and carrying?

PaulS:

  delay (1000);

EVERY pass through loop() executes this code. Why?

  delay (1000);

And this code. Why?

  delay (1000);

And this code. Why?

Aside from the proliferation of delay() calls, the useless comments, the overuse of white space, and the fact that there is too much code in loop(), I don't see a problem with the code.

I suspect that you have a hardware problem. I suspect that it is with how the steppers are powered.

What, exactly, is the pan-tilt mechanism doing and carrying?

Hello!
What I need is to move this pan-tilt that has 2 built-in 12V 30 ohm motors.
The routine is simple, the motor that makes the "Pan" moves from side to side and the one that fulfills the function for the "Tilt" every few steps moves down.

This is the link of the general product.

https://www.alibaba.com/product-detail/BESNT-Italy-hot-selling-motorized-220V_60717851799.html?spm=a2700.12243863.0.0.1c6b5e2cMkmk9z

Hello cessariuss,

You really need to learn to write code without using delay(); all delay() does is make your code slow and unresponsive. Read these tutorials to get you started.
Using millis for timing
Demonstration for several things at the same time

PerryBebbington:
Hello cessariuss,

You really need to learn to write code without using delay(); all delay() does is make your code slow and unresponsive. Read these tutorials to get you started.
Using millis for timing
Demonstration for several things at the same time

Thank you very much teacher !! I will read them carefully.

I have removed the excess delay, actually all. I realized I don't need them.

I also detected in what condition the motors stop (not the relay). They stop when there is a time change.

Example, a routine begins at 9:30 p.m. and must end at 10:30 p.m. This routine is interrupted just when it is 22:00 hrs.

Any ideas how to avoid blocking?

Any ideas how to avoid blocking?

Yes, follow the advice I gave you in reply #4 and learn from the demonstration of several things at a time example, then modify your code to take advantage of what you have learnt.