RGB Sensor Arduino
From Arduino to Processing. An Arduino board collects RGB values and shows pictures of famous paintings.
Devices & Components
1
Resistor 1k ohm
1
Solderless Breadboard Half Size
15
Jumper wires (generic)
2
Resistor 10k ohm
1
RGB Diffused Common Cathode
Software & Tools
1
Processing
Project description
Code
FinalProjectAndrea.pde
java
1 import processing.serial.*; 2 3Serial mySerial; // Create object from Serial class 4String myString = null; 5int nl = 10; 6float myVal; 7int myR, myG, myB, myR2, myG2, myB2; 8 9 10 11PFont font; 12 13 14void setup() { 15 size(1250, 800); 16 pixelDensity(2); 17 String myPort = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port 18 mySerial = new Serial(this, myPort, 9600); 19 //println(myPort); 20 21 font = createFont("Helvetica", 32); 22 23 24} 25 26void draw() { 27 background(255); 28 29 while ( mySerial.available() > 0) { // If data is available, 30 myString = mySerial.readStringUntil(nl); 31 32 if (myString != null) { 33 myVal = float(myString); 34 35// the code below puts the information from the Serial into a processing String Array 36 String[] mySplitString = split(myString, ','); 37 38 if (mySplitString.length >=3) { 39 myR = int(mySplitString[0]); 40 41 / 42 myR2 = (int)map(myR, 0, 7000, 0, 255); 43 myG = int(mySplitString[1]); 44 myG2 = (int)map(myG, 0, 9000, 0, 255); 45 myB = int(mySplitString[2]); 46 myB2 = (int)map(myB, 0, 8000, 0, 255); 47 } 48 49 50 println(mySplitString.length + " " + myR2 + " " + myG2 + " " + myB2 + " " + myString); 51 52 } 53 } 54 55// this if statement is set to display a rectangle if the object over the sensor contains more red that green or blue 56 if (myR2 > myG2 && myR2 > myB2 && myR2 > 20) { 57 58PImage img; 59img = loadImage("TINTORETTO.jpg"); 60image(img, 0, 0); 61 } 62 if (myG2 > myR2 && myG2 > myB2 && myG2 > 20) { 63 64PImage img; 65img = loadImage("MONET.jpg"); 66image(img, 0, 0); 67 } 68 if (myB2 > myR2 && myB2 > myG2 && myB2 > 20) { 69 70PImage img; 71img = loadImage("BARTHES.jpg"); 72image(img, 0, 0); 73 } 74 } 75 76
FinalProjectAndrea.pde
java
1 import processing.serial.*; 2 3Serial mySerial; 4 // Create object from Serial class 5String myString = null; 6int nl = 10; 7float 8 myVal; 9int myR, myG, myB, myR2, myG2, myB2; 10 11 12 13PFont font; 14 15 16void 17 setup() { 18 size(1250, 800); 19 pixelDensity(2); 20 String myPort = Serial.list()[1]; 21 //change the 0 to a 1 or 2 etc. to match your port 22 mySerial = new Serial(this, 23 myPort, 9600); 24 //println(myPort); 25 26 font = createFont("Helvetica", 27 32); 28 29 30} 31 32void draw() { 33 background(255); 34 35 while ( 36 mySerial.available() > 0) { // If data is available, 37 myString = mySerial.readStringUntil(nl); 38 39 40 if (myString != null) { 41 myVal = float(myString); 42 43// the code 44 below puts the information from the Serial into a processing String Array 45 String[] 46 mySplitString = split(myString, ','); 47 48 if (mySplitString.length >=3) 49 { 50 myR = int(mySplitString[0]); 51 52 / 53 myR2 54 = (int)map(myR, 0, 7000, 0, 255); 55 myG = int(mySplitString[1]); 56 myG2 57 = (int)map(myG, 0, 9000, 0, 255); 58 myB = int(mySplitString[2]); 59 myB2 60 = (int)map(myB, 0, 8000, 0, 255); 61 } 62 63 64 println(mySplitString.length 65 + " " + myR2 + " " + myG2 + " " + myB2 + " " + myString); 66 67 } 68 69 } 70 71// this if statement is set to display a rectangle if the object over 72 the sensor contains more red that green or blue 73 if (myR2 > myG2 && myR2 74 > myB2 && myR2 > 20) { 75 76PImage img; 77img = loadImage("TINTORETTO.jpg"); 78image(img, 79 0, 0); 80 } 81 if (myG2 > myR2 && myG2 > myB2 && myG2 > 20) { 82 83PImage 84 img; 85img = loadImage("MONET.jpg"); 86image(img, 0, 0); 87 } 88 if 89 (myB2 > myR2 && myB2 > myG2 && myB2 > 20) { 90 91PImage img; 92img = loadImage("BARTHES.jpg"); 93image(img, 94 0, 0); 95 } 96 } 97 98
rgbAndrea.ino
jade
1#include <Adafruit_TCS34725.h> 2 3#include <Wire.h> 4#include "Adafruit_TCS34725.h" 5 6/* Example code for the Adafruit TCS34725 breakout library */ 7 8/* Connect SCL to analog 5 9 Connect SDA to analog 4 10 Connect VDD to 3.3V DC 11 Connect GROUND to common ground */ 12 13/* Initialise with default values (int time = 2.4ms, gain = 1x) */ 14// Adafruit_TCS34725 tcs = Adafruit_TCS34725(); 15 16/* Initialise with specific int time and gain values */ 17Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_101MS, TCS34725_GAIN_1X); 18 19void setup(void) { 20 Serial.begin(9600); 21 22 if (tcs.begin()) { 23 Serial.println("Found sensor"); 24 } else { 25 Serial.println("No TCS34725 found ... check your connections"); 26 //while (0); 27 } 28 29 // Now we're ready to get readings! 30} 31 32void loop(void) { 33 uint16_t r, g, b, c; 34 // colorTemp, lux; 35 36 tcs.getRawData(&r, &g, &b, &c); 37 38 Serial.print(r, DEC); 39 Serial.print(","); 40 Serial.print(g, DEC); 41 Serial.print(","); 42 Serial.print(b, DEC); 43 Serial.println(","); //IMPORTANT println 44 45 46} 47
Comments
Only logged in users can leave comments