Arduino Mega with dc motor and rotary encoder with Syren50

hello
iam new with Arduino and mcu, i have a challenge with a project, i need to control a DC motor with rotary encoder and Syren50 with 2 limited switch for the safety, after i write the code i can read from the encoder between 0-500 the problem is after i connect it with the Syren50 on TX pin i can’t read the right numbers from the encoder, i tried different baud rate to the Syren this is my code:

#include <RotaryEncoder.h>
#include <Sabertooth.h>

const int switch1PinR = 11;
const int switch2PinL = 12;
int monitoringR;
int monitoringL; 
int IsMotorOn=0;
bool FirstTime = false;
Sabertooth SR(128);
int speedF=30;
int speedB=-30;
int speedS=0;
RotaryEncoder encoder(18, 19);


void setup() {
  // put your setup code here, to run once:

SabertoothTXPinSerial.begin(9600);

  // SR.autobaud();
  
 Serial.begin(115200);
 pinMode(switch1PinR,INPUT);
 pinMode(switch2PinL,INPUT);
 Serial.println("SimplePollRotator example for the RotaryEncoder library.");

}

int DecCounter = 0;


void loop() {

  // put your main code here, to run repeatedly:
monitoringR =digitalRead(switch1PinR);
monitoringL =digitalRead(switch2PinL);
static int pos = 0;
encoder.tick();
int newPos = encoder.getPosition();
if (pos != newPos) {
Serial.println("-------");
Serial.println(newPos);
pos = newPos;
DecCounter=pos;
  
// Serial.println(DecCounter);
  }
 
//Serial.println(DecCounter);
//Serial.print(monitoringL);
//Serial.println();
     
if((monitoringR==1) || (monitoringL==1))
{
  IsMotorOn=1;
}
else
{
  IsMotorOn=0;
  }
if(IsMotorOn==1)
{
 
   //   StartBackAndForth(pos);
}
else
{
  
}

// StartBackAndForth(-100);

//Serial.println(IsMotorOn);
//Serial.println(pos);

    

}

int Direction=0;

void StartBackAndForth(int EnCPos)
{   
   
    if(FirstTime==false){
           SR.motor(1,speedF);
           FirstTime=true;
           Direction=0;
    }
 
      if ((EnCPos <= 25) || (Direction==0))
      {
           SR.motor(1,speedF);
           Direction=0;

      }
      if ((EnCPos >= 330) || (Direction==1))
      {
          SR.motor(1,speedB);
          Direction=1;
      }  
}

in attachment my project picture

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

You need to post links to the non-core libraries that you are using.

You need to read the stickies at the top of the forum, and fix your post to post the code correctly.

void loop() {

  // put your main

The only justification for putting the { on the same line as the function/statement is to save real estate. You look pretty stupid wasting real estate with white space after doing that.

Put EVERY { on a line BY ITSELF.
Put EVERY } on a line BY ITSELF.
Use Tools + Auto Format before deleting the code from your post and posting it correctly.

hello
iam new with Arduino and mcu, i have a challenge with a project, i need to control a DC motor with rotary encoder and Syren50 with 2 limited switch for the safety, after i write the code i can read from the encoder between 0-500 the problem is after i connect it with the Syren50 on TX pin i can’t read the right numbers from the encoder, i tried different baud rate to the Syren this is my code:

const int switch1PinR = 11;
const int switch2PinL = 12;
int monitoringR;
int monitoringL; 
int IsMotorOn=0;
bool FirstTime = false;
Sabertooth SR(128);
int speedF=30;
int speedB=-30;
int speedS=0;
RotaryEncoder encoder(18, 19);

void setup() {
   Serial.begin(115200);
   pinMode(switch1PinR,INPUT);
   pinMode(switch2PinL,INPUT);
   Serial.println("SimplePollRotator example for the RotaryEncoder library.");}

int DecCounter = 0;
void loop() {
    monitoringR =digitalRead(switch1PinR);
    monitoringL =digitalRead(switch2PinL);
    static int pos = 0;
    encoder.tick();
    int newPos = encoder.getPosition();

   if (pos != newPos) {
    Serial.println("-------");
        Serial.println(newPos);
       //Serial.println();
    pos = newPos;
    DecCounter=pos;
   // Serial.println(DecCounter);}
 
if((monitoringR==1) || (monitoringL==1))
{
  IsMotorOn=1;}

else{
  IsMotorOn=0;}

if(IsMotorOn==1){
    //StartBackAndForth(pos);}
         
int Direction=0;

void StartBackAndForth(int EnCPos)
{     
    if(FirstTime==false){
           SR.motor(1,speedF);
           FirstTime=true;
           Direction=0; }
 
      if ((EnCPos <= 25) || (Direction==0))
      {
           SR.motor(1,speedF);
           Direction=0;  }

      if ((EnCPos >= 330) || (Direction==1))
      {
          SR.motor(1,speedB);
          Direction=1; }  }

Can you post a schematic of your project to show all the parts and how they are connected?

Threads merged.