BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Color sorter Blynk

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 &gt; 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 &gt; 29; i--) {

    topServo.write(i);

    delay(2);

  } 

  delay(200);

  for(int i = 29; i &lt; 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&lt;B &amp; B&lt;G){

    color = 1; 

    lcd.setCursor (5,0);

    lcd.print(a);

    delay(500); //Red

  }

  if(R&lt;G &amp; G&lt;B){

    color = 2; 

    lcd.setCursor (13,0);

    lcd.print(b);

    delay(500); //Yellow

  }

  if(G&lt;B &amp; B&lt;R){

    color = 3; 

 

    lcd.setCursor (9,1);

    lcd.print(c);

    delay(500); //Greem

  }

  if(B&lt;R &amp; R&lt;G){

    color = 4;

    lcd.setCursor (14,1);

    lcd.print(d);

    delay(500); // Brown

  }

  if (B&lt;G &amp; G&lt;R){

    color = 5; 

    lcd.setCursor (4,1);

    lcd.print(e);

    delay(500); //Blue

  }

  return color;  

}

Please edit your post and add triple backticks at the beginning and end of your code.
Triple backticks look like this:
```

Pete.

Have done so. Thanks

You should start by reading this:


as your current code won’t work with Blynk.

Pete.

Thank you.
But What alternative do people use to void loop() ?

Did you read the document I linked?

Pete.

I have. And cleaned up the void loop.

Is there any way to send a variable value to Blynk ?

Yes, it was mentioned in the document I linked. It’s called Blynk.virtualWrite

Pete.

Thank you for such quick replies.

My last question is that I have a LCD hardware, and I want to take that data a put it on the LCD on Blynk. How will that be achieve without declaration issue

I don’t understand the question.
If you want to print data to both a physical LCD and an LCD widget then that’s perfectly possible. However many sketches for physical LCDs are badly written by putting the write routines in the void loop. This is totally unnecessary, as the last data written to the LCD will persist until it’s refreshed.

Pete.

Yes that’s exactly what I’m trying to achieve, those cause I’m using an Hardware lcd working with liquid crystal, there’s a conflicting declaration through to Blynk lcd coding as seen in the picture.

Any suggestions ?

I have no code in the void loop. Besides the Blynk.run and timer.run

Posting screenshots isn’t really helpful.
It would be helpful if you posted your updated code, and explained how you’re connecting your Uno to Blynk.

Pete.

The Blynk examples don’t show how you can send values that are varying to the LCD. That’s what I would like to know.
Connecting to Arduino serially

Thanks

So, change one of the declarations to something different… lcd2 perhaps? They are written in code, not stone :stuck_out_tongue:

I realized how stupid my question was :joy: I edited it thanks

1 Like

Either you’re looking in the wrong place, or your knowledge of coding in C++ needs to improve:

There’s no code to do that in the sketch you’ve posted.

Pete.