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
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");
}
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
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");
}
}
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.
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
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.