Hello all,
I would like my arduino monitoring a motion sensor and counting how many time it has been trigerred. This sensor has a "normally closed" switch. As my project will be extended soon with more sensors I have combined an interrupt pin with a regular one and have added the sensors expected to come in the future.
It seems to work but I'd like to make sure I'm not wrong with the pin mode, their state and the pull down resistor..
Can you please take look and let me know how my code looks in regards to my breadboard sketch ?
Here is the code :
const int LED=13;
volatile int NBROFDETECTIONS=0;
int t=0;
const int inter=2;
const int button1=3;
const int button2=4;
const int button3=5;
volatile unsigned long button_time = 0;
volatile int trigger = 0;
int debounce = 250
; // debounce latency in ms
int total = 0;
int inta = 0;
int intb = 0;
int intc = 0;
void setup() {
initPins();
attachInterrupt(0, detect, RISING);
}
void loop() {
// Blink LED with NBROFDETECTIONS-BEGIN
t=0;
delay(2000);
for (t=0;t<NBROFDETECTIONS;t=t+1)
{
digitalWrite(LED,HIGH);
delay(500);
digitalWrite(LED,LOW);
delay(500);
}
// Blink LED with NBROFDETECTIONS-END
}
void detect(){
if (button_time > millis()) return;
delayMicroseconds(32000);
if (digitalRead(button1)) trigger=1;
if (digitalRead(button2)) trigger=2;
if (digitalRead(button3)) trigger=3;
button_time = millis() + debounce;
NBROFDETECTIONS=NBROFDETECTIONS+1;
}
void initPins(){
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(inter, INPUT);
digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
digitalWrite(inter, HIGH);
pinMode(LED,OUTPUT);
digitalWrite(LED, LOW);
}
Thanks in advance !!
Christophe
Sketch 1_bb.pdf (687 KB)