Examples - Electronic Bricks - Electronic Brick PIR - Motion Detection Sensor
PIR Motion Sensor Brick - working with Interrupts
This program gives you a basic understanding how to use the PIR motion detection Electronic Brick. Interrupts are needed because of detecting falling or rising digital signals.
Explanation from seeedstudio:
Interrupts are useful for making things happen automatically in microcontroller
programs, and can help solve timing problems. A good task for using an interrupt
might be reading a rotary encoder, monitoring user input.
For an example, if you want to check the PIR sensor when displaying LCD. It may be
lose the PIR pulse signal when updating the screen. The best way is to use the Interrupt to catch the small pulse. In a situations like this, using an interrupt can free the microcontroller to get some other work done without missing the input signal.
Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1
(on digital pin 3). The Arduino Mega has an additional four: numbers 2 (pin 21), 3
(pin 20), 4 (pin 19), and 5 (pin 18).
And you can use the interrupt function to control the interrupt:
attachInterrupt(interrupt, function, mode): Specifies a function to call when an
external interrupt occurs. Replaces any previous function that was attached to the
interrupt.
LOW - to trigger the interrupt whenever the pin is low,
CHANGE - to trigger the interrupt whenever the pin changes value
RISING - to trigger when the pin goes from low to high,
FALLING - for when the pin goes from high to low.
In chassis the D2 and D3 are not prepared individually, but they are include in the Bus
1. So we need a BUS HUB module to break them out of BUS1. Connect the “Input”
connector on the bus hub to “BUS1” connector on chassis by the 10 pins cable.
Hardware Setup:
Connect the PIR sensor to the “Pin2” connector on the bus hub, and connect the LED to the D8 connector on the chassis. Modify the code for interrupt:
int PIR = 0; //define the 2th digital pin for PIR sensor brick interrupt int LED = 8; //define the 8th digital pin for LED brick int time=0; // initial the time void setup() { pinMode(LED,OUTPUT); //set the LED pin for digital output attachInterrupt(PIR, blink, RISING); // enable the interrupt , and when D2 rising jump into the blink() function. } void loop() { if (time>0) // check if need to light the LED { digitalWrite(LED,HIGH); // light the LED time--; // decrease light time } else { digitalWrite(LED,LOW); // turn off the LED } } void blink() { time=1000; // catch the PIR sensor pulse }