Two limit switches and a gear motor

Please help me find the error.

I 'm doing a barrier project .
I use ESP8266, touch button, gear motors, DRV8833 driver and two limit switches for locating.

The algorithm of such:

When the power is turned on, the motor turns back until the first limit switch is pressed. (This Barrier is open)

When you press the touch button, if you open the barrier, the motor turns forward until the second limit switch is pressed (This is the barrier closed)

If you still press the touch button if the barrier is closed, the motor turns back until the first limit switch is pressed (This is the barrier open)

My algorithm only works on one side. Please help me solve it!

Code:

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

enum barrierStates barrierState;

const uint8_t upLimitSwitchPin = D5;    // UNO pin 4
const uint8_t downLimitSwitchPin = D6;  // UNO pin 5
const uint8_t touchSwitchPin = D2;      // UNO Pin 6

uint8_t upLimitSwitch;
uint8_t downLimitSwitch;
uint8_t touchSwitch;
uint8_t prevTouchSwitch;

#define AIN1 D3
#define AIN2 D4

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

  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, 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;
  }
}

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

  switch ( barrierState ) {
    case UNKNOWN:
      digitalWrite(AIN2,HIGH);
      upLimitSwitch = digitalRead( upLimitSwitchPin );
      if ( upLimitSwitch == LOW ) {
        digitalWrite(AIN2,LOW);
        Serial.println(F("BARRIER UP"));
        barrierState = UP;
      }
      break;

    case DOWN:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        digitalWrite(AIN2,HIGH);
        Serial.println(F("BARRIER GOING UP"));
        barrierState = GOING_UP;
      }
      break;

    case GOING_UP:
      upLimitSwitch = digitalRead( upLimitSwitchPin );
      if ( upLimitSwitch == LOW ) {
        digitalWrite(AIN2,LOW);
        Serial.println(F("BARRIER UP"));
        barrierState = UP;
      }
      break;

    case UP:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        digitalWrite(AIN1,HIGH);
        Serial.println(F("BARRIER GOING DOWN"));
        barrierState = GOING_DOWN;
      }
      break;

    case GOING_DOWN:
      downLimitSwitch = digitalRead( downLimitSwitchPin );
      if ( downLimitSwitch == LOW ) {
        digitalWrite(AIN1,LOW);
        Serial.println(F("BARRIER DOWN"));
        barrierState = DOWN;
      }
      break;
  }
  prevTouchSwitch = touchSwitch;
}

image

What do you mean by "My algorithm only works on one side."? What do you get on the console?

2 Likes

Do you get bouncing from your limit switches?

Poor’s man anti bounce would be to add a delay(20); when you detect a press and change state

2 Likes

Please explain that!

I can't see a problem with your code.

Tip: You could simplify this

if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) )

to

if (prevTouchSwitch == LOW && touchSwitch == HIGH)
2 Likes

This means - when I turn on the power, nothing will happen, I press the touch button, the motor turns back and stops when the second limit switch is pressed. That is, the barrier is closing. After that, if you press the touch button, nothing will happen, that is, the motor does not turn and the barriers do not open. And nothing appears in Serial

Write a very small sketch to do nothing more than read the 3 digital inputs and print them to serial monitor. Use this to test that all 3 buttons/switches are operating as expected.

1 Like

There's a trick to figuring out why something isn't working:

Use a logic analyzer to see what happens.
Put Serial.print statements at various places in the code to see the values of variables, especially ones that control the motors, and determine whether they meet your expectations.

1 Like

I did as you wrote, it didn't help. I checked all the limit switches and motors, everything works fine.
The algorithm does not work on one side. That is, when the barrier is open, press the button, then the motor turns and the barrier closes perfectly, but after pressing the button nothing happens.

Changed the pin limit switches in the code, everything works like this, but only the other way around

Here is a code-version that does more serial printing for debugging.

Upload this code-version.
copy what gets printed in the serial monitor and post the serial content as a code-section

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


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

char myStateNames[][16] = {
  "UNKNOWN",
  "DOWN",
  "GOING_UP",
  "UP",
  "GOING_DOWN"
};

enum barrierStates barrierState;

const uint8_t upLimitSwitchPin = D5;    // UNO pin 4
const uint8_t downLimitSwitchPin = D6;  // UNO pin 5
const uint8_t touchSwitchPin = D2;      // UNO Pin 6

uint8_t upLimitSwitch;
uint8_t downLimitSwitch;
uint8_t touchSwitch;
uint8_t prevTouchSwitch;

#define AIN1 D3
#define AIN2 D4

void setup() {
  Serial.begin(9600);
  Serial.println("Setup-Start");
  
  pinMode( upLimitSwitchPin, INPUT_PULLUP );
  pinMode( downLimitSwitchPin, INPUT_PULLUP );
  pinMode( touchSwitchPin, INPUT_PULLUP );

  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, 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;
  }
}

void loop() {
  printStateIfChanged();
  
  // assume HIGH = switch touched
  touchSwitch = digitalRead( touchSwitchPin );
  // only in case variable "touchSwitch" has CHANGED its value
  dbgc("01",touchSwitch); // print value only once

  upLimitSwitch = digitalRead( upLimitSwitchPin );
  dbgc("02",upLimitSwitch);
  downLimitSwitch = digitalRead( downLimitSwitchPin );
  dbgc("03",downLimitSwitch);

  switch ( barrierState ) {
    case UNKNOWN:
      digitalWrite(AIN2,HIGH);
      upLimitSwitch = digitalRead( upLimitSwitchPin );
      if ( upLimitSwitch == LOW ) {
        digitalWrite(AIN2,LOW);
        Serial.println(F("BARRIER UP"));
        barrierState = UP;
      }
      break;

    case DOWN:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        digitalWrite(AIN2,HIGH);
        Serial.println(F("BARRIER GOING UP"));
        barrierState = GOING_UP;
      }
      break;

    case GOING_UP:
      upLimitSwitch = digitalRead( upLimitSwitchPin );
      if ( upLimitSwitch == LOW ) {
        digitalWrite(AIN2,LOW);
        Serial.println(F("BARRIER UP"));
        barrierState = UP;
      }
      break;

    case UP:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        digitalWrite(AIN1,HIGH);
        Serial.println(F("BARRIER GOING DOWN"));
        barrierState = GOING_DOWN;
      }
      break;

    case GOING_DOWN:
      downLimitSwitch = digitalRead( downLimitSwitchPin );
      if ( downLimitSwitch == LOW ) {
        digitalWrite(AIN1,LOW);
        Serial.println(F("BARRIER DOWN"));
        barrierState = DOWN;
      }
      break;
  }
  prevTouchSwitch = touchSwitch;
}


void printStateIfChanged() {
  static byte lastbarrierState;

  if (lastbarrierState != barrierState) {
    Serial.print("lastbarrierState changed from ");
    Serial.print(myStateNames[lastbarrierState]);
    Serial.print(" to ");
    Serial.println(myStateNames[barrierState]);    
    lastbarrierState = barrierState;    
  }
}

best regards Stefan

1 Like

Post a picture of the hardware setup in detail.

1 Like
m UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
lastbarrierState changed from UNKNOWN to UP
"01" touchSwitch changed from 0 to 1
BARRIER GOING DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
"01" touchSwitch changed from 1 to 0
"02" upLimitSwitch changed from 0 to 1
lastbarrierState changed from UNKNOWN to GOING_DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
"01" touchSwitch changed from 0 to 1
lastbarrierState changed from UNKNOWN to GOING_DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
lastbarrierState changed from UNKNOWN to GOING_DOWN
"03" downLimitSwitch changed from 1 to 0
BARRIER DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
"01" touchSwitch changed from 1 to 0
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
lastbarrierState changed from UNKNOWN to DOWN
"01" touchSwitch changed from 0 to 1
BARRIER GOING UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
"01" touchSwitch changed from 1 to 0
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP
lastbarrierState changed from UNKNOWN to GOING_UP

ups seems to be a coding error. Will look through again. takes some minutes...

1 Like

Yes was a bug from me

// ==> this is wrong    lastbarrierState = lastbarrierState;    

it must be

    lastbarrierState = barrierState;    // update lastbarrierState with value of **barrier**State

corrected in the code version above. try the code-version posted above again

1 Like

This is when the barrier closes

"01" touchSwitch changed from 0 to 1
BARRIER GOING DOWN
lastbarrierState changed from UP to GOING_DOWN
"01" touchSwitch changed from 1 to 0
"02" upLimitSwitch changed from 0 to 1
BARRIER DOWN
lastbarrierState changed from GOING_DOWN to DOWN
"03" downLimitSwitch changed from 1 to 0

And this is after pressing the button, but the barrier did not open

"01" touchSwitch changed from 0 to 1
BARRIER GOING UP
lastbarrierState changed from DOWN to GOING_UP
"01" touchSwitch changed from 1 to 0

I ran the code from #1 and it functions well. The switches are assumed to be closed and read LOW if the barrier is at the corresponding switch as you say.

You have some issue other than code. It is helpful to add printing and stuff, in general. Here it will not help you find the actual issue.

Please post a schematic, hand drawn is easiest. Show all components and how they are connected, pay particular attention to sources of power and how they are routed to the components that need power.

BARRIER UP
BARRIER GOING DOWN
BARRIER DOWN
BARRIER GOING UP
BARRIER UP
BARRIER GOING DOWN
BARRIER DOWN

a7

2 Likes

If the barrier is down and you do press your button
this part of the code is executed

    case DOWN:
      if (( touchSwitch != prevTouchSwitch ) && ( touchSwitch == HIGH ) ) {
        digitalWrite(AIN2,HIGH);
        Serial.println(F("BARRIER GOING UP"));
        barrierState = GOING_UP;
      }
      break;

If the barrier does not move this means that this line of code has no effect

        digitalWrite(AIN2,HIGH);

This line gets executed
because this line

        Serial.println(F("BARRIER GOING UP"));

that prints BARRIER GOING UP is printed.
same with this line
lastbarrierState changed from DOWN to GOING_UP
gets printed which means your code runs until it is in state
GOING_UP
As you say that the barrier does not move there is an electrical problem with the wiring
of IO-pin named AIN2

which is

#define AIN2 D4

D4 sound like you are using an ESP8266
Is this the case?
If you look up this website

you can read that IO-pin D4 has a pullup-resistor
So it might be that this pullup-resistor is disturbing.
try D5 instead of D4

best regards Stefan

1 Like

Connected to D7, did not help

THX.

So you have a common ground amongst all that wiring? It is not in the schematic or I am a sleeping.

a7

2 Likes

grafik

Connect the drv8833 to GND too.

2 Likes