// definiranje pinova za spoj RGB Ledice int ledPolje[] = {2,3,4}; // logicka varijabla koja provjerava da li je izvrsena kalibracija boolean balanceSet = false; //place holders for colour detected int red = 0; int green = 0; int blue = 0; //polje za vrijednosti boja float bojaPolje[] = {0,0,0}; float bijelaPolje[] = {0,0,0}; float crnaPolje[] = {0,0,0}; //place holder for average int prosjekOcitanog; void setup(){ //setup the outputs for the colour sensor pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, HIGH); //begin serial communication Serial.begin(9600); } void loop(){ checkBalance(); checkColour(); printColour(); } void checkBalance(){ //check if the balance has been set, if not, set it if(balanceSet == false){ setBalance(); } } void setBalance(){ Serial.println("Postavi bijeli papir ispred senzora");//set white balance delay(5000); //delay for five seconds, this gives us time to get a white sample in front of our sensor //scan the white sample. //go through each light, get a reading, set the base reading for each colour red, green, and blue to the white array for(int i = 0;i<=2;i++){ digitalWrite(ledPolje[i],LOW); delay(100); ocitaj(5); //ocitava bilo koji pin RGB led onoliko puta koliko je brojac bijelaPolje[i] = prosjekOcitanog; digitalWrite(ledPolje[i],HIGH); delay(100); } Serial.println("Postavi crni papir ispred senzora");//set white balance //done scanning white, now it will pulse blue to tell you that it is time for the black (or grey) sample. //set black balance delay(5000); //wait for five seconds so we can position our black sample //go ahead and scan, sets the colour values for red, green, and blue when exposed to black for(int i = 0;i<=2;i++){ digitalWrite(ledPolje[i],LOW); delay(100); ocitaj(5); crnaPolje[i] = prosjekOcitanog; //blackArray[i] = analogRead(2); digitalWrite(ledPolje[i],HIGH); delay(100); } //set boolean value so we know that balance is set balanceSet = true; //delay another 5 seconds to allow the human to catch up to what is going on delay(5000); } void checkColour(){ for(int i = 0;i<=2;i++){ digitalWrite(ledPolje[i],LOW); //turn or the LED, red, green or blue depending which iteration delay(100); //delay to allow CdS to stabalize, they are slow ocitaj(5); //take a reading however many times bojaPolje[i] = prosjekOcitanog; //set the current colour in the array to the average reading float razlikaBoja = bijelaPolje[i] - crnaPolje[i]; //the highest possible return minus the lowest returns the area for values in between bojaPolje[i] = (bojaPolje[i] - crnaPolje[i])/(razlikaBoja)*255; //the reading returned minus the lowest value divided by the possible range multiplied by 255 will give us a value roughly between 0-255 representing the value for the current reflectivity(for the colour it is exposed to) of what is being scanned digitalWrite(ledPolje[i],HIGH); //turn off the current LED delay(100); } } void ocitaj(int brojac){ int ocitanje; int zbroj=0; //ocitava boju onoliko puta koliko je zadan brojac for(int i = 0;i < brojac;i++){ ocitanje = analogRead(0); zbroj = zbroj + ocitanje; delay(10); } //racuna prosjek prosjekOcitanog = (zbroj)/brojac; } //prints the colour in the colour array, in the next step, we will send this to processing to see how good the sensor works. void printColour(){ Serial.print("R = "); Serial.print(int(bojaPolje[0])); Serial.print(" G = "); Serial.print(int(bojaPolje[1])); Serial.print(" B = "); Serial.println(int(bojaPolje[2])); delay(2000); }