Motorized Curtain

Dear All

Plan to make motorized curtain with 12vdc Geared motor with limit switch,

present code is only long press latch code lack of other funtion code so for that component are added like diode, capacitors and comparators to achive desired result

As i am not good at programing need some hint on aurduino code so that extra components can be eleminated.

working of curtain

for curtain going up

  • button 1 short press will start move curtain up and stop when button 1 released
    *button 1 long press >=1 sec will start move curtain up till reaches the limit by sensing limit switch button 2 or if button 3 is short pressed only to stop motor

for curtain going down

  • button 3 short press will start move curtain down and stop when button 3 released
    *button 3 long press >=1 sec will start move curtain down till reaches the limit by sensing limit switch button 4 or if button 1 is short pressed only to stop motor

Thanks in advance

const int buttonPin1 = 2;  // Pin for the first push button
const int buttonPin2 = 3;  // Pin for the second push button
const int buttonPin3 = 4;  // Pin for the third push button
const int buttonPin4 = 5;  // Pin for the fourth push button
const int ledPin1 = 6;     // Pin for the first LED
const int ledPin2 = 7;     // Pin for the second LED

unsigned long buttonPressStartTime1 = 0;
unsigned long buttonPressStartTime2 = 0;
const unsigned long requiredPressTime = 1000;  // 1 seconds

bool ledState1 = false;
bool ledState2 = false;

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(ledPin1, OUTPUT);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
  pinMode(ledPin2, OUTPUT);
}


void loop(){

 
  // Check if the first button is pressed and held
  if (digitalRead(buttonPin1) == HIGH) {
    if (buttonPressStartTime1 == 0) {
      buttonPressStartTime1 = millis();
    } else if (millis() - buttonPressStartTime1 >= requiredPressTime) {
      ledState1 = true;
      digitalWrite(ledPin1, ledState1);
    }
  } else {
    buttonPressStartTime1 = 0;
  }

  // Check if the second button is pressed to turn off the first LED
  if (digitalRead(buttonPin2) == HIGH) {
    ledState1 = false;
    digitalWrite(ledPin1, ledState1);
  }

  // Check if the third button is pressed and held
  if (digitalRead(buttonPin3) == HIGH) {
    if (buttonPressStartTime2 == 0) {
      buttonPressStartTime2 = millis();
    } else if (millis() - buttonPressStartTime2 >= requiredPressTime) {
      ledState2 = true;
      digitalWrite(ledPin2, ledState2);
    }
  } else {
    buttonPressStartTime2 = 0;
  }

  // Check if the fourth button is pressed to turn off the second LED
  if (digitalRead(buttonPin4) == HIGH) {
    ledState2 = false;
    digitalWrite(ledPin2, ledState2);
  }
}

for what? I've not seen any question in your post.

the OneButton library or Toggle can easily offer short and long press detection (without worrying about detecting bounces), making what you describe easy with a simple state machine (limit switches are also buttons)

This is a little bit strange. With a short press of button 1 it is already released. How will your release it again to stop the movement? Point 3 is similar - how will you release a button before it is pressed?

Good idea. There should be no need for complicate external circuits to make buttons dance.

The second circuit drawing is plausible.

A few pints before you go further.

Rename the input pins. Of course they are buttons. On pins. And number four. You could give them meaningful names like upperLimitPin, I would dare to name that one upperLimit and trust that the context and my own habit would mean it was a pin designation.

I'm not good at names, you did you on that if you decide to.

Reconfigure, while it's still not been built, all those switches so they are between the inputs and ground, that is to say that closing the switches will pull the input pin down from their normally pulled up status. Wire the pull-up resistors between the pin and Vcc. I recommend using the resistors, others will say that INPUT_PULLUP mode will mean you don't need the resistor.

You'll need to add some fly back diodes across the relays you drive with transistors.

I hastily scribbled how a schematic that is easier to read might be done. It's sorta a black box diagram that I did, and if it looks like I did it with my finger sitting under an umbrella there's a good reason. It's also cold and rainy, so.

I didn't draw exactly how the relays make the motor go one or the other way. TBH I'm too lazy to intangle that section. I'm sure it can be drawn so it is more obvsly providing power in one direction or the other, amIRight?

You may want to use the normally closed section of switches used as the limit switches, if they are SPDT. That way a failure of the wiring will be equal to being activated, used for "real" limit switches as a form of redundancy. Safer

Then… I agree that perhaps a good button library would make the code easier to write. I prefer to role all my own code to the lowest level possible, but there's where I have fun. Presumably your goal is to get this thing working, so again, you do you.

As for the odd user interface, it did seem odd but I didn't run any of that to ground, deciding first to recommend using meaningful symbols in your code and then got a little carried away. Nothing to do until the sun comes back.

HTH

a7

1 Like

This act as same when push button is pressed motor starts and when button is released motor turns off to fine tune curtain position

Please have look have edited the post

Thanks

Please have look have edited the post

Thanks

Please don't do that anymore! Add information or change your mind in new posts to this thread.

You have perhaps made trash out of some comments.

Changing a post beyond typos and grammar is not a good idea. I don't even bother with those after a few minutes.

a7

Thanks for drawing circuit under an umbrella , yes need to do with button library

That's ok and understandable, but

how will you do that? A short press means button is pressed, and released a short time after pressing. So the button is already released when the motor starts moving. How will your release a released button to stop the motor. The only thing you could do is another short press.

Seems like

  • short presses nudge the motor, runs while the button is down

  • long presses latch the motor moving

  • when the motor is moving, short presses stop it

and the limit switches obvsly stop the motor what activated.

Leaving to discover if not planned for what happens to a moving motor when the opposite direction is held for a long press. I'd argue code that to be the same as a short press, that is to say stop, not stop and reverse.

a7

1 Like

I have not taken a low level flight about your code.

You could use a finite state machine, or FSM. Here's a simple chart

I only labeled some of the arcs.

A short press turns the motor on. If the press doesn't last long enough, the motor stops. If the limit switch is reached the motor stops. If the other button is pressed the motor stops.

Code writes itself. :wink:

a7

2 Likes

Hello mrp440

A very smart design consists of connecting the limit switches coded by diodes into the power lines of the motor.

Have a nice day and enjoy coding in C++.

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