Advertisement
dcai169

Color Sensor LCD

Jan 8th, 2020
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <LiquidCrystal.h>
  3.  
  4. //These values are the readings of the whitepoint of the led. Take the back of vinyl sticker and put it againts face of sensor
  5. #define LED_RED 7540.0f
  6. #define LED_GREEN 14470.0f
  7. #define LED_BLUE 7270.0f
  8.  
  9. //Calculate the balancing factors
  10. #define BAL_RED (LED_GREEN/LED_RED)
  11. #define BAL_GREEN (LED_GREEN/LED_GREEN)
  12. #define BAL_BLUE (LED_GREEN/LED_BLUE)
  13.  
  14. #define I2C_ADDR 0x52
  15.  
  16. uint8_t readBuff[9];
  17. uint16_t ir=0;
  18. uint16_t red=0;
  19. uint16_t green=0;
  20. uint16_t blue=0;
  21.  
  22. const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
  23. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  24.  
  25. void setup() {
  26.   Wire.begin();
  27.   Serial.begin(115200);
  28.   lcd.begin(16, 2);
  29. //  lcd.print("Hello World!");
  30.   i2cWrite(0x00,0b0110);  //enable light sensor and activate rgb mode
  31.   i2cWrite(0x04,0b01000000); //set to 16 bit resolution for 25ms response time and set measurement rate to 25ms
  32. }
  33.  
  34. void loop() {
  35.   i2cRead(0x0A,readBuff,12);
  36.  
  37.   ir=(readBuff[1]<<8)|readBuff[0];
  38.   green=(readBuff[4]<<8)|readBuff[3];
  39.   blue=(readBuff[7]<<8)|readBuff[6];
  40.   red=(readBuff[10]<<8)|readBuff[9];
  41.  
  42.   red*=BAL_RED;
  43.   green*=BAL_GREEN;
  44.   blue*=BAL_BLUE;
  45.  
  46.  
  47.   //Normalize the readings to brightest channel then apply log scale to better discern the colors.
  48.   float maxV=max(blue,max(red,green));
  49.   red=255*pow(red/maxV,5);
  50.   green=255*pow(green/maxV,5);
  51.   blue=255*pow(blue/maxV,5);
  52.  
  53.   if(blue>254){ //max blue?
  54.     red=0;
  55.     green=0;
  56.     blue=255;
  57.   }
  58.   else if(green>254){ //max green?
  59.     red=0;
  60.     green=255;
  61.     blue=0;
  62.   }
  63.   else if(red>254){   //Max red occurs when it's red or yellow
  64.     if(green<25){     //Check green channel, if it's below threadhold then it's not yellow
  65.       red=255;
  66.       green=0;
  67.       blue=0;
  68.     }
  69.     else{             //red is max and green is significant portion so we can assume it's yellow (though white will also trigger this since we don't check blue channel)
  70.       red=255;
  71.       green=150;
  72.       blue=0;
  73.     }
  74.   }
  75.  
  76.   // print sensor results to lcd
  77.   lcd.print(red);
  78.   lcd.setCursor(4, 0);
  79.  
  80.   lcd.print(green);
  81.   lcd.setCursor(8, 0);
  82.  
  83.   lcd.print(blue);
  84.   lcd.setCursor(0, 0);
  85.  
  86.   delay(25);
  87. }
  88.  
  89.  
  90. void i2cWrite(uint8_t reg, uint8_t val){
  91.     Wire.beginTransmission(I2C_ADDR);
  92.     Wire.write(reg);
  93.     Wire.write(val);
  94.     Wire.endTransmission();
  95. }
  96. void i2cRead(uint8_t reg,uint8_t *val,uint16_t len){
  97.     Wire.beginTransmission(I2C_ADDR);
  98.     Wire.write(reg);
  99.     Wire.endTransmission();
  100.     Wire.requestFrom(I2C_ADDR, len);
  101.     for(uint8_t i=0;i<len;i++){
  102.       val[i]=Wire.read();
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement