Using Switch, ending case search problem
in
Core Library Questions
•
1 years ago
I am trying to select a color via the camera and have processing return a message depending on whether it's R, G, or B and how many times it has seen that color (up to 3). The problem is, after it returns the first cases, it begins to reiterate the messages on top of each other rather than starting over with the new color;
In other words, it should print:
RED IS DOMINANT. HAS BEEN 1 times.
Scared
RED IS DOMINANT. HAS BEEN 2 times.
Maybe
RED IS DOMINANT. HAS BEEN 3 times.
OK
BLUE IS DOMINANT. HAS BEEN 1 times.
Scared
BLUE IS DOMINANT. HAS BEEN 1 times.
Maybe
and so on. What it is actually doing is this:
RED IS DOMINANT. HAS BEEN 1 times.
Scared
Maybe
OK
RED IS DOMINANT. HAS BEEN 2 times.
Maybe
OK
RED IS DOMINANT. HAS BEEN 3 times.
OK
BLUE IS DOMINANT. HAS BEEN 1 times.
OK
Scared
Maybe
OK
RED IS DOMINANT. HAS BEEN 4 times.
Scared
Maybe
OK
I need it to just print the one case. I think I need some sort of end statement but I'm new to Processing and so far haven't figured out what looks like. Here's my code:
import processing.serial.*;
import processing.video.*;
Serial port;
float val = 0;
Capture camera;
int numTimesRed=0;
int numTimesBlue=0;
int numTimesGreen=0;
void setup()
{
size(320, 240);
println(Capture.list());
camera = new Capture(this, 320, 240);
//println(Serial.list());
port = new Serial(this, Serial.list()[1], 9600);
background(val);
}
void draw()
{
if (camera.available()) { // finds camera s
camera.read();
int redCount=0; // zeros out counts
int greenCount=0;
int blueCount=0;
image(camera, 0, 0);
loadPixels();
for (int i=0; i < pixels.length;i++)
{
int c=pixels[i];
int r=(c>>16)&0x0000FF;
int g=(c>>8)&0x0000FF;
int b=c&0x0000FF;
if (r>g && r>b)
{
r=255;
g=0;
b=0;
redCount++;
}
else if (g>r && g>b)
{
r=0;
g=255;
b=0;
greenCount++;
}
else if (b>r && b>g)
{
r=0;
g=0;
b=255;
blueCount++;
}
else
{
r=255;
g=255;
b=255;
}
pixels[i]=0x000000|(r<<16)|(g<<8)|b;
} // done looping over pixels
// from that frame, what was the dominant color?
if ((redCount > greenCount) && (redCount > blueCount)) {
// red is dominant. do something! like increment the numRedDominant count.
if (numTimesRed<=3) {
numTimesRed++;
} //add one
else {
numTimesRed=0;
}
println("RED IS DOMINANT. HAS BEEN "+numTimesRed+" times.");
}
if ((greenCount > redCount) && (greenCount > blueCount)) {
// green is dominant. do something! like increment the numGreenDominant count.
if (numTimesGreen<=3) {
numTimesGreen++;
} //add one
else {
numTimesGreen=0;
}
println("GREEN IS DOMINANT. HAS BEEN "+numTimesGreen+" times.");
}
if ((blueCount > greenCount) && (blueCount > redCount)) {
// blue is dominant. do something! like increment the numBlueDominant variable.
if (numTimesBlue<=3) {
numTimesBlue++;
} //add one
else {
numTimesBlue=0;
}
println("BLUE IS DOMINANT. HAS BEEN "+numTimesBlue+" times.");
}
switch (numTimesRed) {
case 1:
println("Scared"); // Prints "Scared"
break;
case 2:
println("Maybe"); // Prints "Maybe"
break;
case 3:
println("OK"); // Prints "OK"
break;
}
switch (numTimesBlue) {
case 1:
println("Scared"); // Prints "Scared"
break;
case 2:
println("Maybe"); // Prints "Maybe"
break;
case 3:
println("OK"); // Prints "OK"
break;
// case 4:
// println("Like"); // Prints "One"
// break;
}
switch (numTimesGreen) {
case 1:
println("Scared"); // Prints "Scared"
port.write(1); // send 1 to the serial port
break;
case 2:
println("Maybe"); // Prints "Maybe"
port.write(2); // send 2 to the serial port
break;
case 3:
println("OK"); // Prints "OK"
port.write(3); // send 3 to the serial port
break;
}
updatePixels();
}
delay(3000); // check every 3000mS (3 seconds)
}
In other words, it should print:
RED IS DOMINANT. HAS BEEN 1 times.
Scared
RED IS DOMINANT. HAS BEEN 2 times.
Maybe
RED IS DOMINANT. HAS BEEN 3 times.
OK
BLUE IS DOMINANT. HAS BEEN 1 times.
Scared
BLUE IS DOMINANT. HAS BEEN 1 times.
Maybe
and so on. What it is actually doing is this:
RED IS DOMINANT. HAS BEEN 1 times.
Scared
Maybe
OK
RED IS DOMINANT. HAS BEEN 2 times.
Maybe
OK
RED IS DOMINANT. HAS BEEN 3 times.
OK
BLUE IS DOMINANT. HAS BEEN 1 times.
OK
Scared
Maybe
OK
RED IS DOMINANT. HAS BEEN 4 times.
Scared
Maybe
OK
I need it to just print the one case. I think I need some sort of end statement but I'm new to Processing and so far haven't figured out what looks like. Here's my code:
import processing.serial.*;
import processing.video.*;
Serial port;
float val = 0;
Capture camera;
int numTimesRed=0;
int numTimesBlue=0;
int numTimesGreen=0;
void setup()
{
size(320, 240);
println(Capture.list());
camera = new Capture(this, 320, 240);
//println(Serial.list());
port = new Serial(this, Serial.list()[1], 9600);
background(val);
}
void draw()
{
if (camera.available()) { // finds camera s
camera.read();
int redCount=0; // zeros out counts
int greenCount=0;
int blueCount=0;
image(camera, 0, 0);
loadPixels();
for (int i=0; i < pixels.length;i++)
{
int c=pixels[i];
int r=(c>>16)&0x0000FF;
int g=(c>>8)&0x0000FF;
int b=c&0x0000FF;
if (r>g && r>b)
{
r=255;
g=0;
b=0;
redCount++;
}
else if (g>r && g>b)
{
r=0;
g=255;
b=0;
greenCount++;
}
else if (b>r && b>g)
{
r=0;
g=0;
b=255;
blueCount++;
}
else
{
r=255;
g=255;
b=255;
}
pixels[i]=0x000000|(r<<16)|(g<<8)|b;
} // done looping over pixels
// from that frame, what was the dominant color?
if ((redCount > greenCount) && (redCount > blueCount)) {
// red is dominant. do something! like increment the numRedDominant count.
if (numTimesRed<=3) {
numTimesRed++;
} //add one
else {
numTimesRed=0;
}
println("RED IS DOMINANT. HAS BEEN "+numTimesRed+" times.");
}
if ((greenCount > redCount) && (greenCount > blueCount)) {
// green is dominant. do something! like increment the numGreenDominant count.
if (numTimesGreen<=3) {
numTimesGreen++;
} //add one
else {
numTimesGreen=0;
}
println("GREEN IS DOMINANT. HAS BEEN "+numTimesGreen+" times.");
}
if ((blueCount > greenCount) && (blueCount > redCount)) {
// blue is dominant. do something! like increment the numBlueDominant variable.
if (numTimesBlue<=3) {
numTimesBlue++;
} //add one
else {
numTimesBlue=0;
}
println("BLUE IS DOMINANT. HAS BEEN "+numTimesBlue+" times.");
}
switch (numTimesRed) {
case 1:
println("Scared"); // Prints "Scared"
break;
case 2:
println("Maybe"); // Prints "Maybe"
break;
case 3:
println("OK"); // Prints "OK"
break;
}
switch (numTimesBlue) {
case 1:
println("Scared"); // Prints "Scared"
break;
case 2:
println("Maybe"); // Prints "Maybe"
break;
case 3:
println("OK"); // Prints "OK"
break;
// case 4:
// println("Like"); // Prints "One"
// break;
}
switch (numTimesGreen) {
case 1:
println("Scared"); // Prints "Scared"
port.write(1); // send 1 to the serial port
break;
case 2:
println("Maybe"); // Prints "Maybe"
port.write(2); // send 2 to the serial port
break;
case 3:
println("OK"); // Prints "OK"
port.write(3); // send 3 to the serial port
break;
}
updatePixels();
}
delay(3000); // check every 3000mS (3 seconds)
}
1