Arduino counter help!!

Help guys, I made a plastic bottle cap sorter. It works with a tcs-3200 color sensor, so when you put a bottle cap (which are painted in 4 colors, red, blue, black and yellow), a servo moves the cap to the color sensor, if its a certain color, another servo changes the direction of which it will fall to a box, so there will be 4 boxes for each color…

So the problem is that I want to count these bottle caps in the Serial monitor, I need to count each color separately, example:

Red=1 Blue=0 Black=3 Yellow= 5

I tried the ++ command, but when I start the serial monitor this happens, if i put the first cap, in this case red, the monitor appears like this:

Red= 1

Then if I put another color, for example black, it will happen this:

Black=2

The arduino is counting just the number of caps passing through the sensor, I need to count them SEPARATELY, …sort of making 4 different counters in the same arduino.

I hope that you could help me solve this problem

Thanks a lot for your help!

Please read the"how to use the forum" stickies to see how to format and post code. You need a count variable for each color if you want to count colors separately (redCnt, blkCnt, etc).

Easy.

Lets say we have a variable that takes note of the color of the current bottle cap in question, named capColor. This will depend on the output of the color sensor. Next, we need a variable holding the number of bottle caps for EACH color. For instance, you’d have:

byte capColor = 0 //1 for red, 2 for blue, 3 for yellow, and 4 for black
int redTotal = 0;
int blueTotal = 0;
int yellowTotal = 0;
int blackTotal = 0;

//update capColor here <--

if(capColor == 1)
{
   redTotal = redTotal + 1;
{
else if (capColor == 2)
{
   blueTotal = blueTotal + 1;
}
//...do this for all colors

Thanks a lot! it helped me a lot :slight_smile: :slight_smile: