Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Arduino pins:
- #define S0 2
- #define S1 3
- #define S2 4
- #define S3 5
- #define sensorOut 6
- #define redLED 7
- #define greenLED 8
- #define blueLED 9
- int a=9;
- //Output from the sensor:
- int redFrequency = 0;
- int greenFrequency = 0;
- int blueFrequency = 0;
- //Formatted color values:
- int redColor = 0;
- int greenColor = 0;
- int blueColor = 0;
- //Values used for calibration
- int redMin;
- int redMax;
- int greenMin;
- int greenMax;
- int blueMin;
- int blueMax;
- int color = 0;
- void setup() {
- //Declarations:
- pinMode(S0, OUTPUT);
- pinMode(S1, OUTPUT);
- pinMode(S2, OUTPUT);
- pinMode(S3, OUTPUT);
- pinMode(redLED, OUTPUT);
- pinMode(greenLED, OUTPUT);
- pinMode(blueLED, OUTPUT);
- pinMode(13, OUTPUT);
- pinMode(sensorOut, INPUT);
- // Set frequency scaling to 20%:
- digitalWrite(S0, HIGH);
- digitalWrite(S1, LOW);
- Serial.begin(9600);//begin serial communication
- calibrate();//calibrate sensor (look at serial monitor)
- }
- void loop() {
- readColor();//read sensor
- decideColor();//format color values
- printColor();//print values
- }
- void decideColor() {//format color values
- //Limit possible values:
- redColor = constrain(redColor, 0, 255);
- greenColor = constrain(greenColor, 0, 255);
- blueColor = constrain(blueColor, 0, 255);
- //find brightest color:
- int maxVal = max(redColor, blueColor);
- maxVal = max(maxVal, greenColor);
- //map new values
- redColor = map(redColor, 0, maxVal, 0, 255);
- greenColor = map(greenColor, 0, maxVal, 0, 255);
- blueColor = map(blueColor, 0, maxVal, 0, 255);
- redColor = constrain(redColor, 0, 255);
- greenColor = constrain(greenColor, 0, 255);
- blueColor = constrain(blueColor, 0, 255);
- //decide which color is present (you may need to change some values here):
- if (redColor > 220 && greenColor > 220 && blueColor > 220) {
- color = 1;//white
- }
- else if (redColor > 250 && greenColor < 10 && blueColor < 10) {
- color = 6;//red
- }
- else if (redColor > 250 && greenColor > 185 && greenColor < 230 && blueColor >130 && blueColor < 150) {
- color = 3;//orange
- }
- else if (redColor > 230 && greenColor > 200 && greenColor < 250 && blueColor >100 && blueColor < 170) {
- color = 4;//yellow
- }
- else if (redColor < 25 && greenColor < 25 && blueColor < 25) {
- color = 2;//black
- }
- else if (redColor < 110 && greenColor >200 && blueColor > 250) {
- color = 5;//blue
- }
- else if (redColor < 200 && greenColor > 250 && blueColor < 200) {
- color = 7;//green
- }
- else if (redColor > 180 && redColor < 250 && greenColor > 150 && greenColor < 220 && blueColor > 250) {
- color = 8;//violet
- }
- else if (redColor < 10 && greenColor < 10 && blueColor > 250) {
- color = 9;//indigo
- }
- else {
- color = 0;//unknown
- }
- }
- void calibrate() {
- digitalWrite(S2, LOW);
- digitalWrite(S3, LOW);
- redMin = 11;//pulseIn(sensorOut, LOW);
- //delay(100);
- digitalWrite(S2, HIGH);
- digitalWrite(S3, HIGH);
- greenMin = 11;//pulseIn(sensorOut, LOW);
- //delay(100);
- digitalWrite(S2, LOW);
- digitalWrite(S3, HIGH);
- blueMin = 4;//pulseIn(sensorOut, LOW);
- //delay(100);
- //Serial.println("next...");//aim sensor at something black
- digitalWrite(S2, LOW);
- digitalWrite(S3, LOW);
- redMax = 59;// pulseIn(sensorOut, LOW);
- //delay(100);
- digitalWrite(S2, HIGH);
- digitalWrite(S3, HIGH);
- greenMax = 70;//pulseIn(sensorOut, LOW);
- //delay(100);
- digitalWrite(S2, LOW);
- digitalWrite(S3, HIGH);
- blueMax = 54;//pulseIn(sensorOut, LOW);
- //delay(100);
- //Serial.println("Done calibrating.");
- }
- //a=1;b=1;c=1;d=1;e=1;f=1;g=1;h=1;i=1;j=1;
- void printColor() {//print data
- delay(100);
- // Serial.print("R = ");
- // Serial.print(redColor);
- // Serial.print(" G = ");
- // Serial.print(greenColor);
- // Serial.print(" B = ");
- // Serial.print(blueColor);
- // Serial.print(" Color: ");
- /*if (color=="1" and a!=0){
- Serial.println("1");//white
- a=0;
- }
- else if (color=="2" and a!=1){
- Serial.println("2");//blak
- a=1;
- }
- else if (color=="3" and a!=2){
- Serial.println("3");//org
- a=2;
- }
- else if (color=="4" and a!=3){
- Serial.println("4");//yellow
- a=3;
- }
- else if (color=="5" and a!=4){
- Serial.println("5");//blue
- a=4;
- }
- else if (color=="6" and a!=5){
- Serial.println("6");//red
- a=5;
- }
- else if (color=="7" and a!=6){
- Serial.println("7");//green
- a=6;
- }
- else if (color=="8" and a!=7){
- Serial.println("8");//violet
- a=7;
- }
- else if (color="9" and a!=8){
- Serial.println("9");//indigo
- a=8;
- }
- else {
- //Serial.println("9");//unknown
- a=9;
- }*/
- switch (color) {
- case 1: Serial.println("1"); break;//white
- case 2: Serial.println("2"); break;//blk
- case 3: Serial.println("3"); break;//org
- case 4: Serial.println("4"); break;//yel
- case 5: Serial.println("5"); break;//blue
- case 6: Serial.println("6"); break;//red
- case 7: Serial.println("7"); break;//gree
- case 8: Serial.println("8"); break;//violet
- case 9: Serial.println("9"); break;//indigo
- //default: Serial.println("0"); break;//unknown
- }
- }
- void readColor() {//get data from sensor
- //red:
- digitalWrite(S2, LOW);
- digitalWrite(S3, LOW);
- redFrequency = pulseIn(sensorOut, LOW);
- redColor = map(redFrequency, redMin, redMax, 255, 0);
- //delay(100);
- //green:
- digitalWrite(S2, HIGH);
- digitalWrite(S3, HIGH);
- greenFrequency = pulseIn(sensorOut, LOW);
- greenColor = map(greenFrequency, greenMin, greenMax, 255, 0);
- //delay(100);
- //blue:
- digitalWrite(S2, LOW);
- digitalWrite(S3, HIGH);
- blueFrequency = pulseIn(sensorOut, LOW);
- blueColor = map(blueFrequency, blueMin, blueMax, 255, 0);
- //delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement