I have a project which is a small color sorter machine. And using Arduino uno I want to display the count of the different colors the machine is reading on my Blynk interface.
But I have not seen any examples on seeing this as occur in real time.
Code is as follows:
#include <LiquidCrystal.h>;
#include <Servo.h>;
#define S0 6
#define S1 7
#define S2 8
#define S3 9
#define sensorOut 10
Servo topServo;
Servo bottomServo;
int frequency = 0;
int color=0;
int a=1;
int b=1;
int c=1;
int d=1;
int e=1;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin (16,2);
lcd.setCursor (1,0);
lcd.print("RED=");
lcd.setCursor (9,0);
lcd.print("YLW=");
lcd.setCursor (1,1);
lcd.print("BL=");
lcd.setCursor (6,1);
lcd.print("GN=");
lcd.setCursor (11,1);
lcd.print("BN=");
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
// Setting frequency-scaling to 20%
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
topServo.attach(14);
bottomServo.attach(15);
Serial.begin(9600);
}
void loop() {
topServo.write(115);
delay(500);
for(int i = 115; i > 65; i--) {
topServo.write(i);
delay(2);
}
delay(500);
color = readColor();
delay(10);
switch (color) {
case 1:
bottomServo.write(25);
a ++;// Red
break;
case 2:
bottomServo.write(38);
b ++;// yellow
break;
case 3:
bottomServo.write(66);
c++;// Green
break;
case 4:
bottomServo.write(94);
d ++;// Brown
break;
case 5:
bottomServo.write(125);
e ++; // Blue
break;
case 0:
break;
}
delay(300);
for(int i = 65; i > 29; i--) {
topServo.write(i);
delay(2);
}
delay(200);
for(int i = 29; i < 115; i++) {
topServo.write(i);
delay(2);
}
color=0;
}
// Custom Function - readColor()
int readColor() {
// Setting red filtered photodiodes to be read
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
// Reading the output frequency
frequency = pulseIn(sensorOut, LOW);
int R = frequency;
// Printing the value on the serial monitor
Serial.print("R= ");//printing name
Serial.print(frequency);//printing RED color frequency
Serial.print(" ");
delay(50);
// Setting Green filtered photodiodes to be read
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
// Reading the output frequency
frequency = pulseIn(sensorOut, LOW);
int G = frequency;
// Printing the value on the serial monitor
Serial.print("G= ");//printing name
Serial.print(frequency);//printing RED color frequency
Serial.print(" ");
delay(50);
// Setting Blue filtered photodiodes to be read
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
// Reading the output frequency
frequency = pulseIn(sensorOut, LOW);
int B = frequency;
// Printing the value on the serial monitor
Serial.print("B= ");//printing name
Serial.print(frequency);//printing RED color frequency
Serial.println(" ");
delay(50);
if(R<B & B<G){
color = 1;
lcd.setCursor (5,0);
lcd.print(a);
delay(500); //Red
}
if(R<G & G<B){
color = 2;
lcd.setCursor (13,0);
lcd.print(b);
delay(500); //Yellow
}
if(G<B & B<R){
color = 3;
lcd.setCursor (9,1);
lcd.print(c);
delay(500); //Greem
}
if(B<R & R<G){
color = 4;
lcd.setCursor (14,1);
lcd.print(d);
delay(500); // Brown
}
if (B<G & G<R){
color = 5;
lcd.setCursor (4,1);
lcd.print(e);
delay(500); //Blue
}
return color;
}