Components and supplies
Color Photo Sensor, RGB
Arduino UNO
Resistor 100k ohm
Jumper wires (generic)
Male/Female Jumper Wires
RGB Diffused Common Anode
Project description
Code
Color Identificator
arduino
1 2int S2 = 7; //Setting up pins of the color sensor 3int S3 = 8; //Setting up pins of the color sensor 4int outPin = 4; //Setting up pins of the color sensor 5unsigned int redPulseWidth; //variable to measure the red component in the color shown to the color sensor 6unsigned int greenPulseWidth; //variable to measure the green component in the color shown to the color sensor 7unsigned int bluePulseWidth; //variable to measure the blue component in the color shown to the color sensor 8float redVolt; //variable to store the voltage to be written to red pin of the RGB led 9float greenVolt; //variable to store the voltage to be written to green pin of the RGB led 10float blueVolt; //variable to store the voltage to be written to blue pin of the RGB led 11 12void setup() { 13 pinMode(S2, OUTPUT); //setting pins S2 AND S3 as output pins 14 pinMode(S3, OUTPUT); 15 pinMode(outPin, INPUT); //setting outPin as input pin 16 Serial.begin(9600); //starting the serial port 17} 18 19void loop() { 20 digitalWrite(S2, LOW); //by setting S2 AND S3 low we are reading the red component of the color 21 digitalWrite(S3, LOW); 22 redPulseWidth = pulseIn(outPin, LOW); //we are storing the red component as a number between 1,02,400. P.S. THE BIGGER THE NUMBER SMALLER THE COLOR COMPONENT AND VICE VERSA 23 24 digitalWrite(S2, LOW); //by setting S2 low and S3 high we are reading the blue component of the color 25 digitalWrite(S3, HIGH); 26 bluePulseWidth = pulseIn(outPin, LOW); //we are storing the blue component as a number between 0 and 1,02,400 27 28 digitalWrite(S2, HIGH); //by setting S2 AND S3 high we are reading the green componet of the color 29 digitalWrite(S3, HIGH); 30 greenPulseWidth = pulseIn(outPin, LOW); //we are storing the green component as a number between 0 and 1,02,400 31 32 redVolt = redPulseWidth/400.-1; //turning the pulse widths into numbers between 0 and 255 33 redVolt = (255 - redVolt); //then reversing the smaller number into bigger one and bigger number into smaller one 34 greenVolt = greenPulseWidth/400.-1; 35 greenVolt = (255 - greenVolt); 36 blueVolt = bluePulseWidth/400.-1; 37 blueVolt = (255 - blueVolt); 38 39 Serial.print(redVolt); 40 Serial.print(", "); 41 Serial.print(blueVolt); 42 Serial.print(", "); 43 Serial.println(greenVolt); 44 delay(250); 45 46 //writing all the conditions and setting the redVolt, greenVolt, and blueVolt accordingly because as you can see in the serial monitor the red, blue, and green components are similar 47 //and if we pass on these values straight to the RGB it won't work. Hence, we are writing the following conditions 48 if(redVolt>greenVolt && greenVolt>blueVolt){ 49 redVolt = 255; 50 greenVolt = greenVolt/2; 51 blueVolt = 0; 52 } 53 54 if(redVolt>blueVolt && greenVolt<blueVolt){ 55 redVolt = 255; 56 blueVolt = blueVolt/2; 57 greenVolt = 0; 58 } 59 60 if(greenVolt>redVolt && blueVolt<redVolt){ 61 greenVolt = 255; 62 redVolt = redVolt/2; 63 blueVolt = 0; 64 } 65 66 67 if(greenVolt>blueVolt && blueVolt>redVolt){ 68 greenVolt = 255; 69 blueVolt = blueVolt/2; 70 redVolt = 0; 71 } 72 73 74 if(blueVolt>redVolt && redVolt>greenVolt){ 75 blueVolt = 255; 76 redVolt = redVolt/2; 77 greenVolt = 0; 78 } 79 80 81 if(blueVolt>greenVolt && greenVolt>redVolt){ 82 redVolt = 0; 83 greenVolt = greenVolt/2; 84 blueVolt = 255; 85 } 86 redVolt = redVolt*8; 87 blueVolt = blueVolt/8; 88 greenVolt = greenVolt/8; 89 90 analogWrite(10, redVolt); //writing values to RGB pins of the colour 91 analogWrite(9, greenVolt); 92 analogWrite(3, blueVolt); 93 94}
Comments
Only logged in users can leave comments
SavarJR
0 Followers
•0 Projects
0