Per part de processing
import processing.video.*;
Capture video;
import processing.serial.*;
Serial myPort;
float TotalPixelsBlaus=0;
float TotalPixelsVermells=0;
float TotalPixelsVerds=0;
int comptadorReset=0;
int EstatAra=0;
int EstatAnt=0;
String EstatAraStr;
void setup() {
size(640, 480);
String[] cameras = Capture.list();
printArray(cameras);
video = new Capture(this, cameras[1]);
video.start();
printArray(Serial.list());
//myPort = new Serial(this, Serial.list()[0], 9600);
myPort = new Serial(this, "COM6", 9600); //determinem el port pel qual envia informació a l'Arduino
}
void draw() {
if (video.available() == true) {
video.read();
}
image(video, 0, 0);
float pB=TotalPixelsBlaus/(TotalPixelsBlaus+TotalPixelsVermells+TotalPixelsVerds)*100;
float pR=TotalPixelsVermells/(TotalPixelsBlaus+TotalPixelsVermells+TotalPixelsVerds)*20;//el Vermell predomina, millor que 100
float pG=TotalPixelsVerds/(TotalPixelsBlaus+TotalPixelsVermells+TotalPixelsVerds)*100;
String pRstr = nf(pR, 0, 2); // convertim a dos decimals
String pGstr = nf(pG, 0, 2);
String pBstr = nf(pB, 0, 2);
println(pRstr,pGstr,pBstr); // imprimim dos decimals
if (pR>pB && pR>pG){EstatAra=1; println("envio Vermell");}
else if (pG>pR && pG>pB){EstatAra=2; println("envio Verd");}
else { EstatAra=3; println("envio Blau");}
if (comptadorReset>100){
TotalPixelsBlaus=0; TotalPixelsVermells=0; TotalPixelsVerds=0; comptadorReset=0;}
else {comptadorReset+=1;}
for (int x = 0; x < video.width; x++ ) {
for (int y = 0; y < video.height; y++ ) {
int loc = x + y * video.width;
color currentColor = video.pixels[loc];
float r = red(currentColor); float g = green(currentColor);float b = blue(currentColor);
if (r>b && r>g) {TotalPixelsVermells+=1;}
else if (g>r && g>b) {TotalPixelsVerds+=1;}
else {TotalPixelsBlaus+=1;}
}
}
if (EstatAra!=EstatAnt && comptadorReset>80){
String EstatAraStr=str(EstatAra);
myPort.write(EstatAraStr);
println("he enviat pel serie",EstatAraStr);
delay(2000); //temps per poder llegir el missatge per la consola
EstatAnt=EstatAra;
}
}
Per part d'Arduino
char receivedChar;
boolean newData = false;
String incomingStr="";
void setup() {
Serial.begin(9600);
}
void loop() {
recvOneChar();
int incom1=showNewData();
for (int i = 0; i <= incom1-1; i++) {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
delay(500);
}
void recvOneChar() {
if (Serial.available() > 0) {
receivedChar = Serial.read();
newData = true;
}
}
int showNewData() {
if (newData == true) {
incomingStr = String(receivedChar);
int incom = incomingStr.toInt();
return incom;
}
newData = false;
}
No hay comentarios:
Publicar un comentario