Indice
Descrizione
Con questo video imparerai ad usare un sensore di colore con Arduino.
A partire dai collegamenti passando per il codice degli sketch scoprirai come individuare il colore degli oggetti e delle superfici.
#define S0_PIN 2
#define S1_PIN 3
#define S2_PIN 4
#define S3_PIN 5
#define OUT_PIN 6
int red;
int green;
int blue;
int clear;
void setup() {
Serial.begin(9600);
pinMode(S0_PIN, OUTPUT);
pinMode(S1_PIN, OUTPUT);
pinMode(S2_PIN, OUTPUT);
pinMode(S3_PIN, OUTPUT);
pinMode(OUT_PIN, INPUT);
// Power down
digitalWrite(S0_PIN, LOW);
digitalWrite(S1_PIN, LOW);
}
void loop() {
updateRGB();
Serial.print("Rosso: ");
Serial.print(red);
Serial.print(" - Verde: ");
Serial.print(green);
Serial.print(" - Blu: ");
Serial.print(blue);
Serial.print(" - Luce: ");
Serial.println(clear);
delay(500);
}
void updateRGB() {
// Frequency 20%
digitalWrite(S0_PIN, HIGH);
digitalWrite(S1_PIN, LOW);
delay(10);
red = 0;
green = 0;
blue = 0;
clear = 0;
for (int i = 0; i < 5; i++) {
digitalWrite(S2_PIN, LOW);
digitalWrite(S3_PIN, LOW);
red += pulseIn(OUT_PIN, LOW);
delay(1);
digitalWrite(S2_PIN, HIGH);
clear += pulseIn(OUT_PIN, LOW);
delay(1);
digitalWrite(S3_PIN, HIGH);
green += pulseIn(OUT_PIN, LOW);
delay(1);
digitalWrite(S2_PIN, LOW);
blue += pulseIn(OUT_PIN, LOW);
delay(1);
}
red /= 5;
green /= 5;
blue /= 5;
clear /= 5;
// Power down
digitalWrite(S0_PIN, LOW);
digitalWrite(S1_PIN, LOW);
}
#include <LiquidCrystal_I2C.h>
#define S0_PIN 2
#define S1_PIN 3
#define S2_PIN 4
#define S3_PIN 5
#define OUT_PIN 6
#define WHITE_THRESHOLD 15
#define BLACK_THRESHOLD 55
int red;
int green;
int blue;
int clear;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(S0_PIN, OUTPUT);
pinMode(S1_PIN, OUTPUT);
pinMode(S2_PIN, OUTPUT);
pinMode(S3_PIN, OUTPUT);
pinMode(OUT_PIN, INPUT);
// Power down
digitalWrite(S0_PIN, LOW);
digitalWrite(S1_PIN, LOW);
}
void loop() {
updateRGB();
String color = getColor();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" COLORE ");
lcd.setCursor(0, 1);
int space = (16 - color.length()) / 2;
for (int i = 0; i < space; ++i) {
lcd.print(" ");
}
lcd.print(color);
delay(500);
}
void updateRGB() {
// Frequency 20%
digitalWrite(S0_PIN, HIGH);
digitalWrite(S1_PIN, LOW);
delay(10);
red = 0;
green = 0;
blue = 0;
clear = 0;
for (int i = 0; i < 5; i++) {
digitalWrite(S2_PIN, LOW);
digitalWrite(S3_PIN, LOW);
red += pulseIn(OUT_PIN, LOW);
delay(1);
digitalWrite(S2_PIN, HIGH);
clear += pulseIn(OUT_PIN, LOW);
delay(1);
digitalWrite(S3_PIN, HIGH);
green += pulseIn(OUT_PIN, LOW);
delay(1);
digitalWrite(S2_PIN, LOW);
blue += pulseIn(OUT_PIN, LOW);
delay(1);
}
red /= 5;
green /= 5;
blue /= 5;
clear /= 5;
// Power down
digitalWrite(S0_PIN, LOW);
digitalWrite(S1_PIN, LOW);
}
String getColor() {
float R_G = (float)red / green;
float G_B = (float)green / blue;
float B_R = (float)blue / red;
if (R_G > 0.75 && R_G < 1.1
&& G_B > 0.9 && G_B < 1.4
&& B_R > 0.75 && B_R < 1.5) {
if (clear <= WHITE_THRESHOLD) {
return "Bianco";
} else if (clear >= BLACK_THRESHOLD) {
return "Nero";
}
return "Grigio";
} else if (R_G <= 0.8 && G_B >= 1.1 && G_B <= 1.4 && B_R >= 1.2) {
return "Rosso";
} else if (R_G > 0.9 && G_B < 1.2 && B_R < 1.45) {
return "Verde";
} else if (R_G >= 0.9 && G_B >= 1.2 && B_R < 0.8) {
return "Blu";
} else if (R_G >= 0.7 && G_B < 1.2 && B_R >= 1) {
return "Giallo";
} else if (R_G < 0.8 && G_B < 1.3 && B_R >= 1) {
return "Arancione";
} else if (G_B > 1.35 && B_R >= 1.1) {
return "Rosa";
} else if (R_G < 0.9 && B_R < 1.2) {
return "Viola";
}
return "";
}