/* Color Sensor Calibration color-sensor-calib.ino Calibrate RGB Color Sensor output Pulse Widths Uses values obtained for RGB Sensor Demo sketch DroneBot Workshop 2020 https://dronebotworkshop.com*/// Define color sensor pins#define S0 4#define S1 5#define S2 6#define S3 7#define sensorOut 8// Variables for Color Pulse Width MeasurementsintredPW=0;intgreenPW=0;intbluePW=0;voidsetup(){// Set S0 - S3 as outputspinMode(S0,OUTPUT);pinMode(S1,OUTPUT);pinMode(S2,OUTPUT);pinMode(S3,OUTPUT);// Set Sensor output as inputpinMode(sensorOut,INPUT);// Set Pulse Width scaling to 20%digitalWrite(S0,HIGH);digitalWrite(S1,LOW);// Setup Serial MonitorSerial.begin(9600);}voidloop(){// Read Red Pulse WidthredPW=getRedPW();// Delay to stabilize sensordelay(200);// Read Green Pulse WidthgreenPW=getGreenPW();// Delay to stabilize sensordelay(200);// Read Blue Pulse WidthbluePW=getBluePW();// Delay to stabilize sensordelay(200);// Print output to Serial MonitorSerial.println("RGB("+String(redPW)+","+String(greenPW)+","+String(bluePW)+")");}// Function to read Red Pulse WidthsintgetRedPW(){// Set sensor to read Red onlydigitalWrite(S2,LOW);digitalWrite(S3,LOW);// Define integer to represent Pulse WidthintPW;// Read the output Pulse WidthPW=pulseIn(sensorOut,LOW);// Return the valuereturnPW;}// Function to read Green Pulse WidthsintgetGreenPW(){// Set sensor to read Green onlydigitalWrite(S2,HIGH);digitalWrite(S3,HIGH);// Define integer to represent Pulse WidthintPW;// Read the output Pulse WidthPW=pulseIn(sensorOut,LOW);// Return the valuereturnPW;}// Function to read Blue Pulse WidthsintgetBluePW(){// Set sensor to read Blue onlydigitalWrite(S2,LOW);digitalWrite(S3,HIGH);// Define integer to represent Pulse WidthintPW;// Read the output Pulse WidthPW=pulseIn(sensorOut,LOW);// Return the valuereturnPW;}
/* RGB Color Sensor Demonstration rgb-color-sensor-demo.ino Read RGB values from Color Sensor Must use calibration values from Color Sensor Calibration Sketch DroneBot Workshop 2020 https://dronebotworkshop.com*/// Define color sensor pins#define S0 4#define S1 5#define S2 6#define S3 7#define sensorOut 8// Calibration Values// Get these from Calibration SketchintredMin=28;// Red minimum value pulse width from calibrationintredMax=204;// Red maximum value pulse width from calibrationintgreenMin=30;// Green minimum value pulse width from calibrationintgreenMax=242;// Green maximum value pulse width from calibrationintblueMin=26;// Blue minimum value pulse width from calibrationintblueMax=220;// Blue maximum value pulse width from calibration// Variables for Color Pulse Width MeasurementsintredPW=0;intgreenPW=0;intbluePW=0;// Variables for final Color valuesintredValue;intgreenValue;intblueValue;voidsetup(){// Set S0 - S3 as outputspinMode(S0,OUTPUT);pinMode(S1,OUTPUT);pinMode(S2,OUTPUT);pinMode(S3,OUTPUT);// Set Sensor output as inputpinMode(sensorOut,INPUT);// Set Frequency scaling to 20%digitalWrite(S0,HIGH);digitalWrite(S1,LOW);// Setup Serial MonitorSerial.begin(9600);}voidloop(){// Read Red valueredPW=getRedPW();// Map to value from 0-255redValue=map(redPW,redMin,redMax,255,0);// Delay to stabilize sensordelay(200);// Read Green valuegreenPW=getGreenPW();// Map to value from 0-255greenValue=map(greenPW,greenMin,greenMax,255,0);// Delay to stabilize sensordelay(200);// Read Blue valuebluePW=getBluePW();// Map to value from 0-255blueValue=map(bluePW,blueMin,blueMax,255,0);// Delay to stabilize sensordelay(200);// Print output to Serial MonitorSerial.println("RGB("+String(redValue)+","+String(greenValue)+","+String(blueValue)+")");}// Function to read Red Pulse WidthsintgetRedPW(){// Set sensor to read Red onlydigitalWrite(S2,LOW);digitalWrite(S3,LOW);// Define integer to represent Pulse WidthintPW;// Read the output Pulse WidthPW=pulseIn(sensorOut,LOW);// Return the valuereturnPW;}// Function to read Green Pulse WidthsintgetGreenPW(){// Set sensor to read Green onlydigitalWrite(S2,HIGH);digitalWrite(S3,HIGH);// Define integer to represent Pulse WidthintPW;// Read the output Pulse WidthPW=pulseIn(sensorOut,LOW);// Return the valuereturnPW;}// Function to read Blue Pulse WidthsintgetBluePW(){// Set sensor to read Blue onlydigitalWrite(S2,LOW);digitalWrite(S3,HIGH);// Define integer to represent Pulse WidthintPW;// Read the output Pulse WidthPW=pulseIn(sensorOut,LOW);// Return the valuereturnPW;}
/******************************************************************************ISL29125_basics.inoSimple example for using the ISL29125 RGB sensor library.Jordan McConnell @ SparkFun Electronics11 Apr 2014https://github.com/sparkfun/ISL29125_BreakoutThis example declares an SFE_ISL29125 object called RGB_sensor. The object/sensor is initialized with a basic configuration so that it continuouslysamples the light intensity of red, green and blue spectrums. These values areread from the sensor every 2 seconds and printed to the Serial monitor.Developed/Tested with:Arduino UnoArduino IDE 1.0.5Requires:SFE_ISL29125_LibraryThis code is beerware.Distributed as-is; no warranty is given. ******************************************************************************/#include<Wire.h>#include"SFE_ISL29125.h"// Declare sensor objectSFE_ISL29125RGB_sensor;voidsetup(){// Initialize serial communicationSerial.begin(115200);// Initialize the ISL29125 with simple configuration so it starts samplingif(RGB_sensor.init()){Serial.println("Sensor Initialization Successful\n\r");}}// Read sensor values for each color and print them to serial monitorvoidloop(){// Read sensor values (16 bit integers)unsignedintred=RGB_sensor.readRed();unsignedintgreen=RGB_sensor.readGreen();unsignedintblue=RGB_sensor.readBlue();// Print out readings, change HEX to DEC if you prefer decimal outputSerial.print("Red: ");Serial.println(red,DEC);Serial.print("Green: ");Serial.println(green,DEC);Serial.print("Blue: ");Serial.println(blue,DEC);Serial.println();delay(2000);}