Make sketch wait for button

I made a loop in which a motion detector senses something then a door opens, but I need it to wait after a button is pressed so it can run the same open door loop then begin at the very top again…Please and thank you (:

this is my code so far( i already have abutton connected to digital pin 12)

const int MOTION_PIN = 2;
const byte LED_PIN = 13; // LED pin - active-high
#include <Servo.h>
Servo servo1;

void setup()
{
Serial.begin(9600);
// The PIR sensor’s output signal is an open-collector,
// so a pull-up resistor is required:
pinMode(MOTION_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
servo1.attach(9);

}

void loop()
{
int proximity = digitalRead(MOTION_PIN);
if (proximity == LOW) // If the sensor’s output goes low, motion is detected
{
Serial.println(“Motion detected!”);
openDoor();
holdDoor();
closeDoor();
holdDoor();
// wait for pressed button to open door from insided then

}
else
{
digitalWrite(LED_PIN, LOW);
Serial.println(“No Motion Detected”);
}
}

void holdDoor(){
servo1.write(90);
delay(5000);
}
void openDoor(){
servo1.write(0);
delay(6000);

}

void closeDoor(){
servo1.write(180);
delay(6000);
}

Switch wired like this:

digin.jpg

Is this what you need?

// motion detector senses something then a door opens
const int MOTION_PIN = 2;           
const byte LED_PIN = 13; // LED pin - active-high
const byte BUTTON_PIN = 12;
#include <Servo.h>
Servo servo1;


void setup() 
{
	Serial.begin(9600);
	// The PIR sensor's output signal is an open-collector, 
	// so a pull-up resistor is required:
	pinMode(MOTION_PIN, INPUT_PULLUP);
	pinMode(BUTTON_PIN, INPUT_PULLUP);
	pinMode(LED_PIN, OUTPUT);
	servo1.attach(9);
}   

void loop() 
{
	int proximity = digitalRead(MOTION_PIN);
	if (proximity == LOW) // If the sensor's output goes low, motion is detected
	{
		Serial.println("Motion detected!");
		openDoor();
		holdDoor();
		closeDoor();
		holdDoor();
		// wait for pressed button to open door from insided then

		int button_state = digitalRead(BUTTON_PIN);
		if (button_state == LOW) // If the button press
		{
			openDoor();
			holdDoor();
			closeDoor();
			holdDoor();
		}
	}
	else
	{
		digitalWrite(LED_PIN, LOW);
		Serial.println("No Motion Detected");
	}
}  

void holdDoor(){
	servo1.write(90);
	delay(5000);
}
void openDoor(){
	servo1.write(0);
	delay(6000);

}

void closeDoor(){
	servo1.write(180);
	delay(6000);
}

BillHo:
Is this what you need?

// motion detector senses something then a door opens

const int MOTION_PIN = 2;         
const byte LED_PIN = 13; // LED pin - active-high
const byte BUTTON_PIN = 12;
#include <Servo.h>
Servo servo1;

void setup()
{
Serial.begin(9600);
// The PIR sensor’s output signal is an open-collector,
// so a pull-up resistor is required:
pinMode(MOTION_PIN, INPUT_PULLUP);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
servo1.attach(9);
}

void loop()
{
int proximity = digitalRead(MOTION_PIN);
if (proximity == LOW) // If the sensor’s output goes low, motion is detected
{
Serial.println(“Motion detected!”);
openDoor();
holdDoor();
closeDoor();
holdDoor();
// wait for pressed button to open door from insided then

	int button_state = digitalRead(BUTTON_PIN);
	if (button_state == LOW) // If the button press
	{
		openDoor();
		holdDoor();
		closeDoor();
		holdDoor();
	}
}
else
{
	digitalWrite(LED_PIN, LOW);
	Serial.println("No Motion Detected");
}

}

void holdDoor(){
servo1.write(90);
delay(5000);
}
void openDoor(){
servo1.write(0);
delay(6000);

}

void closeDoor(){
servo1.write(180);
delay(6000);
}

It still goes through the loop and does not wait for the button to be pressed

groundfungus:
Switch wired like this:

digin.jpg

im sorry im a newbie with this kind of stuff but im using the buton provided in the sprkfund inventors kit v3.2
Red, Blue, Yellow, and Green Tactile Buttons

Is this closer to what you want?

// motion detector senses something then a door opens
const int MOTION_PIN = 2;
const byte LED_PIN = 13; // LED pin - active-high
const byte BUTTON_PIN = 12;
#include <Servo.h>
Servo servo1;


void setup()
{
  Serial.begin(9600);
  // The PIR sensor's output signal is an open-collector,
  // so a pull-up resistor is required:
  pinMode(MOTION_PIN, INPUT_PULLUP);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  servo1.attach(9);
}

void loop()
{
  int proximity = digitalRead(MOTION_PIN);
  if (proximity == LOW) // If the sensor's output goes low, motion is detected
  {
    Serial.println("Motion detected!");
    openDoor();
    holdDoor();
    closeDoor();
    holdDoor();
    // wait for pressed button to open door from insided then
  }

  int button_state = digitalRead(BUTTON_PIN);
  else if (button_state == LOW) // If the button press
  {
    openDoor();
    holdDoor();
    closeDoor();
    holdDoor();
  }
  else
  {
    digitalWrite(LED_PIN, LOW);
    Serial.println("No Motion Detected");
  }
}

void holdDoor(){
  servo1.write(90);
  delay(5000);
}
void openDoor(){
  servo1.write(0);
  delay(6000);

}

void closeDoor(){
  servo1.write(180);
  delay(6000);
}

groundfungus:
Is this closer to what you want?

// motion detector senses something then a door opens

const int MOTION_PIN = 2;
const byte LED_PIN = 13; // LED pin - active-high
const byte BUTTON_PIN = 12;
#include <Servo.h>
Servo servo1;

void setup()
{
  Serial.begin(9600);
  // The PIR sensor’s output signal is an open-collector,
  // so a pull-up resistor is required:
  pinMode(MOTION_PIN, INPUT_PULLUP);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  servo1.attach(9);
}

void loop()
{
  int proximity = digitalRead(MOTION_PIN);
  if (proximity == LOW) // If the sensor’s output goes low, motion is detected
  {
    Serial.println(“Motion detected!”);
    openDoor();
    holdDoor();
    closeDoor();
    holdDoor();
    // wait for pressed button to open door from insided then
  }

int button_state = digitalRead(BUTTON_PIN);
  else if (button_state == LOW) // If the button press
  {
    openDoor();
    holdDoor();
    closeDoor();
    holdDoor();
  }
  else
  {
    digitalWrite(LED_PIN, LOW);
    Serial.println(“No Motion Detected”);
  }
}

void holdDoor(){
  servo1.write(90);
  delay(5000);
}
void openDoor(){
  servo1.write(0);
  delay(6000);

}

void closeDoor(){
  servo1.write(180);
  delay(6000);
}

It still seems to loop the code without waiting for a button to be presses after it opens to open up again

You MUST get away from using the delay() function if you expect to be able to detect a button press. Use millis() to manage timing without blocking. See the demo Several Things at a Time

You ALSO need to use the concept of states. In simple terms, your system will be in one of 3 (?) states ...

  • waiting for something to happen
  • motion detected but button not pressed
  • motion detected and button pressed

You need a variable to keep track of that - perhaps something called systemState which can have values of 'w', 'm' and 'b'

When the motion is detected the state will be changed from 'w' to 'm' - but nothing will happen. When the button is pressed it will be changed from 'm' to 'b'. When it is 'b' stuff can happen and when the stuff is finished it should be changed back to 'w'. If the button is pressed when the state is 'w' the button press will be ignored.

...R

Is the switch wired like this. Note that the diagonal pins are connected.digital button switch.jpg

groundfungus:
Is the switch wired like this. Note that the diagonal pins are connected.digital button switch.jpg

i have the top one on the bottom side

Robin2:
You MUST get away from using the delay() function if you expect to be able to detect a button press. Use millis() to manage timing without blocking. See the demo Several Things at a Time

You ALSO need to use the concept of states. In simple terms, your system will be in one of 3 (?) states …

  • waiting for something to happen
  • motion detected but button not pressed
  • motion detected and button pressed

You need a variable to keep track of that - perhaps something called systemState which can have values of ‘w’, ‘m’ and ‘b’

When the motion is detected the state will be changed from ‘w’ to ‘m’ - but nothing will happen. When the button is pressed it will be changed from ‘m’ to ‘b’. When it is ‘b’ stuff can happen and when the stuff is finished it should be changed back to ‘w’. If the button is pressed when the state is ‘w’ the button press will be ignored.

…R

thank you for that but im still alittle confused on the changing of state if you can help alittle more.
would i use boolean conditions to change states for example
if (MOTION_PIN == HIGH && digitalRead(BUTTON_PIN) == LOW)
{
// systemstate = a
}

Would i use that as a void and call it back in the loop? im sorry if im not following correctly

alex117j:
i have the top one on the bottom side

That’s why diagonal is best. You don’t need to look to see what’s the bottom and what’s the side. But really, if you have the button working, let’s move on to the real problem.

alex117j:
would i use boolean conditions to change states for example
if (MOTION_PIN == HIGH && digitalRead(BUTTON_PIN) == LOW)
{
// systemstate = a
}

Nearly but not quite.

Don't do two "external" tests at the same time. The logic should be something like

void loop() {
   motionPinState = digitalRead(MOTION_PIN);
   buttonPinState = digitalRead(BUTTON_PIN);
   if (systemState == 'w' && motionPinState == HIGH) {
      systemState = 'm';
   }
   if (systemState == 'm' && buttonPinState == HIGH) {
      systemState = 'b');
   }
   if (systemState == 'b') {
      // do your stuff
      systemState = 'w'';
   }
}

...R