using multiple counters on 1 arduino

hi

i am new here,
and not that good with arduino but i'm learning

i'm trying to build something that can count and color sort plastic coins that are identical in shape and size
sofar i can read a color and push them in the assigned bay but now i'm trying to count the number of coins for each color

so my question : how can i do this
i would like to use the output signal from the color sensor to count each time it detects a certain color in my case green or blue
and than display these two numbers on my display

i'm using a arduino leonard for this setup
don't know if it makes any difference

thanks in advance
Maarten

so my question : how can i do this

You mean something like this:

int redTokens = 0;
int grnTokens = 0;

void loop()
{
   if(getTheColor() == Red)
   {
       redTokens++;
   }
   else if getTheColor() == Grn)
   {
      grnTokens++;
   }

   if(digitalRead(showCountPin) == LOW)
   {
      Serial.print("We've seen ");
      Serial.print(redTokens);
      Serial.print(" red tokens and ");

What you describe sounds simple.
When you detect a coin of a certain colour presumably you do something to push the coin into the appropriate bin. To count the number of coins of that colour add one to a counter at the same time then update the display to show the counters for each colour.

Post your code here and we can give advice as to where and how to do this.

Please read the 2 sticky posts at the top of this page before posting your code noting the use of code tags to help make your code readable on the forum.

thanks for the rapid response

i wil try implementing the piece of code from PaulS

and then i'll post the code i have thus far

again thank you both
sincerely
Maarten

this is the program i have thus far

#include <Wire.h>
#include <Average.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(16x2, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

char EMIC;
int S0=2,S1=3,S2=4,S3=5, LED=6; 
int OUT=7;
int K1=A0,K2=A1,K3=A2,K4=A4;
int OK=12,A=11,B=10,reset=9;
int blueTokens = 0;
int grnTokens = 0;

void setup() { 
  TCS3200_Setup();             // initialize color sensor
  Serial.begin(9600);          // Used to type in characters
  lcd.begin(16,2);             // initialize the lcd for 16 chars 2 lines, turn on backlight
  lcd.setCursor(0,0);          // Start at character 0 on line 0
  lcd.print("welcome");
  delay (1000);
  lcd.clear();  
  RelayModuleSetup();          // initialize relay module
  pinMode(OK,INPUT);           // butons
  pinMode(A,INPUT);            // butons
  pinMode(B,INPUT);            // butons
  pinMode(reset,INPUT);        // butons
  digitalWrite(K1,HIGH);       // start motor
  delay(1000);
}

void loop() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Green Coins");
  lcd.setCursor(0,1);
  lcd.print("Blue Coins");
  if(ReadColor() == Blue)
  {
      blueTokens++;
      digitalWrite(K2,HIGH);         // right bin
      digitalWrite(K3,LOW);
      delay (50);
  }
  else if ReadColor() == Green)
  {
      grnTokens++;
      digitalWrite(K2,HIGH);         //left bin
      digitalWrite(K3,HIGH);
      delay (50);
  }
  lcd.setCursor(13,0);
  lcd.print(grnTokens);
  delay(1);
  lcd.setCursor(13,1);
  lcd.print(blueTokens);
  delay(1);
  

}



//********************************* FUNCTIONS COLOR SENSOR ********************************* 
void TCS3200_Setup() {      //instellen van de digitale pin configuratie
  pinMode(S0,OUTPUT); 
  pinMode(S1,OUTPUT); 
  pinMode(S2,OUTPUT); 
  pinMode(S3,OUTPUT); 
  pinMode(LED,OUTPUT); 
  pinMode(OUT,INPUT); 
}

void TCS3200_On() {
  digitalWrite(LED,HIGH); // Switch LED on
  digitalWrite(S0,HIGH); //Output frequency scaling (100%)
  digitalWrite(S1,HIGH);
  delay(5);
}

void TCS3200_Off() {
  digitalWrite(LED,LOW); // Switch LED off
  digitalWrite(S0,LOW); //Power off sensor
  digitalWrite(S1,LOW);
}

void NoFilter() { //Select no filter
  digitalWrite(S2,HIGH); 
  digitalWrite(S3,LOW);
  delay(5);
}

void RedFilter() { //Select red filter
  digitalWrite(S2,LOW); 
  digitalWrite(S3,LOW);
  delay(5);
}

void GreenFilter() { //Select green filter
  digitalWrite(S2,HIGH); 
  digitalWrite(S3,HIGH);
  delay(5);
}

void BlueFilter() { //Select blue filter
  digitalWrite(S2,LOW); 
  digitalWrite(S3,HIGH);
  delay(5);
}

char* ReadColor() { //0=white, 1=orange, 2=yellow, 3=red, 4=green, 5=blue, 6=object out of range
  float FrequencyClear,FrequencyRed,FrequencyGreen,FrequencyBlue;
  int PercentageRed,PercentageGreen,PercentageBlue;
  TCS3200_On();
  NoFilter();
  FrequencyClear=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
  RedFilter();
  FrequencyRed=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
  GreenFilter();
  FrequencyGreen=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
  BlueFilter();
  FrequencyBlue=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
  TCS3200_Off();
  //Output frequency blue, green, red percentage represents the ratio of the 
  //respective color to the Clear channel absolute value:
  PercentageRed=int((FrequencyRed/FrequencyClear)*100.0);
  PercentageGreen=int((FrequencyGreen/FrequencyClear)*100.0);
  PercentageBlue=int((FrequencyBlue/FrequencyClear)*100.0);
  //Learned blue, green, red percentage values of different colors
  int SavedColorRed[] = {
    28,55,42,50,19,13  }; 
  int SavedColorGreen[] = {
    30,25,36,22,45,26  };
  int SavedColorBlue[] = {
    45,20,20,30,36,58  };
  char* GetColor[] = {
    "white","orange","yellow","red","green","blue",""  };
  int ColorArray[3];
  int i_color; 
  int ClosestColor;
  int MaxDiff;
  int MinDiff=300;
  if(FrequencyClear<1.5)ClosestColor=6; // Object out of range
  else {
    for (i_color=0; i_color<6; i_color++) { //Find closest color
      ColorArray[0]=abs(SavedColorRed[i_color]-PercentageRed);
      ColorArray[1]=abs(SavedColorGreen[i_color]-PercentageGreen);
      ColorArray[2]=abs(SavedColorBlue[i_color]-PercentageBlue);
      MaxDiff=maximum(ColorArray,3);
      if (MaxDiff<MinDiff) {
        MinDiff=MaxDiff;
        ClosestColor=i_color;
      }
    }
  }
  return GetColor[ClosestColor];  
} 

//********************************* FUNCTIONS Relay Module ********************************* 

void RelayModuleSetup(){       //initialize relay module
  pinMode(K1,OUTPUT); 
  pinMode(K2,OUTPUT); 
  pinMode(K3,OUTPUT); 
  pinMode(K4,OUTPUT); 
  digitalWrite(K1,LOW);
  digitalWrite(K2,LOW);
  digitalWrite(K3,LOW);
  digitalWrite(K4,LOW);
}
char* ReadColor()

It looks like you're returning a pointer to an item on the stack - that's a really Bad Idea, and probably unnecessary.

Edit: I don't have a development environment here, but it looks to me like your code as posted won't even compile.

i don't really know what you mean with "It looks like you're returning a pointer to an item on the stack - that's a really Bad Idea, and probably unnecessary."

but indeed the code does not compile
the only error i get is : "error: invalid suffix "x2" on integer constant"

#include <Wire.h>
#include <Average.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(16x2, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

char EMIC;
int S0=2,S1=3,S2=4,S3=5, LED=6; 
int OUT=7;
int K1=A0,K2=A1,K3=A2,K4=A4;
int OK=12,A=11,B=10,reset=9;
int blueTokens = 0;
int grnTokens = 0;

void setup() { 
  TCS3200_Setup();             // initialize color sensor
  Serial.begin(9600);          // Used to type in characters
  lcd.begin(16,2);             // initialize the lcd for 16 chars 2 lines, turn on backlight
  lcd.setCursor(0,0);          // Start at character 0 on line 0
  lcd.print("welcome");
  delay (1000);
  lcd.clear();  
  RelayModuleSetup();          // initialize relay module
  pinMode(OK,INPUT);           // butons
  pinMode(A,INPUT);            // butons
  pinMode(B,INPUT);            // butons
  pinMode(reset,INPUT);        // butons
  digitalWrite(K1,HIGH);       // start motor
  delay(1000);
}

void loop() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Green Coins");
  lcd.setCursor(0,1);
  lcd.print("Blue Coins");
  if(ReadColor() == "blue"){
      blueTokens++;
      digitalWrite(K2,HIGH);         // right bin
      digitalWrite(K3,LOW);
      delay (50);
  }
  else if(ReadColor() == "green"){
      grnTokens++;
      digitalWrite(K2,HIGH);         //left bin
      digitalWrite(K3,HIGH);
      delay (50);
  }
  lcd.setCursor(13,0);
  lcd.print(grnTokens);
  delay(1);
  lcd.setCursor(13,1);
  lcd.print(blueTokens);
  delay(1);
  

}



//********************************* FUNCTIONS COLOR SENSOR ********************************* 
void TCS3200_Setup() {      //instellen van de digitale pin configuratie
  pinMode(S0,OUTPUT); 
  pinMode(S1,OUTPUT); 
  pinMode(S2,OUTPUT); 
  pinMode(S3,OUTPUT); 
  pinMode(LED,OUTPUT); 
  pinMode(OUT,INPUT); 
}

void TCS3200_On() {
  digitalWrite(LED,HIGH); // Switch LED on
  digitalWrite(S0,HIGH); //Output frequency scaling (100%)
  digitalWrite(S1,HIGH);
  delay(5);
}

void TCS3200_Off() {
  digitalWrite(LED,LOW); // Switch LED off
  digitalWrite(S0,LOW); //Power off sensor
  digitalWrite(S1,LOW);
}

void NoFilter() { //Select no filter
  digitalWrite(S2,HIGH); 
  digitalWrite(S3,LOW);
  delay(5);
}

void RedFilter() { //Select red filter
  digitalWrite(S2,LOW); 
  digitalWrite(S3,LOW);
  delay(5);
}

void GreenFilter() { //Select green filter
  digitalWrite(S2,HIGH); 
  digitalWrite(S3,HIGH);
  delay(5);
}

void BlueFilter() { //Select blue filter
  digitalWrite(S2,LOW); 
  digitalWrite(S3,HIGH);
  delay(5);
}

char* ReadColor() { //0=white, 1=orange, 2=yellow, 3=red, 4=green, 5=blue, 6=object out of range
  float FrequencyClear,FrequencyRed,FrequencyGreen,FrequencyBlue;
  int PercentageRed,PercentageGreen,PercentageBlue;
  TCS3200_On();
  NoFilter();
  FrequencyClear=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
  RedFilter();
  FrequencyRed=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
  GreenFilter();
  FrequencyGreen=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
  BlueFilter();
  FrequencyBlue=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
  TCS3200_Off();
  //Output frequency blue, green, red percentage represents the ratio of the 
  //respective color to the Clear channel absolute value:
  PercentageRed=int((FrequencyRed/FrequencyClear)*100.0);
  PercentageGreen=int((FrequencyGreen/FrequencyClear)*100.0);
  PercentageBlue=int((FrequencyBlue/FrequencyClear)*100.0);
  //Learned blue, green, red percentage values of different colors
  int SavedColorRed[] = {
    28,55,42,50,19,13  }; 
  int SavedColorGreen[] = {
    30,25,36,22,45,26  };
  int SavedColorBlue[] = {
    45,20,20,30,36,58  };
  char* GetColor[] = {
    "white","orange","yellow","red","green","blue",""  };
  int ColorArray[3];
  int i_color; 
  int ClosestColor;
  int MaxDiff;
  int MinDiff=300;
  if(FrequencyClear<1.5)ClosestColor=6; // Object out of range
  else {
    for (i_color=0; i_color<6; i_color++) { //Find closest color
      ColorArray[0]=abs(SavedColorRed[i_color]-PercentageRed);
      ColorArray[1]=abs(SavedColorGreen[i_color]-PercentageGreen);
      ColorArray[2]=abs(SavedColorBlue[i_color]-PercentageBlue);
      MaxDiff=maximum(ColorArray,3);
      if (MaxDiff<MinDiff) {
        MinDiff=MaxDiff;
        ClosestColor=i_color;
      }
    }
  }
  return GetColor[ClosestColor];  
} 

//********************************* FUNCTIONS Relay Module ********************************* 

void RelayModuleSetup(){       //initialize relay module
  pinMode(K1,OUTPUT); 
  pinMode(K2,OUTPUT); 
  pinMode(K3,OUTPUT); 
  pinMode(K4,OUTPUT); 
  digitalWrite(K1,LOW);
  digitalWrite(K2,LOW);
  digitalWrite(K3,LOW);
  digitalWrite(K4,LOW);
}

i don't really know what you mean with "It looks like you're returning a pointer to an item on the stack - that's a really Bad Idea, and probably unnecessary."

Do you recognize that the function returns a pointer? A pointer points to something. A pointer that points to space that no longer is reserved is useless. The array that you are pointing to goes out of scope (ceases to be reserved) when the function ends. THAT is what makes returning the pointer a bad idea. The array that you return a pointer to should be global. You should be returning the index into the global array.

LiquidCrystal_I2C lcd(16x2, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

So, why DO you have a "x2" suffix on an integer constant?

In ReadColor() replace:

  //Learned blue, green, red percentage values of different colors
  int SavedColorRed[] = {
    28,55,42,50,19,13  }; 
  int SavedColorGreen[] = {
    30,25,36,22,45,26  };
  int SavedColorBlue[] = {
    45,20,20,30,36,58  };
  char* GetColor[] = {
    "white","orange","yellow","red","green","blue",""  };

by

  //Learned blue, green, red percentage values of different colors
  static int SavedColorRed[] = {
    28,55,42,50,19,13  }; 
  static int SavedColorGreen[] = {
    30,25,36,22,45,26  };
  static int SavedColorBlue[] = {
    45,20,20,30,36,58  };
  static char* GetColor[] = {
    "white","orange","yellow","red","green","blue",""  };

That way those arrays are statically allocated and setup once - not built laboriously
on the stack on every call. The passed back string is statically allocated
anyway, as it happens, but these arrays are static in nature - you can declare
them const as well as they are constant.

the only error i get is : "error: invalid suffix "x2" on integer constant"

That's maybe the only compilation error.

You're not understanding how to compare strings (which I don't really see the need for)

Oh yes, that's a howler. You cannot compare char* style strings using ==,
you have to call strcmp(). (+)

You don't need strings to name states - do some research on enum, or #define
or const keyword. You typically use named small numbers to identify things
in code, not strings.

If you want to print out the values then strings are appropriate(*), but
internally to the program that ends up being a clumsy and fragile way
of coding (experience will show you this, take it on trust for now!)

(+) A char* string is represented as a pointer to char, ie the address of
some chars that form the string. The problem is many different copies
of the string could exist, with different addresses, so comparing addresses
using '==' could give false even though the same string contents are
involved.

The String class is available for a higher-level implementation of
strings, and String objects can be compared by == because the class
overrides == for String objects. String objects have more overheads
than char* strings, and in particular heap-allocate, using more RAM.

(*) You can index an array of string with the small integer value, for
instance.

PaulS:

LiquidCrystal_I2C lcd(16x2, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

So, why DO you have a "x2" suffix on an integer constant?

i found the mistake it must be 0x27 instead of 16x2
and then it compiled

but indeed i loaded the program to my arduino leonard and it does not work
the entier piece of code with the functions of the color sensor i got from somebody who worked with the same sensor
( https://www.youtube.com/watch?v=li_ip0KPfxE )

to be honest i don't really understand all of it
so i'm not capable of rewriting it in another way and stil have the color sensor working properly

Have another look at at reply #10