Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define S0 4
- #define S1 5
- #define S2 6
- #define S3 7
- #define out 8
- int pulse, R, G, B, toll, ColorR, ColorG, ColorB;
- void setup() {
- for(int i = S0; i<= S3; i++){
- pinMode(i,OUTPUT);
- }
- pinMode(out, INPUT);
- //Scaling colore a 20%
- digitalWrite(S0, HIGH);
- digitalWrite(S1, LOW);
- //Comunicazione seriale
- Serial.begin(9600);
- }
- void loop() {
- /* Combinazioni
- * S2 S3
- * R: LOW | LOW
- * G: HIGH | HIGH
- * B: LOW | HIGH */
- //R
- digitalWrite(S2, LOW);
- digitalWrite(S3, LOW);
- pulse = pulseIn(out, LOW); //frequenza
- R = map(pulse,25,72,255,0);
- //G
- digitalWrite(S2, HIGH);
- digitalWrite(S3, HIGH);
- pulse = pulseIn(out, LOW);
- G = map(pulse,30,90,255,0);
- //B
- digitalWrite(S2, LOW);
- digitalWrite(S3, HIGH);
- pulse = pulseIn(out, LOW);
- R = map(pulse,25,70,255,0);
- Serial.print("R=");
- Serial.print(R);
- Serial.print("G=");
- Serial.print(G);
- Serial.print("B=");
- Serial.println(B);
- if(isGray(R,G,B) == true){
- Serial.println("Gray");
- }
- else if (isYellow(R,G,B) == true){
- Serial.println("Yellow");
- }
- else if (isRed(R,G,B) == true){
- Serial.println("Red");
- }
- }
- bool isGray(int R, int G, int B){
- ColorR = 127;
- ColorG = 127;
- ColorB = 127;
- bool colorgray = isColor(ColorR, ColorG, ColorB, R ,G ,B);
- return colorgray;
- }
- bool isYellow(int R, int G, int B){
- ColorR = 252;
- ColorG = 241;
- ColorB = 0;
- bool colorgray = isColor(ColorR, ColorG, ColorB, R ,G ,B);
- return colorgray;
- }
- bool isRed(int R, int G, int B){
- ColorR = 245;
- ColorG = 23;
- ColorB = 34;
- bool colorgray = isColor(ColorR, ColorG, ColorB, R ,G ,B);
- return colorgray;
- }
- bool isColor(int ColorR, int ColorG, int ColorB, int R, int G, int B){
- toll = 10; //tolleranza
- if(R <= ColorR+toll && R>= ColorR-toll){
- if(G <= ColorG+toll && G>= ColorG-toll){
- if(B <= ColorB+toll && B>= ColorB-toll){
- return true;
- } else return false;
- } else return false;
- } else return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement