Arduino coloured ball counter PLEASE HELP FOR SCHOOL PROJECT!!

I have a school project where i have to make a colour sorter with a arduino i got it far enough that it sorts it phisically but I also have to sort is digitally

This is how it's supposed to work:

  1. the laptop where the arduino is connected to has to show "press button to start colour sorter"

  2. you put in the balls and it sorts it

  3. then the laptop shows "press button to stop sorting"

  4. you press the button again the machine stops and shows

-total amount of balls

-the amount of red balls,blue balls and white balls (only colours I need to sort)

-and show "press button to restart again"

the arduino file i have as a attachment this is what i have so far

this is what it is going to look like:

arduino_kleur_soorteer.ino (2.18 KB)

Please post your code.
Please post how far your observations fall short of your expectations.

You have not told us what part of your program you need help with. Tell us in as much detail as possible what the program actually does and what you want it to do that is different.

If you need help with receiving commands from your PC have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

it still needs to count the total balls and count the balls individual and it needs the button to show those values and restart the program

so what the code can do is sort the balls physicaly and display the current colour
what i want the code to do extra is display the total counted balls and the total counted colours individual

here's the code

// rood groen blauw
//rode 25-40 70-90 65-78
//witte20-30 38-50 35-43
//blauwe40-50 60-80 40-60

#include <Servo.h>
#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define OUT 6
Servo bovenServo;
Servo onderServo;
int frequency = 0;
int color=0;
void setup() {
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(OUT, INPUT);

digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
bovenServo.attach(7);
onderServo.attach(8);
Serial.begin(9600);
}
void loop() {
bovenServo.write(115);
delay(1000);

for(int i = 115; i > 55; i--) {
bovenServo.write(i);
delay(2);
}
delay(500);

color = readColor();
delay(10);
switch (color) {
case 1:
onderServo.write(50);
break;
case 2:
onderServo.write(100);
break;
case 3:
onderServo.write(150);
break;

case 0:
break;
}
delay(500);

for(int i = 55; i > 29; i--) {
bovenServo.write(i);
delay(2);
}
delay(400);

for(int i = 29; i < 115; i++) {
bovenServo.write(i);
delay(2);
}
color=0;
}

int readColor() {

digitalWrite(S2, LOW);
digitalWrite(S3, LOW);

frequency = pulseIn(OUT, LOW);
int R = frequency;

Serial.print("R= ");
Serial.print(frequency);
Serial.print(" ");
delay(50);

digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);

frequency = pulseIn(OUT, LOW);
int G = frequency;

Serial.print("G= ");
Serial.print(frequency);
Serial.print(" ");
delay(50);

digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);

frequency = pulseIn(OUT, LOW);
int B = frequency;

Serial.print("B= ");
Serial.print(frequency);
Serial.println(" ");
delay(50);

// rood groen blauw
//rode 25-40 70-90 65-78
//witte20-30 35-50 35-43
//blauwe40-50 60-80 40-60

if(R<50 & R>25 & G<90 & G>70){
color = 1; // Rood
//Serial.print("rood");
//Serial.print('\n');
}
if(G<50 & G>35 & B<45 &B>35){
color = 2; // wit
//Serial.print("wit");
//Serial.print('\n');
}
if(G<80 & G>60 & B<60 & B>40){
color = 3; // Blauw
//Serial.print("blauw");
//Serial.print('\n');
}
return color;
}

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Is the code in Reply #3 the same as the code in your Original Post?

It will be much easier to help if you start by making an attempt to do the counting. If you get stuck then post the code that represents your best attempt.

...R

...and that's why we like code tags.

Is there enough space between the balls to see the back wall between every ball? (Obviously make the back wall a colour that matches no ball: white or black maybe.) Then count the number of times it changes from back-wall colour to any ball-colour. If there isn't enough space then you need some other way of detecting two balls of the same colour following each other. Or make them fall faster so the spaces are bigger.