Hi I really need solution quick
I'm trying to make a motion sensor alarm; when the webcam detects motion, the sound will alarm for few seconds and turns off.
I'm currently using my webcam as a motion detection, as well as using my arduino kit. I got the motion detection but here's the biggest problem. I want the sound to activate right as the webcam detects a motion.
Anyway here's my code
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.*;
import processing.video.*;
import processing.serial.*;
Minim minim;
AudioPlayer groove;
Serial myPort; // Create object from Serial class
Capture video; // Create capture device from Video class
PImage prevFrame; // Previous Frame ro compare with
float threshold = 10; // How much change before the code reacts
int theswitch = 0;
int counter = 0;
void setup()
{
size(320, 240);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
video = new Capture(this, width, height, 15);
// Create an empty image the same size as the video
prevFrame = createImage(video.width,video.height,RGB);
minim = new Minim(this);
groove = minim.loadFile("Rods_and_Cones.mp3", 512);
frameRate(75);
noCursor();
smooth();
}
void draw() {
background(0);
// You don't need to display it to analyze it!
image(video,0,0);
float motion = DetectMotion();
/*
if (motion > threshold) {
theswitch = 1;
}
if(theswitch == 1) {
counter++;
}
if(counter > 200) {
theswitch = 0;
counter = 0;
}*/
if (motion > threshold) {
theswitch =1;
counter = 0;
} else {
if (counter > 150) {
theswitch = 0;
}
}
counter++;
if(theswitch == 1) {
fill(204);
myPort.write(1);
}
else {
fill(0);
myPort.write(0);
}
rect(50, 50, 100, 100);
}
// Test if anything in view moves
float DetectMotion() {
// Capture video
if (video.available()) {
// Save previous frame for motion detection!!
prevFrame.copy(video,0,0,video.width,video.height,0,0,video.width,video.
height);
prevFrame.updatePixels();
video.read();
}
loadPixels();
video.loadPixels();
prevFrame.loadPixels();
// Begin loop to walk through every pixel
// Start with a total of 0
float totalMotion = 0;
// Sum the brightness of each pixel
for (int i = 0; i < video.pixels.length; i ++ ) {
// Step 2, what is the current color
color current = video.pixels[i];
// Step 3, what is the previous color
color previous = prevFrame.pixels[i];
// Step 4, compare colors (previous vs. current)
float r1 = red(current);
float g1 = green(current);
float b1 = blue(current);
float r2 = red(previous);
float g2 = green(previous);
float b2 = blue(previous);
// Motion for an individual pixel is the difference between the previous color and current color.
float diff = dist(r1,g1,b1,r2,g2,b2);
// totalMotion is the sum of all color differences.
totalMotion += diff;
}
// averageMotion is total motion divided by the number of pixels analyzed.
float avgMotion = totalMotion / video.pixels.length;
return avgMotion;
}
void float(){
if (groove.isPlaying()){
groove.pause();
} else {
groove.play();
}
}
void stop()
{
groove.close();
minim.stop();
super.stop();
}
Please I need solution.