Control of the gear motor at a given speed

That does not seem like enough. What is supposed to happen if you touch the touch switch when neither limit switch is activated? If you hold the touch switch pressed, should it bounce back and forth continuously?
Does it go only while the touch switch is still pressed? Or does it start when the touch switch is first touched? Or when it is released?

1 Like

Hi,
Can we please have a circuit diagram of you new dual limit switch project?
We need to see how your limit switches are wired, C and NO, or C and NC.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

The algorithm remembers the most recent position. For example, if I touch a touch switch when none of the limit switches are activated, the algorithm I sent works correctly

No, if I hold the touch screen, if the algorithm is in the "home position" position to spin forward and will not stop if I do not press the limit switch

The motor starts spinning at the first touch touch. And the motor does not react to almost anyone and will spin until the second limit switch is pressed
For example, the algorithm is in the second position. And the touch button will be pressed, the motor will spin back and spin until the first limit switch is pressed

Hi!
Similarly, but the difference is that the limit switches are connected, pins 22 and 23 are connected.

If you need an exact diagram for more understanding, please tell me, I will try to draw a diagram than I can

Hi, @muhammedjan

Thanks, that would be good, put us all on the one page.

Tom... :smiley: :+1: :coffee: :australia:

1 Like

Here is the full diagram
Regarding the separation of limit switch segments.

The first red part is responsible for the first motor.
The second orange part is for the second motor
The third yellow part is for the third motor
Fourth green part for the fourth motor

Sorry, that is not a circuit diagram.
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

What are the terminals labelled on this microswitch?
linit

Can you please tell us your electronics, programming, arduino, hardware experience?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

I use such limit switches:

  1. They are NO/NC:
    https://aliexpress.ru/item/32273125391.html?spm=a2g2w.orderdetail.0.0.1e1d4aa69GnGcF&sku_id=10000000087821675 - 4 pieces
  2. It's not written:
    https://aliexpress.ru/item/32968198983.html?spm=a2g2w.orderdetail.0.0.4e2a4aa6xcuYH5&sku_id=66597821833 - 4pcs

I have been working with electronics, programming, arduino, hardware for about 3 years

Hi,

Thanks for the information, so you should be able to draw a schematic.

Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Do you need more information from me?

HI,
Your limit switches, are they open or closed contacts when not activated?
This has a big influence on your code logic.

Which driver are you actually using.
The resolution of your "circuit" is very low, you can't read the pin numbers on the Mega.

An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

My limit switches, they have closed contacts when they are not activated.
That's where I tested:

int endSwitchPin = 22; // pin for connecting limit switches

void setup() {
  pinMode(endSwitchPin, INPUT_PULLUP); // setting the pin as an input
  Serial.begin(9600);
}

void loop() {
  int endSwitchState = digitalRead(endSwitchPin); // reading pin status
  if (endSwitchState == HIGH) {
    // limit switch is not activated (contacts are closed)
    Serial.println("contacts are closed");
  } else {
    // limit switch activated (contacts open)
    Serial.println("contacts are open");
  }
  delay(100); // delay for stable state reading
}

When the limit switches are not activated, serial output "contacts are closed"

I use the drv8833 driver to control the motor

You can watch it here

You get a better image if you export jpg.

Tom.. :smiley: :+1: :coffee: :australia:

Is it clearly visible now?

Hi,
I have used a simple CAD to show how clear a schematic can be, compared to Fritzy.
A simple pen(cil) and paper schematic would be just as clear.

Tom.. :smiley: :+1: :coffee: :australia:
PS. Reason I didn't hand draw, I have neuropathy, hands don't work properly when drawing diags.

1 Like

Can you tell me which program you are using?

But you drew the diagram correctly.

**I made such an algorithm. The practical one works, but there are drawbacks. **
How to make the touch button wait for all limit switches to be pressed in setup. So that all the motors come home position. In short, how to make the touch button not work when the functions in setup have not finished their functions yet

enum barrierStates {
  UNKNOWN,
  DOWN,
  GOING_UP,
  UP,
  GOING_DOWN
};
enum barrierStates barrierState;


enum barrierStates2 {
  UNKNOWN2,
  DOWN2,
  GOING_UP2,
  UP2,
  GOING_DOWN2
};
enum barrierStates2 barrierState2;


enum barrierStates3 {
  UNKNOWN3,
  DOWN3,
  GOING_UP3,
  UP3,
  GOING_DOWN3
};
enum barrierStates3 barrierState3;


enum barrierStates4 {
  UNKNOWN4,
  DOWN4,
  GOING_UP4,
  UP4,
  GOING_DOWN4
};
enum barrierStates4 barrierState4;


const uint8_t upLimitSwitchPin = 22;
const uint8_t downLimitSwitchPin = 23;
const uint8_t upLimitSwitchPin2 = 20;
const uint8_t downLimitSwitchPin2 = 21;
const uint8_t upLimitSwitchPin3 = 18;
const uint8_t downLimitSwitchPin3 = 19;
const uint8_t upLimitSwitchPin4 = 16;
const uint8_t downLimitSwitchPin4 = 17;

const uint8_t touchSwitchPin = 10;
const int motorPin4 = 2;
const int motorPin5 = 3;
const int motorPin6 = 4;
const int motorPin7 = 5;
const int motorPin8 = 6;
const int motorPin9 = 7;
const int motorPin10 = 8;
const int motorPin11 = 9;

uint8_t upLimitSwitch;
uint8_t downLimitSwitch;

uint8_t upLimitSwitch2;
uint8_t downLimitSwitch2;

uint8_t upLimitSwitch3;
uint8_t downLimitSwitch3;

uint8_t upLimitSwitch4;
uint8_t downLimitSwitch4;

uint8_t touchSwitch;
uint8_t prevTouchSwitch;

void setup() {
  Serial.begin(9600);
  pinMode( upLimitSwitchPin, INPUT_PULLUP );
  pinMode( downLimitSwitchPin, INPUT_PULLUP );

  pinMode( upLimitSwitchPin2, INPUT_PULLUP );
  pinMode( downLimitSwitchPin2, INPUT_PULLUP );

  pinMode( upLimitSwitchPin3, INPUT_PULLUP );
  pinMode( downLimitSwitchPin3, INPUT_PULLUP );

  pinMode( upLimitSwitchPin4, INPUT_PULLUP );
  pinMode( downLimitSwitchPin4, INPUT_PULLUP );

  pinMode( touchSwitchPin, INPUT_PULLUP );
  pinMode(motorPin4, OUTPUT);
  pinMode(motorPin5, OUTPUT);
  pinMode(motorPin6, OUTPUT);
  pinMode(motorPin7, OUTPUT);
  pinMode(motorPin8, OUTPUT);
  pinMode(motorPin9, OUTPUT);
  pinMode(motorPin10, OUTPUT);
  pinMode(motorPin11, OUTPUT);

  delay(100);
  touchSwitch = digitalRead( touchSwitchPin );
  prevTouchSwitch = touchSwitch;

  // a HIGH here means the limit switch is open
  upLimitSwitch = digitalRead( upLimitSwitchPin );
  downLimitSwitch = digitalRead( downLimitSwitchPin );

  // determine the initial barrier position
  if ( upLimitSwitch == LOW && downLimitSwitch == LOW ) {
    Serial.println(F("ERROR - both limit switches activated"));
    while (1);
  }
  else if ( upLimitSwitch == HIGH && downLimitSwitch == HIGH ) {
    barrierState = UNKNOWN;
    Serial.println(F("BARRIER POSITION UNKNOWN"));
  }
  else if ( upLimitSwitch == LOW ) {
    Serial.println(F("BARRIER UP"));
    barrierState = UP;
  }
  else {
    Serial.println(F("BARRIER DOWN"));
    barrierState = DOWN;
  }

  upLimitSwitch2 = digitalRead( upLimitSwitchPin2 );
  downLimitSwitch2 = digitalRead( downLimitSwitchPin2 );

  if ( upLimitSwitch2 == LOW && downLimitSwitch2 == LOW ) {
    Serial.println(F("ERROR - both limit switches activated2"));
    while (1);
  }
  else if ( upLimitSwitch2 == HIGH && downLimitSwitch2 == HIGH ) {
    barrierState2 = UNKNOWN2;
    Serial.println(F("BARRIER POSITION UNKNOWN"));
  }
  else if ( upLimitSwitch2 == LOW ) {
    Serial.println(F("BARRIER UP2"));
    barrierState2 = UP2;
  }

  else {
    Serial.println(F("BARRIER DOWN2"));
    barrierState2 = DOWN2;
  }

  upLimitSwitch3 = digitalRead( upLimitSwitchPin3 );
  downLimitSwitch3 = digitalRead( downLimitSwitchPin3 );

  if ( upLimitSwitch3 == LOW && downLimitSwitch3 == LOW ) {
    Serial.println(F("ERROR - both limit switches activated3"));
    while (1);
  }
  else if ( upLimitSwitch3 == HIGH && downLimitSwitch3 == HIGH ) {
    barrierState3 = UNKNOWN3;
    Serial.println(F("BARRIER POSITION UNKNOWN3"));
  }
  else if ( upLimitSwitch3 == LOW ) {
    Serial.println(F("BARRIER UP3"));
    barrierState3 = UP3;
  }

  else {
    Serial.println(F("BARRIER DOWN3"));
    barrierState3 = DOWN3;
  }




  upLimitSwitch4 = digitalRead( upLimitSwitchPin4 );
  downLimitSwitch4 = digitalRead( downLimitSwitchPin4 );

  if ( upLimitSwitch4 == LOW && downLimitSwitch4 == LOW ) {
    Serial.println(F("ERROR - both limit switches activated4"));
    while (1);
  }
  else if ( upLimitSwitch4 == HIGH && downLimitSwitch4 == HIGH ) {
    barrierState4 = UNKNOWN4;
    Serial.println(F("BARRIER POSITION UNKNOWN4"));
  }
  else if ( upLimitSwitch4 == LOW ) {
    Serial.println(F("BARRIER UP4"));
    barrierState4 = UP4;
  }

  else {
    Serial.println(F("BARRIER DOWN4"));
    barrierState4 = DOWN4;
  }





}

void loop() {
  // assume HIGH = switch touched
  touchSwitch = digitalRead( touchSwitchPin );

  switch ( barrierState ) {
    case UNKNOWN:
      // INSERT YOUR CODE TO ACTIVATE MOTOR TO MAKE BARRIER GO UP
      digitalWrite(motorPin4, HIGH);
      upLimitSwitch = digitalRead( upLimitSwitchPin );
      if ( upLimitSwitch == LOW ) {
        // INSERT YOUR CODE TO STOP MOTOR
        digitalWrite(motorPin4, LOW);
        Serial.println(F("BARRIER UP"));
        barrierState = UP;
      }
      break;

    case DOWN:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        // INSERT YOUR CODE TO ACTIVATE MOTOR TO MAKE BARRIER GO UP
        digitalWrite(motorPin4, HIGH);
        Serial.println(F("BARRIER GOING UP"));
        barrierState = GOING_UP;
      }
      break;

    case GOING_UP:
      upLimitSwitch = digitalRead( upLimitSwitchPin );
      if ( upLimitSwitch == LOW ) {
        // INSERT YOUR CODE TO STOP MOTOR
        digitalWrite(motorPin4, LOW);
        Serial.println(F("BARRIER UP"));
        barrierState = UP;
      }
      break;

    case UP:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        // INSERT YOUR CODE TO ACTIVATE MOTOR TO MAKE BARRIER GO DOWN
        digitalWrite(motorPin5, HIGH);
        Serial.println(F("BARRIER GOING DOWN"));
        barrierState = GOING_DOWN;
      }
      break;

    case GOING_DOWN:
      downLimitSwitch = digitalRead( downLimitSwitchPin );
      if ( downLimitSwitch == LOW ) {
        // INSERT YOUR CODE TO STOP MOTOR
        digitalWrite(motorPin5, LOW);
        Serial.println(F("BARRIER DOWN"));
        barrierState = DOWN;
      }
      break;
  }

  switch ( barrierState2 ) {

    case UNKNOWN2:
      // INSERT YOUR CODE TO ACTIVATE MOTOR TO MAKE BARRIER GO UP
      digitalWrite(motorPin6, HIGH);
      upLimitSwitch2 = digitalRead( upLimitSwitchPin2 );
      if ( upLimitSwitch2 == LOW ) {
        // INSERT YOUR CODE TO STOP MOTOR
        digitalWrite(motorPin6, LOW);
        Serial.println(F("BARRIER UP"));
        barrierState2 = UP2;
      }
      break;





    case DOWN2:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        // INSERT YOUR CODE TO ACTIVATE MOTOR TO MAKE BARRIER GO UP
        digitalWrite(motorPin6, HIGH);
        Serial.println(F("BARRIER GOING UP2"));
        barrierState2 = GOING_UP2;
      }
      break;





    case GOING_UP2:
      upLimitSwitch2 = digitalRead( upLimitSwitchPin2 );
      if ( upLimitSwitch2 == LOW ) {
        // INSERT YOUR CODE TO STOP MOTOR
        digitalWrite(motorPin6, LOW);
        Serial.println(F("BARRIER UP2"));
        barrierState2 = UP2;
      }
      break;






    case UP2:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        // INSERT YOUR CODE TO ACTIVATE MOTOR TO MAKE BARRIER GO DOWN
        digitalWrite(motorPin7, HIGH);
        Serial.println(F("BARRIER GOING DOWN2"));
        barrierState2 = GOING_DOWN2;
      }
      break;






    case GOING_DOWN2:
      downLimitSwitch2 = digitalRead( downLimitSwitchPin2 );
      if ( downLimitSwitch2 == LOW ) {
        // INSERT YOUR CODE TO STOP MOTOR
        digitalWrite(motorPin7, LOW);
        Serial.println(F("BARRIER DOWN2"));
        barrierState2 = DOWN2;
      }
      break;
  }











  switch ( barrierState3 ) {

    case UNKNOWN3:
      // INSERT YOUR CODE TO ACTIVATE MOTOR TO MAKE BARRIER GO UP
      digitalWrite(motorPin8, HIGH);
      upLimitSwitch3 = digitalRead( upLimitSwitchPin3 );
      if ( upLimitSwitch3 == LOW ) {
        // INSERT YOUR CODE TO STOP MOTOR
        digitalWrite(motorPin8, LOW);
        Serial.println(F("BARRIER UP3"));
        barrierState3 = UP3;
      }
      break;





    case DOWN3:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        // INSERT YOUR CODE TO ACTIVATE MOTOR TO MAKE BARRIER GO UP
        digitalWrite(motorPin8, HIGH);
        Serial.println(F("BARRIER GOING UP3"));
        barrierState3 = GOING_UP3;
      }
      break;





    case GOING_UP3:
      upLimitSwitch3 = digitalRead( upLimitSwitchPin3 );
      if ( upLimitSwitch3 == LOW ) {
        // INSERT YOUR CODE TO STOP MOTOR
        digitalWrite(motorPin8, LOW);
        Serial.println(F("BARRIER UP3"));
        barrierState3 = UP3;
      }
      break;






    case UP3:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        // INSERT YOUR CODE TO ACTIVATE MOTOR TO MAKE BARRIER GO DOWN
        digitalWrite(motorPin9, HIGH);
        Serial.println(F("BARRIER GOING DOWN3"));
        barrierState3 = GOING_DOWN3;
      }
      break;






    case GOING_DOWN3:
      downLimitSwitch3 = digitalRead( downLimitSwitchPin3 );
      if ( downLimitSwitch3 == LOW ) {
        // INSERT YOUR CODE TO STOP MOTOR
        digitalWrite(motorPin9, LOW);
        Serial.println(F("BARRIER DOWN3"));
        barrierState3 = DOWN3;
      }
      break;
  }

  switch ( barrierState4 ) {

    case UNKNOWN4:
      // INSERT YOUR CODE TO ACTIVATE MOTOR TO MAKE BARRIER GO UP
      digitalWrite(motorPin10, HIGH);
      upLimitSwitch4 = digitalRead( upLimitSwitchPin4 );
      if ( upLimitSwitch4 == LOW ) {
        // INSERT YOUR CODE TO STOP MOTOR
        digitalWrite(motorPin10, LOW);
        Serial.println(F("BARRIER UP4"));
        barrierState4 = UP4;
      }
      break;





    case DOWN4:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        // INSERT YOUR CODE TO ACTIVATE MOTOR TO MAKE BARRIER GO UP
        digitalWrite(motorPin10, HIGH);
        Serial.println(F("BARRIER GOING UP4"));
        barrierState4 = GOING_UP4;
      }
      break;





    case GOING_UP4:
      upLimitSwitch4 = digitalRead( upLimitSwitchPin4 );
      if ( upLimitSwitch4 == LOW ) {
        // INSERT YOUR CODE TO STOP MOTOR
        digitalWrite(motorPin10, LOW);
        Serial.println(F("BARRIER UP4"));
        barrierState4 = UP4;
      }
      break;






    case UP4:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        // INSERT YOUR CODE TO ACTIVATE MOTOR TO MAKE BARRIER GO DOWN
        digitalWrite(motorPin11, HIGH);
        Serial.println(F("BARRIER GOING DOWN4"));
        barrierState4 = GOING_DOWN4;
      }
      break;






    case GOING_DOWN4:
      downLimitSwitch4 = digitalRead( downLimitSwitchPin4 );
      if ( downLimitSwitch4 == LOW ) {
        // INSERT YOUR CODE TO STOP MOTOR
        digitalWrite(motorPin11, LOW);
        Serial.println(F("BARRIER DOWN4"));
        barrierState4 = DOWN4;
      }
      break;
  }




  prevTouchSwitch = touchSwitch;
}

Can you help?

Hi,
Have you written test code to check each of your buttons and limit switches?
Write some ode that JUST reads your inputs and use Serial.print statements to send back to the IDE monitor what you code sees input.

Tom... :+1: :smiley: :coffee: :coffee: :australia:
PS. I am saddened that you have not attempted a readable schematic.