Algoirthm and Code Problem

hello, I want to design a garage door code with the components I have, but I couldn't find the kind of solution I want. When I press the button the motor should start spinning and when I press it again it should stop, when I press it again it should spin again. When I press the second button, the motor should reverse and when I press it again, it should stop in the same way. Also, when I press the first button and then the other button, the motor should reverse. I'm seriously confused. Is there an example code for this?

If your research has not turned up any code like this, I doubt that it exists. On the bright side, this is an opportunity for you to create something unique!

Just out of curiosity, why don't you use a ready-made wireless garage door opener?

You need to detect when the button becomes pressed rather than when it is pressed
See the StateChangeDetection example in the IDE

Show what you’ve got

// Arduino Two Push button Garage Door Opener Code

int motorup=13; 
int motordown=12;
int buttonup=2;
int buttondown=4;
bool flag=false;
 
void setup() {
pinMode(motorup, OUTPUT);  
pinMode(motordown, OUTPUT); //Motor reverse
pinMode(buttonup, INPUT);   
pinMode(buttondown, INPUT);  
}
 
void loop() {
  digitalRead(buttonup);
  digitalRead(buttondown);

  if(buttonup==HIGH && buttondown==LOW && flag==false) // Open the door
  {
    flag=true;
    digitalWrite(motorup, HIGH);
    digitalWrite(motordown, LOW);
   }
  if (buttonup==HIGH && buttondown==LOW && flag== true) //Pause the Door
   { 
   flag=false;
   digitalWrite(motorup, LOW); //Kapı Yukarı Doğru Açılır. 
   digitalWrite(motordown, LOW);
   }
  if (buttonup==LOW && buttondown==HIGH && flag==false) // Open the door
   {
    flag=true;
    digitalWrite(motorup,LOW);
    digitalWrite(motordown, HIGH);
   }
   if (buttonup==LOW && buttondown==HIGH && flag ==true) //Pause the Door
   {
    flag=false;
    digitalWrite(motorup, LOW);
    digitalWrite(motordown, LOW);
   }
   else // BUG
   {
    flag=false;
    digitalWrite(motorup,LOW);
    digitalWrite(motordown,LOW);
    }
}

I can write this but not work, there is not any response. Why ?

// Arduino Two Push button Garage Door Opener Code

int motorup=13; 
int motordown=12;
int buttonup=2;
int buttondown=4;
bool flag=false;
 
void setup() {
pinMode(motorup, OUTPUT);  
pinMode(motordown, OUTPUT); //Motor reverse
pinMode(buttonup, INPUT);   
pinMode(buttondown, INPUT);  
}
 
void loop() {
  digitalRead(buttonup);
  digitalRead(buttondown);

  if(buttonup==HIGH && buttondown==LOW && flag==false) // Open the door
  {
    flag=true;
    digitalWrite(motorup, HIGH);
    digitalWrite(motordown, LOW);
   }
  if (buttonup==HIGH && buttondown==LOW && flag== true) //Pause the Door
   { 
   flag=false;
   digitalWrite(motorup, LOW); //Kapı Yukarı Doğru Açılır. 
   digitalWrite(motordown, LOW);
   }
  if (buttonup==LOW && buttondown==HIGH && flag==false) // Open the door
   {
    flag=true;
    digitalWrite(motorup,LOW);
    digitalWrite(motordown, HIGH);
   }
   if (buttonup==LOW && buttondown==HIGH && flag ==true) //Pause the Door
   {
    flag=false;
    digitalWrite(motorup, LOW);
    digitalWrite(motordown, LOW);
   }
   else // BUG
   {
    flag=false;
    digitalWrite(motorup,LOW);
    digitalWrite(motordown,LOW);
    }
}

Why don`t I get any response with this code? Where am I wrong?

// Arduino Two Push button Garage Door Opener Code

int motorup=13; 
int motordown=12;
int buttonup=2;
int buttondown=4;
bool flag=false;
 
void setup() {
pinMode(motorup, OUTPUT);  
pinMode(motordown, OUTPUT); //Motor reverse
pinMode(buttonup, INPUT);   
pinMode(buttondown, INPUT); 
    digitalWrite(motorup,LOW);
    digitalWrite(motordown,LOW); 
}
 
void loop() {

  if(digitalRead(buttonup==HIGH) && digitalRead(buttondown==LOW) && flag==false) // Open the door
  {
    flag=true;
    digitalWrite(motorup, HIGH);
    digitalWrite(motordown, LOW);
   }
  if (digitalRead(buttonup)==HIGH && digitalRead(buttondown)==LOW && flag== true) //Pause the Door
   { 
   flag=false;
   digitalWrite(motorup, LOW); 
   digitalWrite(motordown, LOW);
   }
  if (digitalRead(buttonup)==LOW && digitalRead(buttondown)==HIGH && flag==false) // Open the door
   {
    flag=true;
    digitalWrite(motorup,LOW);
    digitalWrite(motordown, HIGH);
   }
   if (digitalRead(buttonup)==LOW && digitalRead(buttondown)==HIGH && flag ==true) //Pause the Door
   {
    flag=false;
    digitalWrite(motorup, LOW);
    digitalWrite(motordown, LOW);
   }
   else // BUG
   {
    flag=false;
    digitalWrite(motorup,LOW);
    digitalWrite(motordown,LOW);
    }
}

I fix the some issues in code but now, just pin 13 give "1" output everytime and doesn`t effect the input pins of the system. Why ?

If the first if is true, the second will be too, directly after leaving the first.

The same holds true for the second pair of if's.

And you still

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