Can you help me to Change the motor direction with 2 limit switch (TZ-8108)

Can you guys pls help me to change the motor direction using these 2 limit switch (TZ-8108)
Currently im doing the motors fwd & rev direction using timer logic but now have to change the logic in limit switch to change the direction with delay.

#include "controlHelper.h"

//global variables
int botDirection = 1;//1->left 2->right 3->stop
unsigned long previousMillis = 0;
int numberOfCyclesDone = 0;

//assign pins
int BrushMotor = 44; // pwm pins
int DriveMotor = 45;

int IN1_dir_Brush = 42; //direction pins of Brush motor
int IN2_dir_Brush = 46;

int IN1_dir_Drive = 41; //direction pins of Drive Motor
int IN2_dir_Drive = 43;

int But1_H=A2; //Button mapping But1=Low speed, But2=Moderate speed, But3=High speed
int But1_L=A3; //Toggle switch GPIO pins
int But2_H=A4;
int But2_L=A5;
int But3_H=8;
int But3_L=9;

int pwm1; //Pwm of switches
int pwm2;
int pwm3;

//individual button status
bool B1_H = true;
bool B1_L = true;
bool B2_H = true;
bool B2_L = true;
bool B3_H = true;
bool B3_L = true;

//to check button is activated = BX_H | BX_L
bool Bu1 = false;
bool Bu2 = false;
bool Bu3 = false;

//to ensure only single mode is activated at a time
bool check1 = false;
bool check2 = false;
bool check3 = false;
bool check = false;

//variables to store manual inputs
int selectedPWM;
int SelectedDirection;//0->Invalid 1->forward 2->reverse
int currentCycle;

//function to initialize control module
void initControlModule(int PWM1, int PWM2, int PWM3)
{
//set PWM duty-cyles
pwm1 = PWM1;
pwm2 = PWM2;
pwm3 = PWM3;

configureDIO();
}

//function to configure Digital pins
void configureDIO()
{
pinMode(BrushMotor,OUTPUT);
pinMode(DriveMotor,OUTPUT);
pinMode(IN1_dir_Brush,OUTPUT);
pinMode(IN2_dir_Brush,OUTPUT);
pinMode(IN1_dir_Drive,OUTPUT);
pinMode(IN2_dir_Drive,OUTPUT);

pinMode(But1_H,INPUT_PULLUP);
pinMode(But1_L,INPUT_PULLUP);
pinMode(But2_H,INPUT_PULLUP);
pinMode(But2_L,INPUT_PULLUP);
pinMode(But3_H,INPUT_PULLUP);
pinMode(But3_L,INPUT_PULLUP);

digitalWrite(IN1_dir_Brush,LOW);
digitalWrite(IN2_dir_Brush,LOW);
digitalWrite(IN1_dir_Drive,LOW);
digitalWrite(IN2_dir_Drive,LOW);
digitalWrite(BrushMotor,LOW);
digitalWrite(DriveMotor,LOW);
}

//function to get currently selected mode
bool validateManualInput()
{ // put your main code here, to run repeatedly:
B1_H=digitalRead(But1_H); //Reading GPIO
B1_L=digitalRead(But1_L);
B2_H=digitalRead(But2_H);
B2_L=digitalRead(But2_L);
B3_H=digitalRead(But3_H);
B3_L=digitalRead(But3_L);

Bu1=(!B1_H)|(!B1_L); //Ensuring only one is toggled
Bu2=(!B2_H)|(!B2_L);
Bu3=(!B3_H)|(!B3_L);
check1=(~Bu1)&(~Bu2)&(Bu3);
check2=(~Bu1)&(Bu2)&(~Bu3);
check3=(Bu1)&(~Bu2)&(~Bu3);
check=check1|check2|check3;

return check;
}

//function to move bot in user specified direction and speed
void moveBot(int brushPWM, int drivePWM, int cycleTime, int coolingPeriod, int numberOfCycles)
{

// Serial.print("moving......................");
// Serial.print(botDirection);
// Serial.print("\n\rTime Elapsed");
// Serial.print(((millis() - previousMillis)/1000));
// Serial.print("\n\r");

currentCycle = numberOfCyclesDone/2;

//get new time
if(previousMillis == 0) previousMillis = millis();

if (( (unsigned long)(millis() - previousMillis)/1000) >= cycleTime)
{
//check number of cycles
if(numberOfCyclesDone/2 >= numberOfCycles)
{
botDirection = 0;
}
else{

  //stop bot 
    digitalWrite(BrushMotor,LOW);                //If no switch toggled then stop both motors
    digitalWrite(DriveMotor,LOW);
    Serial.write("STOP\n");

    //store current pref.
    selectedPWM = 0;
    SelectedDirection = 0;

    delay((unsigned long)coolingPeriod*1000);
    
    if(botDirection != 2) 
    {
      botDirection = 2;
    }
    else if(botDirection != 1) 
    {
      botDirection = 1;
    }
}


Serial.print("\n\r<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< CHANGE IN DIRECTION >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
previousMillis = 0;
numberOfCyclesDone++;

}

analogWrite(BrushMotor,brushPWM);          //PWM of Brush motor always set
if(botDirection == 1)                             // Setting Direction and pwm of motors 
{                                     // according to the toggle switch
  digitalWrite(IN1_dir_Brush,LOW);           
  digitalWrite(IN2_dir_Brush,HIGH);
  digitalWrite(IN1_dir_Drive,HIGH);           
  digitalWrite(IN2_dir_Drive,LOW);
  analogWrite(DriveMotor,drivePWM);
  Serial.write("RIGHT \n");

  //store current pref.
  selectedPWM = drivePWM;
  SelectedDirection = botDirection;
 }
else if(botDirection == 2)
{
  digitalWrite(IN1_dir_Brush,HIGH);           
  digitalWrite(IN2_dir_Brush,LOW);
  digitalWrite(IN1_dir_Drive,LOW);           
  digitalWrite(IN2_dir_Drive,HIGH);
  analogWrite(DriveMotor,drivePWM);
  Serial.write("LEFT \n");
  
  //store current pref.
  selectedPWM = drivePWM;
  SelectedDirection = botDirection;
 }
 else if(botDirection == 0)
 {
    //stop bot 
    digitalWrite(BrushMotor,LOW);                //If no switch toggled then stop both motors
    digitalWrite(DriveMotor,LOW);
    Serial.write("STOP\n");

    //store current pref.
    selectedPWM = 0;
    SelectedDirection = 0;

// delay(coolingPeriod*1000);
}
}

What is the Serial.write() output that you see? Do you see "STOP" or "<<< change direction >>>" or "RIGHT" or "LEFT"?

Assign the limitswitch/button pins byte buttonPin = ??
Assign thie function pinMode(buttonPin, INPUT)
Replace the millis() test with testing button state buttonState = digitalRead(buttonPin);
Change direction as you normally do.

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