Motion sensor program issue

Hi so I have this program for a motion but it doesn’t work? what I want to happen is when motion is detected, one LED will come on, and one will turn off then it will print a message on the LCD screen?

"#include “mbed.h”
#include “C12832.h”
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalIn alarm(p29, PullUp);
C12832 lcd(p5, p7, p6, p8, p11);

int main()
{
wait(1);
lcd.cls(); //Clear screen
while(1)
{
led1=1;
led2=0;
if (!alarm){
lcd.printf(“THE MOTION HAS DETECTED”);
wait(0.5);
}
else
led1=0;"

Hi George,

How long does the alarm signal remain low? You probably want to use an InterruptIn rather than a DigitalIn for your alarm. On the falling edge, you can set your led states in the interrupt and set a flag but don’t print inside the interrupt.

Your clear screen only gets called once as it is outside the while(1) loop.

Good luck,
Leon

Hi Leon,
this is the code now. with an interrupt in and fall edge. I’m not sure how to implement the Lcd screen as when I put it into it it doesn’t seem to work? how do I get it so where the alarm is triggered and its a falling edge, a message will appear on the screen?

thanks
George

#include “mbed.h”
#include “C12832.h”
//example using InterruptIn with the input from the PIR motion sensors
InterruptIn alarm(p11);
DigitalOut led(LED1);
DigitalOut flash(LED4);
C12832 lcd(p5, p7, p6, p8, p11);

void flip() {
led = !led;
}

int main() {
alarm.mode(PullUp);
wait(2);
alarm.fall(&flip); // attach the address of the flip function to the falling edge
while(1) { // wait around, interrupts will interrupt
flash = !flash;
wait(0.25);
}
}