HC-SR312 has three PIN
VCC (2.7 -12V)connect to 3.3V
OUT connect to GP15
GND connect to GND
This code will make the Buildin LED(GP25) in Duo256 up when HC-SR312 detect human move, and keep it on for 60s.
#include <stdio.h>
#include <unistd.h>
#include <time.h> // Add this for time functions
#include <wiringx.h>
int main() {
int DUO_LED = 25;
int DUO_KEY = 15;
if(wiringXSetup("milkv_duo256m", NULL) == -1) {
wiringXGC();
return 1;
}
if(wiringXValidGPIO(DUO_LED) != 0) {
printf("Invalid GPIO %d\n", DUO_LED);
}
pinMode(DUO_LED, PINMODE_OUTPUT);
if(wiringXValidGPIO(DUO_KEY) != 0) {
printf("Invalid GPIO %d\n", DUO_KEY);
}
pinMode(DUO_KEY, PINMODE_INPUT);
// Initialize variables for the timer
time_t start_time;
int led_on = 0; // Flag to check if LED is already on
while(1) {
if(digitalRead(DUO_KEY) == 1 && !led_on) { // If key pressed and LED is not already on
digitalWrite(DUO_LED, HIGH);
led_on = 1; // Set flag
start_time = time(NULL); // Start timer
} else if (led_on) { // If LED is on
time_t current_time = time(NULL);
if(difftime(current_time, start_time) >= 60) { // Check if 60 seconds have passed
digitalWrite(DUO_LED, LOW);
led_on = 0; // Reset flag
}
}
usleep(100000);
}
return 0;
}