Sketch shows webcam input and takes a picture every 'x' seconds and checks the dark pixels. When there are more then 'y' dark pixels, the transistor is set low, the picture is saved and the sketch is stopped. When key-R is pressed, the sketch starts again and the webcam input is shown again.
(this can be used to stop a (model) conveyor-motor when there is a visual error, for example damaged box)
Arduino sketch:
int pin12 = 12;
int numReceived;
void setup(){
Serial.begin(9600);
pinMode(pin12, OUTPUT);
digitalWrite(pin12, HIGH);
}
void loop(){
if (Serial.available() > 0){
numReceived = Serial.read();
}
if (numReceived == 125){
digitalWrite(pin12, LOW);
} else {
digitalWrite(pin12, HIGH);
}
}
Processing sketch:
import processing.video.*;
import processing.serial.*;
Capture cam;
PImage img;
color pixelColor;
Serial myPort;
long timeLast = 0; //used by millis function to create a delay
int timeDelay = 10000;
int numFault = 0;
void setup() {
size(320, 240, P3D);
myPort = new Serial(this, Serial.list()[0], 9600);
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("There are no cameras available for capture.");
exit();
} else {
println("Available cameras:");
for (int i = 0; i < cameras.length; i++) {
println(cameras[i]);
}
println("Press c to capture || Press r restart");
// The camera can be initialized directly using an
// element from the array returned by list():
cam = new Capture(this, cameras[5]);
cam.start();
}
}
void draw() {
if (cam.available() == true) {
cam.read();
}
image(cam, 0, 0);
if (millis() - timeLast >= timeDelay){
timeLast = millis();
img = get (0, 0, width, height);
imageCheck();
}
}
void keyPressed(){
if (key == 'r' || key == 'R'){
cam.start();
loop();
int restart = 0;
myPort.write(restart);
}
}
void imageCheck(){
color dark = color(30, 30, 30);
color pixelColor;
int countDark = 0;
int dimension = width * height;
img.loadPixels();
for (int i = 0; i < dimension; i ++) {
pixelColor = img.pixels[i];
if (pixelColor <= dark){
countDark++;
}
}
println("Dark: "+countDark);
if (countDark > 20000){
img.save("test"+numFault);
numFault++;
int stop = 125;
myPort.write(stop);
println("byte:"+stop);
cam.stop();
noLoop();
}
}
Geen opmerkingen:
Een reactie posten